internal static void RenderAlphaImage(Graphics g, Image image, Rectangle imageRect, float alpha) { using (ImageAttributes imageAttributes = new ImageAttributes()) { ColorMap colorMap = new ColorMap(); colorMap.OldColor = Color.FromArgb(0xff, 0, 0xff, 0); colorMap.NewColor = Color.FromArgb(0, 0, 0, 0); ColorMap[] remapTable = new ColorMap[] { colorMap }; imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap); float[][] CS_0_0001 = new float[5][]; float[] CS_0_0002 = new float[5]; CS_0_0002[0] = 1f; CS_0_0001[0] = CS_0_0002; float[] CS_0_0003 = new float[5]; CS_0_0003[1] = 1f; CS_0_0001[1] = CS_0_0003; float[] CS_0_0004 = new float[5]; CS_0_0004[2] = 1f; CS_0_0001[2] = CS_0_0004; float[] CS_0_0005 = new float[5]; CS_0_0005[3] = alpha; CS_0_0001[3] = CS_0_0005; float[] CS_0_0006 = new float[5]; CS_0_0006[4] = 1f; CS_0_0001[4] = CS_0_0006; float[][] colorMatrixElements = CS_0_0001; ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements); imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); g.DrawImage(image, imageRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttributes); } }
public static void ConvertImage(Bitmap image, Graphics g, Color ForeColor, Color BackColor, bool isActive) { using (ImageAttributes imageAttributes = new ImageAttributes()) { ColorMap[] colorMap = new ColorMap[2]; colorMap[0] = new ColorMap(); colorMap[0].OldColor = Color.FromArgb(0, 0, 0); colorMap[0].NewColor = ForeColor; colorMap[1] = new ColorMap(); colorMap[1].OldColor = image.GetPixel(0, 0); colorMap[1].NewColor = isActive ? BackColor : Color.Transparent; imageAttributes.SetRemapTable(colorMap); g.DrawImage( image, new Rectangle(0, 0, image.Width, image.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttributes); } }
/// <summary> /// Will draw the image, replacing oldColor with the foreColor. /// And also replacing the color at [0,0] to be the default transparent color. /// Usefull for operations with system BMP images. /// </summary> public static void DrawImageColorMapped(Graphics g, Bitmap image, Rectangle rectangle, Color oldColor, Color foreColor) { ColorMap[] colorMap = new ColorMap[2]; colorMap[0] = new ColorMap(); colorMap[0].OldColor = oldColor; colorMap[0].NewColor = foreColor; colorMap[1] = new ColorMap(); colorMap[1].OldColor = image.GetPixel(0, 0); colorMap[1].NewColor = Color.Transparent; using (ImageAttributes imageAttributes = new ImageAttributes()) { imageAttributes.SetRemapTable(colorMap); g.DrawImage( image, //new Rectangle(0, 0, image.Width, image.Height), rectangle, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttributes); } }
protected override void OnPaint(PaintEventArgs e) { if (IsMouseOver && Enabled) { using (Pen pen = new Pen(ForeColor)) { e.Graphics.DrawRectangle(pen, Rectangle.Inflate(ClientRectangle, -1, -1)); } } using (ImageAttributes imageAttributes = new ImageAttributes()) { ColorMap[] colorMap = new ColorMap[2]; colorMap[0] = new ColorMap(); colorMap[0].OldColor = Color.FromArgb(0, 0, 0); colorMap[0].NewColor = ForeColor; colorMap[1] = new ColorMap(); colorMap[1].OldColor = Image.GetPixel(0, 0); colorMap[1].NewColor = Color.Transparent; imageAttributes.SetRemapTable(colorMap); e.Graphics.DrawImage( Image, new Rectangle(0, 0, Image.Width, Image.Height), 0, 0, Image.Width, Image.Height, GraphicsUnit.Pixel, imageAttributes); } base.OnPaint(e); }
public override void PaintValue(PaintValueEventArgs e) { if (!(e.Context.Instance is KrbTabControl)) return; var parent = (KrbTabControl)e.Context.Instance; var caption = (ButtonsCaption)e.Value; using (var brush = new LinearGradientBrush(e.Bounds, parent.GradientCaption.InactiveCaptionColorStart, parent.GradientCaption.InactiveCaptionColorEnd, parent.GradientCaption.CaptionGradientStyle)) { var bl = new Blend(2) { Factors = new[] { 0.1F, 1.0F }, Positions = new[] { 0.0F, 1.0F } }; brush.Blend = bl; e.Graphics.FillRectangle(brush, e.Bounds); Image captionDropDown = Resources.DropDown; using (var attributes = new ImageAttributes()) { var map = new[] { new ColorMap {OldColor = Color.White, NewColor = Color.Transparent}, new ColorMap {OldColor = Color.Black, NewColor = caption.InactiveCaptionButtonsColor} }; attributes.SetRemapTable(map); var rect = e.Bounds; rect.Inflate(-3, 0); e.Graphics.DrawImage(captionDropDown, rect, 0, 0, captionDropDown.Width, captionDropDown.Height, GraphicsUnit.Pixel, attributes); } } }
internal static void RenderAlphaImage(Graphics g, Image image, Rectangle imageRect, float alpha) { using (ImageAttributes attributes = new ImageAttributes()) { ColorMap map = new ColorMap { OldColor = Color.FromArgb(0xff, 0, 0xff, 0), NewColor = Color.FromArgb(0, 0, 0, 0) }; ColorMap[] mapArray = new ColorMap[] { map }; attributes.SetRemapTable(mapArray, ColorAdjustType.Bitmap); float[][] numArray2 = new float[5][]; float[] numArray3 = new float[5]; numArray3[0] = 1f; numArray2[0] = numArray3; float[] numArray4 = new float[5]; numArray4[1] = 1f; numArray2[1] = numArray4; float[] numArray5 = new float[5]; numArray5[2] = 1f; numArray2[2] = numArray5; float[] numArray6 = new float[5]; numArray6[3] = alpha; numArray2[3] = numArray6; float[] numArray7 = new float[5]; numArray7[4] = 1f; numArray2[4] = numArray7; float[][] newColorMatrix = numArray2; ColorMatrix matrix = new ColorMatrix(newColorMatrix); attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); g.DrawImage(image, imageRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes); } }
protected override void WndProc(ref Message m) { switch (m.Msg) { case LpsWindowsApiDefine.WM_PAINT: Bitmap bmpCaptured = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height); Bitmap bmpResult = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height); Rectangle r = new Rectangle(0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height); CaptureWindow(this, ref bmpCaptured); this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); this.BackColor = Color.Transparent; ImageAttributes imgAttrib = new ImageAttributes(); ColorMap[] colorMap = new ColorMap[1]; colorMap[0] = new ColorMap(); colorMap[0].OldColor = Color.White; colorMap[0].NewColor = Color.Transparent; imgAttrib.SetRemapTable(colorMap); Graphics g = Graphics.FromImage(bmpResult); g.DrawImage(bmpCaptured, r, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, GraphicsUnit.Pixel, imgAttrib); g.Dispose(); pictureBox.Image = (Image)bmpResult.Clone(); break; case LpsWindowsApiDefine.WM_HSCROLL: case LpsWindowsApiDefine.WM_VSCROLL: this.Invalidate(); // repaint // if you use scrolling then add these two case statements break; } //switch (m.WParam.ToInt32()) //{ // case WindowsApiDefine.WM_LBUTTONDOWN: // p = new Point(Cursor.Position.X, Cursor.Position.Y); // break; //} base.WndProc(ref m); }
public void Show() { if (!isInit) { isInit = true; using (Bitmap newBmp = new Bitmap(16, 16)) { Graphics g = Graphics.FromImage(newBmp); ColorMap[] colorMap = new ColorMap[1]; colorMap[0] = new ColorMap(); colorMap[0].OldColor = Color.Fuchsia; colorMap[0].NewColor = Color.FromKnownColor(KnownColor.ButtonFace); ImageAttributes attr = new ImageAttributes(); attr.SetRemapTable(colorMap); g.DrawImage(tbBmp_tbTab, new Rectangle(0, 0, 16, 16), 0, 0, 16, 16, GraphicsUnit.Pixel, attr); tbIcon = Icon.FromHandle(newBmp.GetHicon()); } NppTbData _nppTbData = new NppTbData(); _nppTbData.hClient = this.Handle; _nppTbData.pszName = this.FormName(); _nppTbData.dlgID = id; _nppTbData.uMask = FormMask; _nppTbData.hIconTab = (uint)tbIcon.Handle; _nppTbData.pszModuleName = UnmanagedExports.main.PluginName(); IntPtr _ptrNppTbData = Marshal.AllocHGlobal(Marshal.SizeOf(_nppTbData)); Marshal.StructureToPtr(_nppTbData, _ptrNppTbData, false); Win32.SendMessage(PluginBase.nppData._nppHandle, NppMsg.NPPM_DMMREGASDCKDLG, 0, _ptrNppTbData); } else { Win32.SendMessage(PluginBase.nppData._nppHandle, NppMsg.NPPM_DMMSHOW, 0, this.Handle); } }
public override void draw(List<Cluster> clusters) { //I added this to subtract from the X, Y coord //to center the heat map. float radius = this._grid.cell_width / 2; if (this.surface == null) this.clear(); surface.TranslateTransform(-this.grid.location.X, -this.grid.location.Y); PointF pasdf = this._grid.scale_to_screen_coords(new Location(1, 1)); ImageAttributes remapper = new ImageAttributes(); remapper.SetRemapTable(ColorHeatMapClusterRenderer.color_map); HeatMapClusterRenderer hm = new HeatMapClusterRenderer(this.surface, this._grid); hm.draw(clusters); Rectangle dest_rectangle = new Rectangle(this.grid.location, this.grid.size); dest_rectangle.Width += this.grid.location.X * 2; dest_rectangle.Height += this.grid.location.Y * 2; for (int i = 0; i < 12; ++i) this.g.DrawImage(output, dest_rectangle, 0, 0, output.Width, output.Height, GraphicsUnit.Pixel, remapper); }
public static Bitmap Threshold(Bitmap original) { int width = original.Width; int height = original.Height; Bitmap bmp = new Bitmap(width, height); using (Graphics g = Graphics.FromImage(bmp)) { ImageAttributes threshold = new ImageAttributes(); threshold.SetThreshold(0.92f); //attributes.SetThreshold(0.08f); g.DrawImage(original, new Rectangle(0, 0, width, height), 0, 0, width, height, GraphicsUnit.Pixel, threshold); ImageAttributes invert = new ImageAttributes(); ColorMap[] map = new ColorMap[2]; map[0] = new ColorMap(); map[0].OldColor = Color.Black; map[0].NewColor = Color.White; map[1] = new ColorMap(); map[1].OldColor = Color.White; map[1].NewColor = Color.Black; invert.SetRemapTable(map); g.DrawImage(bmp, new Rectangle(0, 0, width, height), 0, 0, width, height, GraphicsUnit.Pixel, invert); } return bmp; }
public static Bitmap Colorize(Bitmap Mask, byte Alpha) { // Create new bitmap to act as a work surface for the colorization process Bitmap Output = new Bitmap(Mask.Width, Mask.Height, PixelFormat.Format32bppArgb); // Create a graphics object from our memory bitmap so we can draw on it and clear it's drawing surface using (Graphics Surface = Graphics.FromImage(Output)) { Surface.Clear(Color.Transparent); // Build an array of color mappings to remap our greyscale mask to full color // Accept an alpha byte to specify the transparancy of the output image ColorMap[] Colors = CreatePaletteIndex(Alpha); // Create new image attributes class to handle the color remappings // Inject our color map array to instruct the image attributes class how to do the colorization ImageAttributes Remapper = new ImageAttributes(); Remapper.SetRemapTable(Colors); // Draw our mask onto our memory bitmap work surface using the new color mapping scheme Surface.DrawImage(Mask, new Rectangle(0, 0, Mask.Width, Mask.Height), 0, 0, Mask.Width, Mask.Height, GraphicsUnit.Pixel, Remapper); } // Send back newly colorized memory bitmap return Output; }
//********************************************************* // /// <summary> /// Control ctor /// </summary> // //********************************************************* public LostPiecesControl() { InitializeComponent(); m_colorMapWhite = new ColorMap(); m_colorMapWhite.OldColor = System.Drawing.Color.FromArgb(255, 255, 0, 0); m_colorMapWhite.NewColor = System.Drawing.Color.FromArgb(255, System.Drawing.Color.White); m_colorMapTblWhite = new ColorMap[] { m_colorMapWhite }; m_imgAttrWhite = new ImageAttributes(); m_bDesignMode = false; m_imgAttrWhite.SetRemapTable(m_colorMapTblWhite); }
/// <summary> /// 添加图片水印 /// </summary> /// <param name="path">原图片绝对地址</param> /// <param name="suiyi">水印文件</param> /// <param name="pos">水印位置</param> private static byte[] addWaterMark(Image image, string suiyi, WaterPositionMode pos) { try { Bitmap b = new Bitmap(image.Width, image.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); Graphics g = Graphics.FromImage(b); g.Clear(Color.White); g.DrawImage(image, 0, 0, image.Width, image.Height); System.Drawing.Image watermark = new Bitmap(suiyi); System.Drawing.Imaging.ImageAttributes imageAttributes = new System.Drawing.Imaging.ImageAttributes(); System.Drawing.Imaging.ColorMap colorMap = new System.Drawing.Imaging.ColorMap(); colorMap.OldColor = Color.FromArgb(255, 0, 255, 0); colorMap.NewColor = Color.FromArgb(0, 0, 0, 0); System.Drawing.Imaging.ColorMap[] remapTable = { colorMap }; imageAttributes.SetRemapTable(remapTable, System.Drawing.Imaging.ColorAdjustType.Bitmap); float[][] colorMatrixElements = { new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f }, new float[] { 0.0f, 1.0f, 0.0f, 0.0f, 0.0f }, new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f }, new float[] { 0.0f, 0.0f, 0.0f, 1.0f, 0.0f },//设置透明度 new float[] { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f } }; System.Drawing.Imaging.ColorMatrix colorMatrix = new System.Drawing.Imaging.ColorMatrix(colorMatrixElements); imageAttributes.SetColorMatrix(colorMatrix, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap); int xpos = 0; int ypos = 0; Point position = SetMarkPoint(pos, watermark, image); xpos = position.X; // ((image.Width - watermark.Width) - 50);//水印位置 ypos = position.Y; //image.Height - watermark.Height - 50;//水印位置 g.DrawImage(watermark, new Rectangle(xpos, ypos, watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes); watermark.Dispose(); imageAttributes.Dispose(); MemoryStream ms = new MemoryStream(); b.Save(ms, ImageFormat.Bmp); byte[] byteImage = ms.ToArray(); b.Dispose(); image.Dispose(); g.Dispose(); return(byteImage); } catch (Exception ex) { MemoryStream ms = new MemoryStream(); image.Save(ms, ImageFormat.Bmp); return(ms.ToArray()); } }
/* internal static void myMenuFunction() { MessageBox.Show("Hello N++!"); } * */ internal static void myDockableDialog() { if (frmMyDlg == null) { frmMyDlg = new frmMyDlg(); using (Bitmap newBmp = new Bitmap(16, 16)) { Graphics g = Graphics.FromImage(newBmp); ColorMap[] colorMap = new ColorMap[1]; colorMap[0] = new ColorMap(); colorMap[0].OldColor = Color.Fuchsia; colorMap[0].NewColor = Color.FromKnownColor(KnownColor.ButtonFace); ImageAttributes attr = new ImageAttributes(); attr.SetRemapTable(colorMap); g.DrawImage(tbBmp_tbTab, new Rectangle(0, 0, 16, 16), 0, 0, 16, 16, GraphicsUnit.Pixel, attr); tbIcon = Icon.FromHandle(newBmp.GetHicon()); } NppTbData _nppTbData = new NppTbData(); _nppTbData.hClient = frmMyDlg.Handle; _nppTbData.pszName = "Linq2Lines query"; _nppTbData.dlgID = idMyDlg; _nppTbData.uMask = NppTbMsg.DWS_DF_CONT_BOTTOM | NppTbMsg.DWS_ICONTAB | NppTbMsg.DWS_ICONBAR; _nppTbData.hIconTab = (uint)tbIcon.Handle; _nppTbData.pszModuleName = PluginName; IntPtr _ptrNppTbData = Marshal.AllocHGlobal(Marshal.SizeOf(_nppTbData)); Marshal.StructureToPtr(_nppTbData, _ptrNppTbData, false); Win32.SendMessage(PluginBase.nppData._nppHandle, NppMsg.NPPM_DMMREGASDCKDLG, 0, _ptrNppTbData); // init config if (xmlConfig != null) { try { frmMyDlg.QueryText = xmlConfig.Element("query").Value; frmMyDlg.HelpersText = xmlConfig.Element("helpers").Value; } catch (Exception ex) { MessageBox.Show("Linq2Lines error: " + ex.Message); } } } else { Win32.SendMessage(PluginBase.nppData._nppHandle, NppMsg.NPPM_DMMSHOW, 0, frmMyDlg.Handle); } }
/// <summary> /// 添加图片水印 /// </summary> /// <param name="path">原图片绝对地址</param> /// <param name="suiyi">水印文件</param> public void addWaterMark(string oldfile, string newfile, string suiyi) { System.Drawing.Image image = System.Drawing.Image.FromFile(oldfile); Bitmap b = new Bitmap(image.Width, image.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); Graphics g = Graphics.FromImage(b); g.Clear(Color.White); g.DrawImage(image, 0, 0, image.Width, image.Height); System.Drawing.Image watermark = new Bitmap(suiyi); System.Drawing.Imaging.ImageAttributes imageAttributes = new System.Drawing.Imaging.ImageAttributes(); System.Drawing.Imaging.ColorMap colorMap = new System.Drawing.Imaging.ColorMap(); colorMap.OldColor = Color.FromArgb(255, 0, 255, 0); colorMap.NewColor = Color.FromArgb(0, 0, 0, 0); System.Drawing.Imaging.ColorMap[] remapTable = { colorMap }; imageAttributes.SetRemapTable(remapTable, System.Drawing.Imaging.ColorAdjustType.Bitmap); float[][] colorMatrixElements = { new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f }, new float[] { 0.0f, 1.0f, 0.0f, 0.0f, 0.0f }, new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f }, new float[] { 0.0f, 0.0f, 0.0f, 1.0f, 0.0f },//设置透明度0.3f new float[] { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f } }; System.Drawing.Imaging.ColorMatrix colorMatrix = new System.Drawing.Imaging.ColorMatrix(colorMatrixElements); imageAttributes.SetColorMatrix(colorMatrix, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap); int xpos = 0; int ypos = 0; xpos = (image.Width - watermark.Width) / 2; //水印位置 ypos = (image.Height - watermark.Height) / 2; //水印位置 g.DrawImage(watermark, new Rectangle(xpos, ypos, watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes); watermark.Dispose(); imageAttributes.Dispose(); b.Save(newfile); b.Dispose(); image.Dispose(); if (File.Exists(oldfile))//删除原始文件 { File.Delete(oldfile); } }
private static void RemapColor(Bitmap image, Color oldColor, Color newColor) { var graphics = Graphics.FromImage(image); var imageAttributes = new ImageAttributes(); var colorMap = new ColorMap(); colorMap.OldColor = oldColor; colorMap.NewColor = newColor; ColorMap[] remapTable = { colorMap }; imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap); graphics.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttributes); graphics.Dispose(); }
//public static byte[] ToByteArray2(this Image myImage) //{ // int width = myImage.Width; // int height = myImage.Height; // System.Windows.Media.PixelFormat format = System.Windows.Media.PixelFormats.Bgr32; // byte[] pixels = new byte[width * height * ((format.BitsPerPixel + 7) / 8)]; // Bitmap bitm = new Bitmap(myImage); // //bitm.LockBits(); // //bitm.UnlockBits(); // //int stride = width * format.BitsPerPixel / 8; // return pixels; //} //int height = frame.FrameDescription.Height; //public static List<System.Drawing.Color> ToColorList_Slow(this Image image) //{ // List<int> raw = new List<int>(); // Bitmap img = new Bitmap(image); // List<System.Drawing.Color> pixels = new List<System.Drawing.Color>(); // for (int y = 0; y < img.Height; y++) // { // for (int x = 0; x < img.Width; ++x) // pixels.Add(img.GetPixel(x, y)); // } // return pixels; //} //public static List<System.Drawing.Color> ToColorList(this Image image) //Bitmap original //{ // List<System.Drawing.Color> pixels = new List<System.Drawing.Color>(); // unsafe // { // //create an empty bitmap the same size as original // Bitmap newBitmap = new Bitmap(image); // //lock the original bitmap in memory // System.Drawing.Imaging.BitmapData bitmapData = newBitmap.LockBits(new Rectangle(0, 0, newBitmap.Width, newBitmap.Height), // System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); // ////lock the new bitmap in memory // //System.Drawing.Imaging.BitmapData newData = newBitmap.LockBits(new Rectangle(0, 0, newBitmap.Width, newBitmap.Height), // // System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); // //set the number of bytes per pixel // int pixelSize = 3; // for (int y = 0; y < image.Height; y++) // { // //get the data from the original image // byte* oRow = (byte*)bitmapData.Scan0 + (y * bitmapData.Stride); // //get the data from the new image // byte* nRow = (byte*)bitmapData.Scan0 + (y * bitmapData.Stride); // for (int x = 0; x < newBitmap.Width; x++) // { // //create the grayscale version // byte r = (byte)(oRow[x * pixelSize]); // byte g = (byte)(oRow[x * pixelSize + 1]); // byte b = (byte)(oRow[x * pixelSize + 2]); // System.Drawing.Color color = System.Drawing.Color.FromArgb(r, g, b); // pixels.Add(color); // } // } // //unlock the bitmaps // newBitmap.UnlockBits(bitmapData); // return pixels; // } //} public static void OverwriteImageRegion(this Image image, Image destinationImage, System.Drawing.Rectangle myRect) { System.Drawing.Image newImage = (System.Drawing.Image)image.Clone(); System.Drawing.Graphics g1 = System.Drawing.Graphics.FromImage(newImage); // Create a color map. System.Drawing.Imaging.ColorMap colorMap = new System.Drawing.Imaging.ColorMap(); System.Drawing.Imaging.ColorMap[] colorMapArray = new System.Drawing.Imaging.ColorMap[1]; colorMapArray[0].OldColor = System.Drawing.Color.Red; colorMapArray[0].NewColor = System.Drawing.Color.Beige; //= new System.Drawing.Imaging.ColorMap() {colorMap}; //Create an ImageAttributes object, and then pass the transformerobject to the SetRemapTable method. System.Drawing.Imaging.ImageAttributes imageAttr = new System.Drawing.Imaging.ImageAttributes(); imageAttr.SetRemapTable(colorMapArray); g1.DrawImage(newImage, new System.Drawing.Rectangle(0, 0, newImage.Width, newImage.Height), 0, 0, newImage.Width, newImage.Height, System.Drawing.GraphicsUnit.Pixel, imageAttr); }
static private Bitmap watermark(string path) { string suiyi = ConfigurationManager.AppSettings["Watermark"]; if (suiyi == null) { return(null); } System.Drawing.Image image = System.Drawing.Image.FromFile(path); Bitmap b = new Bitmap(image.Width, image.Height); Graphics g = Graphics.FromImage(b); g.Clear(Color.Transparent); g.DrawImage(image, 0, 0, image.Width, image.Height); System.Drawing.Image watermark = new Bitmap(suiyi); System.Drawing.Imaging.ImageAttributes imageAttributes = new System.Drawing.Imaging.ImageAttributes(); System.Drawing.Imaging.ColorMap colorMap = new System.Drawing.Imaging.ColorMap(); colorMap.OldColor = Color.FromArgb(255, 0, 255, 0); colorMap.NewColor = Color.FromArgb(0, 0, 0, 0); System.Drawing.Imaging.ColorMap[] remapTable = { colorMap }; imageAttributes.SetRemapTable(remapTable, System.Drawing.Imaging.ColorAdjustType.Bitmap); float[][] colorMatrixElements = { new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f }, new float[] { 0.0f, 1.0f, 0.0f, 0.0f, 0.0f }, new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f }, new float[] { 0.0f, 0.0f, 0.0f, 0.2f, 0.0f }, new float[] { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f } }; System.Drawing.Imaging.ColorMatrix colorMatrix = new System.Drawing.Imaging.ColorMatrix(colorMatrixElements); imageAttributes.SetColorMatrix(colorMatrix, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap); int xpos = 0; int ypos = 0; xpos = ((image.Width - watermark.Width) - 0); ypos = image.Height - watermark.Height - 0; g.DrawImage(watermark, new Rectangle(xpos, ypos, watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes); watermark.Dispose(); imageAttributes.Dispose(); return(b); }
public static ImageAttributes GetTransparencyAttributes(float value) { ImageAttributes imageAttributes = new ImageAttributes(); ColorMap colorMap = new ColorMap(); colorMap.OldColor = Color.FromArgb(255, 0, 255, 0); colorMap.NewColor = Color.FromArgb(0, 0, 0, 0); ColorMap[] remapTable = { colorMap }; imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap); float[][] colorMatrixElements = { new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 0.0f, value, 0.0f}, new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f} }; ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements); imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); return imageAttributes; }
internal static void myDockableDialog() { if (frmMyDlg == null) { frmMyDlg = new ClipboardForm(); using (Bitmap newBmp = new Bitmap(16, 16)) { Graphics g = Graphics.FromImage(newBmp); ColorMap[] colorMap = new ColorMap[1]; colorMap[0] = new ColorMap(); colorMap[0].OldColor = Color.Fuchsia; colorMap[0].NewColor = Color.FromKnownColor(KnownColor.ButtonFace); ImageAttributes attr = new ImageAttributes(); attr.SetRemapTable(colorMap); g.DrawImage(tbBmp_tbTab, new Rectangle(0, 0, 16, 16), 0, 0, 16, 16, GraphicsUnit.Pixel, attr); tbIcon = Icon.FromHandle(newBmp.GetHicon()); } NppTbData _nppTbData = new NppTbData(); _nppTbData.hClient = frmMyDlg.Handle; _nppTbData.pszName = "Clipboard Helper"; _nppTbData.dlgID = idMyDlg; _nppTbData.uMask = NppTbMsg.DWS_DF_CONT_RIGHT | NppTbMsg.DWS_ICONTAB | NppTbMsg.DWS_ICONBAR; _nppTbData.hIconTab = (uint)tbIcon.Handle; _nppTbData.pszModuleName = PluginName; IntPtr _ptrNppTbData = Marshal.AllocHGlobal(Marshal.SizeOf(_nppTbData)); Marshal.StructureToPtr(_nppTbData, _ptrNppTbData, false); Win32.SendMessage(PluginBase.nppData._nppHandle, NppMsg.NPPM_DMMREGASDCKDLG, 0, _ptrNppTbData); isDialogVisible = true; } else { Win32.SendMessage(PluginBase.nppData._nppHandle, isDialogVisible ? NppMsg.NPPM_DMMHIDE : NppMsg.NPPM_DMMSHOW, 0, frmMyDlg.Handle); isDialogVisible = !isDialogVisible; } frmMyDlg.SetItems(repository.Get()); }
internal static void RenderAlphaImage( Graphics g, Image image, Rectangle imageRect, float alpha) { using (ImageAttributes imageAttributes = new ImageAttributes()) { ColorMap colorMap = new ColorMap(); colorMap.OldColor = Color.FromArgb(255, 0, 255, 0); colorMap.NewColor = Color.FromArgb(0, 0, 0, 0); ColorMap[] remapTable = { colorMap }; imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap); float[][] colorMatrixElements = { new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 0.0f, alpha, 0.0f}, new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}}; ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements); imageAttributes.SetColorMatrix( wmColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); g.DrawImage( image, imageRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttributes); } }
public void addStamp(Graphics graphics, String stampImagePath) { Image image2 = Image.FromFile(stampImagePath); //graphics.Clear(Color.Black); ImageAttributes imageAttributes = new ImageAttributes(); ColorMap colorMap = new ColorMap(); colorMap.OldColor = Color.FromArgb(255, 0, 255, 0); colorMap.NewColor = Color.FromArgb(0, 0, 0, 0); ColorMap[] remapTable = { colorMap }; imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap); float[][] colorMatrixElements = { new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 0.0f, 0.3f, 0.0f}, new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f} }; ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements); imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); Point stampPoint = new Point(30, 20); //Add stamp on top graphics.DrawImage(image2, stampPoint); //Add stamp as watermark //graphics.DrawImage(image2, new Rectangle(30, 20, image2.Width, image2.Height), 0, 0, image2.Width, image2.Height, GraphicsUnit.Pixel, imageAttributes); //graphics.DrawImage(image2, new Point(0, image1.Height - image2.Height)); graphics.Dispose(); image2.Dispose(); }
protected override void OnPaint (PaintEventArgs e) { base.OnPaint(e); if(color != null) { // replace transparent color with selected color Graphics g = e.Graphics; //Graphics g = Graphics.FromImage(Image); ColorMap[] colorMap = new ColorMap[1]; colorMap[0] = new ColorMap(); colorMap[0].OldColor = Color.Magenta;//this.ImageTransparentColor; colorMap[0].NewColor = color; ImageAttributes attr = new ImageAttributes(); attr.SetRemapTable(colorMap); Rectangle rect = new Rectangle(0, 0, Image.Width, Image.Height); // todo find a way to retrieve transparency offset automatically // for now, we use the public variable Offset to define this manually rect.Offset(Offset.X,Offset.Y); //Image. Debug.WriteLine("paint!"+this.Text+": "+color); //ssif(color.Equals(Color.Transparent)) ((Bitmap)Image).MakeTransparent(Color.Magenta); g.DrawImage(Image, rect, 0, 0, rect.Width, rect.Height, GraphicsUnit.Pixel, attr); //this.Image.In } }
/// <summary> /// 图片等比缩放 /// </summary> /// <remarks>吴剑 2012-08-08</remarks> /// <param name="fromFile">原图Stream对象</param> /// <param name="savePath">缩略图存放地址</param> /// <param name="targetWidth">指定的最大宽度</param> /// <param name="targetHeight">指定的最大高度</param> /// <param name="watermarkText">水印文字(为""表示不使用水印)</param> /// <param name="watermarkImage">水印图片路径(为""表示不使用水印)</param> public static void ZoomAuto(System.IO.Stream fromFile, string savePath, System.Double targetWidth, System.Double targetHeight, string watermarkText, string watermarkImage) { //创建目录 string dir = Path.GetDirectoryName(savePath); if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); //原始图片(获取原始图片创建对象,并使用流中嵌入的颜色管理信息) System.Drawing.Image initImage = System.Drawing.Image.FromStream(fromFile, true); //原图宽高均小于模版,不作处理,直接保存 if (initImage.Width <= targetWidth && initImage.Height <= targetHeight) { //文字水印 if (watermarkText != "") { using (System.Drawing.Graphics gWater = System.Drawing.Graphics.FromImage(initImage)) { System.Drawing.Font fontWater = new Font("黑体", 10); System.Drawing.Brush brushWater = new SolidBrush(Color.White); gWater.DrawString(watermarkText, fontWater, brushWater, 10, 10); gWater.Dispose(); } } //透明图片水印 if (watermarkImage != "") { if (File.Exists(watermarkImage)) { //获取水印图片 using (System.Drawing.Image wrImage = System.Drawing.Image.FromFile(watermarkImage)) { //水印绘制条件:原始图片宽高均大于或等于水印图片 if (initImage.Width >= wrImage.Width && initImage.Height >= wrImage.Height) { Graphics gWater = Graphics.FromImage(initImage); //透明属性 ImageAttributes imgAttributes = new ImageAttributes(); ColorMap colorMap = new ColorMap(); colorMap.OldColor = Color.FromArgb(255, 0, 255, 0); colorMap.NewColor = Color.FromArgb(0, 0, 0, 0); ColorMap[] remapTable = { colorMap }; imgAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap); float[][] colorMatrixElements = { new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 0.0f, 0.5f, 0.0f},//透明度:0.5 new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f} }; ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements); imgAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); gWater.DrawImage(wrImage, new Rectangle(initImage.Width - wrImage.Width, initImage.Height - wrImage.Height, wrImage.Width, wrImage.Height), 0, 0, wrImage.Width, wrImage.Height, GraphicsUnit.Pixel, imgAttributes); gWater.Dispose(); } wrImage.Dispose(); } } } //保存 initImage.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg); } else { //缩略图宽、高计算 double newWidth = initImage.Width; double newHeight = initImage.Height; //宽大于高或宽等于高(横图或正方) if (initImage.Width > initImage.Height || initImage.Width == initImage.Height) { //如果宽大于模版 if (initImage.Width > targetWidth) { //宽按模版,高按比例缩放 newWidth = targetWidth; newHeight = initImage.Height * (targetWidth / initImage.Width); } } //高大于宽(竖图) else { //如果高大于模版 if (initImage.Height > targetHeight) { //高按模版,宽按比例缩放 newHeight = targetHeight; newWidth = initImage.Width * (targetHeight / initImage.Height); } } //生成新图 //新建一个bmp图片 System.Drawing.Image newImage = new System.Drawing.Bitmap((int)newWidth, (int)newHeight); //新建一个画板 System.Drawing.Graphics newG = System.Drawing.Graphics.FromImage(newImage); //设置质量 newG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; newG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //置背景色 newG.Clear(Color.White); //画图 newG.DrawImage(initImage, new System.Drawing.Rectangle(0, 0, newImage.Width, newImage.Height), new System.Drawing.Rectangle(0, 0, initImage.Width, initImage.Height), System.Drawing.GraphicsUnit.Pixel); //文字水印 if (watermarkText != "") { using (System.Drawing.Graphics gWater = System.Drawing.Graphics.FromImage(newImage)) { System.Drawing.Font fontWater = new Font("宋体", 10); System.Drawing.Brush brushWater = new SolidBrush(Color.White); gWater.DrawString(watermarkText, fontWater, brushWater, 10, 10); gWater.Dispose(); } } //透明图片水印 if (watermarkImage != "") { if (File.Exists(watermarkImage)) { //获取水印图片 using (System.Drawing.Image wrImage = System.Drawing.Image.FromFile(watermarkImage)) { //水印绘制条件:原始图片宽高均大于或等于水印图片 if (newImage.Width >= wrImage.Width && newImage.Height >= wrImage.Height) { Graphics gWater = Graphics.FromImage(newImage); //透明属性 ImageAttributes imgAttributes = new ImageAttributes(); ColorMap colorMap = new ColorMap(); colorMap.OldColor = Color.FromArgb(255, 0, 255, 0); colorMap.NewColor = Color.FromArgb(0, 0, 0, 0); ColorMap[] remapTable = { colorMap }; imgAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap); float[][] colorMatrixElements = { new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 0.0f, 0.5f, 0.0f},//透明度:0.5 new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f} }; ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements); imgAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); gWater.DrawImage(wrImage, new Rectangle(newImage.Width - wrImage.Width, newImage.Height - wrImage.Height, wrImage.Width, wrImage.Height), 0, 0, wrImage.Width, wrImage.Height, GraphicsUnit.Pixel, imgAttributes); gWater.Dispose(); } wrImage.Dispose(); } } } //保存缩略图 newImage.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg); //释放资源 newG.Dispose(); newImage.Dispose(); initImage.Dispose(); } }
internal static void DrawImageReplaceColor(Graphics g, Image image, Rectangle dest, Color oldColor, Color newColor) { ImageAttributes imageAttrs = new ImageAttributes(); ColorMap map = new ColorMap { OldColor = oldColor, NewColor = newColor }; imageAttrs.SetRemapTable(new ColorMap[] { map }, ColorAdjustType.Bitmap); g.DrawImage(image, dest, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttrs, null, IntPtr.Zero); imageAttrs.Dispose(); }
private static void DrawFrameControl(Graphics graphics, int x, int y, int width, int height, int kind, int state, Color foreColor, Color backColor) { if (graphics == null) { throw new ArgumentNullException("graphics"); } if (width < 0) { throw new ArgumentOutOfRangeException("width"); } if (height < 0) { throw new ArgumentOutOfRangeException("height"); } System.Windows.Forms.NativeMethods.RECT rect = System.Windows.Forms.NativeMethods.RECT.FromXYWH(0, 0, width, height); using (Bitmap bitmap = new Bitmap(width, height)) { using (Graphics graphics2 = Graphics.FromImage(bitmap)) { graphics2.Clear(Color.Transparent); using (WindowsGraphics graphics3 = WindowsGraphics.FromGraphics(graphics2)) { System.Windows.Forms.SafeNativeMethods.DrawFrameControl(new HandleRef(graphics3, graphics3.DeviceContext.Hdc), ref rect, kind, state); } if ((foreColor == Color.Empty) || (backColor == Color.Empty)) { graphics.DrawImage(bitmap, x, y); } else { ImageAttributes imageAttrs = new ImageAttributes(); ColorMap map = new ColorMap { OldColor = Color.Black, NewColor = foreColor }; ColorMap map2 = new ColorMap { OldColor = Color.White, NewColor = backColor }; imageAttrs.SetRemapTable(new ColorMap[] { map, map2 }, ColorAdjustType.Bitmap); graphics.DrawImage(bitmap, new Rectangle(x, y, width, height), 0, 0, width, height, GraphicsUnit.Pixel, imageAttrs, null, IntPtr.Zero); } } } }
/// <summary> /// 图片水印 /// </summary> /// <param name="imgPath">服务器图片相对路径</param> /// <param name="filename">保存文件名</param> /// <param name="watermarkFilename">水印文件相对路径</param> /// <param name="watermarkStatus">图片水印位置 0=不使用 1=左上 2=中上 3=右上 4=左中 9=右下</param> /// <param name="quality">附加水印图片质量,0-100</param> /// <param name="watermarkTransparency">水印的透明度 1--10 10为不透明</param> public static void AddImageSignPic(string imgPath, string filename, string watermarkFilename, int watermarkStatus, int quality, int watermarkTransparency) { if(!File.Exists(Utils.GetMapPath(imgPath))) return; byte[] _ImageBytes = File.ReadAllBytes(Utils.GetMapPath(imgPath)); Image img = Image.FromStream(new System.IO.MemoryStream(_ImageBytes)); filename = Utils.GetMapPath(filename); if (watermarkFilename.StartsWith("/") == false) watermarkFilename = "/" + watermarkFilename; watermarkFilename = Utils.GetMapPath(watermarkFilename); if (!File.Exists(watermarkFilename)) return; Graphics g = Graphics.FromImage(img); //设置高质量插值法 //g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度 //g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; Image watermark = new Bitmap(watermarkFilename); if (watermark.Height >= img.Height || watermark.Width >= img.Width) return; ImageAttributes imageAttributes = new ImageAttributes(); ColorMap colorMap = new ColorMap(); colorMap.OldColor = Color.FromArgb(255, 0, 255, 0); colorMap.NewColor = Color.FromArgb(0, 0, 0, 0); ColorMap[] remapTable = { colorMap }; imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap); float transparency = 0.5F; if (watermarkTransparency >= 1 && watermarkTransparency <= 10) transparency = (watermarkTransparency / 10.0F); float[][] colorMatrixElements = { new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 0.0f, transparency, 0.0f}, new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f} }; ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements); imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); int xpos = 0; int ypos = 0; switch (watermarkStatus) { case 1: xpos = (int)(img.Width * (float).01); ypos = (int)(img.Height * (float).01); break; case 2: xpos = (int)((img.Width * (float).50) - (watermark.Width / 2)); ypos = (int)(img.Height * (float).01); break; case 3: xpos = (int)((img.Width * (float).99) - (watermark.Width)); ypos = (int)(img.Height * (float).01); break; case 4: xpos = (int)(img.Width * (float).01); ypos = (int)((img.Height * (float).50) - (watermark.Height / 2)); break; case 5: xpos = (int)((img.Width * (float).50) - (watermark.Width / 2)); ypos = (int)((img.Height * (float).50) - (watermark.Height / 2)); break; case 6: xpos = (int)((img.Width * (float).99) - (watermark.Width)); ypos = (int)((img.Height * (float).50) - (watermark.Height / 2)); break; case 7: xpos = (int)(img.Width * (float).01); ypos = (int)((img.Height * (float).99) - watermark.Height); break; case 8: xpos = (int)((img.Width * (float).50) - (watermark.Width / 2)); ypos = (int)((img.Height * (float).99) - watermark.Height); break; case 9: xpos = (int)((img.Width * (float).99) - (watermark.Width)); ypos = (int)((img.Height * (float).99) - watermark.Height); break; } g.DrawImage(watermark, new Rectangle(xpos, ypos, watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes); ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo ici = null; foreach (ImageCodecInfo codec in codecs) { if (codec.MimeType.IndexOf("jpeg") > -1) ici = codec; } EncoderParameters encoderParams = new EncoderParameters(); long[] qualityParam = new long[1]; if (quality < 0 || quality > 100) quality = 80; qualityParam[0] = quality; EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityParam); encoderParams.Param[0] = encoderParam; if (ici != null) img.Save(filename, ici, encoderParams); else img.Save(filename); g.Dispose(); img.Dispose(); watermark.Dispose(); imageAttributes.Dispose(); }
public MemoryStream AddImageSignPic(Image img, string watermarkFilename, int watermarkStatus, int quality, int watermarkTransparency) { Graphics graphics = null; Image image = null; ImageAttributes imageAttr = null; MemoryStream stream = null; MemoryStream stream2; try { graphics = Graphics.FromImage(img); image = new Bitmap(watermarkFilename); imageAttr = new ImageAttributes(); ColorMap map = new ColorMap { OldColor = Color.FromArgb(0xff, 0, 0xff, 0), NewColor = Color.FromArgb(0, 0, 0, 0) }; ColorMap[] mapArray = new ColorMap[] { map }; imageAttr.SetRemapTable(mapArray, ColorAdjustType.Bitmap); float num = 0.5f; if ((watermarkTransparency >= 1) && (watermarkTransparency <= 10)) { num = ((float) watermarkTransparency) / 10f; } float[][] numArray3 = new float[5][]; float[] numArray4 = new float[5]; numArray4[0] = 1f; numArray3[0] = numArray4; float[] numArray5 = new float[5]; numArray5[1] = 1f; numArray3[1] = numArray5; float[] numArray6 = new float[5]; numArray6[2] = 1f; numArray3[2] = numArray6; float[] numArray7 = new float[5]; numArray7[3] = num; numArray3[3] = numArray7; float[] numArray8 = new float[5]; numArray8[4] = 1f; numArray3[4] = numArray8; float[][] newColorMatrix = numArray3; ColorMatrix matrix = new ColorMatrix(newColorMatrix); imageAttr.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); int x = 0; int y = 0; switch (watermarkStatus) { case 1: x = (int) (img.Width * 0.01f); y = (int) (img.Height * 0.01f); break; case 2: x = ((int) (img.Width * 0.5f)) - (image.Width / 2); y = (int) (img.Height * 0.01f); break; case 3: x = ((int) (img.Width * 0.99f)) - image.Width; y = (int) (img.Height * 0.01f); break; case 4: x = (int) (img.Width * 0.01f); y = ((int) (img.Height * 0.5f)) - (image.Height / 2); break; case 5: x = ((int) (img.Width * 0.5f)) - (image.Width / 2); y = ((int) (img.Height * 0.5f)) - (image.Height / 2); break; case 6: x = ((int) (img.Width * 0.99f)) - image.Width; y = ((int) (img.Height * 0.5f)) - (image.Height / 2); break; case 7: x = (int) (img.Width * 0.01f); y = ((int) (img.Height * 0.99f)) - image.Height; break; case 8: x = ((int) (img.Width * 0.5f)) - (image.Width / 2); y = ((int) (img.Height * 0.99f)) - image.Height; break; case 9: x = ((int) (img.Width * 0.99f)) - image.Width; y = ((int) (img.Height * 0.99f)) - image.Height; break; } graphics.DrawImage(image, new Rectangle(x, y, image.Width, image.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttr); ImageCodecInfo[] imageEncoders = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo encoder = null; foreach (ImageCodecInfo info2 in imageEncoders) { if (info2.MimeType.IndexOf("jpeg") > -1) { encoder = info2; } } EncoderParameters encoderParams = new EncoderParameters(); long[] numArray2 = new long[1]; if ((quality < 0) || (quality > 100)) { quality = 80; } numArray2[0] = quality; EncoderParameter parameter = new EncoderParameter(Encoder.Quality, numArray2); encoderParams.Param[0] = parameter; stream = new MemoryStream(); if (encoder != null) { img.Save(stream, encoder, encoderParams); } stream2 = stream; } catch { stream = null; stream2 = stream; } finally { if (graphics != null) { graphics.Dispose(); } if (img != null) { img.Dispose(); } if (image != null) { image.Dispose(); } if (imageAttr != null) { imageAttr.Dispose(); } } return stream2; }
/// <summary> /// 加图片水印 /// </summary> /// <param name="sourceImgMS">要加水印的原图(System.Drawing)</param> /// <param name="watermarkImg">水印文件名</param> /// <param name="watermarkStatus">图片水印位置1=左上 2=中上 3=右上 4=左中 5=中中 6=右中 7=左下 8=右中 9=右下</param> /// <param name="quality">加水印后的质量0~100,数字越大质量越高</param> /// <param name="watermarkTransparency">水印图片的透明度1~10,数字越小越透明,10为不透明</param> ///<param name="imgExt">图片后缀</param> public static MemoryStream ImageWaterMarkPic(MemoryStream sourceImgMS, MemoryStream watermarkImg, int watermarkStatus, int quality, int watermarkTransparency,string imgExt) { Image sourceImg = Image.FromStream(sourceImgMS);//原图 Image watermark = Image.FromStream(watermarkImg);//水印图 ImageFormat tFormat = sourceImg.RawFormat;//获取原图格式 Graphics g = Graphics.FromImage(sourceImg); //设置高质量插值法 //g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度 //g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; if (watermark.Height >= sourceImg.Height || watermark.Width >= sourceImg.Width) throw new Exception("水印图片大于原图"); ImageAttributes imageAttributes = new ImageAttributes(); ColorMap colorMap = new ColorMap(); colorMap.OldColor = Color.FromArgb(255, 0, 255, 0); colorMap.NewColor = Color.FromArgb(0, 0, 0, 0); ColorMap[] remapTable = { colorMap }; imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap); float transparency = 0.5F; if (watermarkTransparency >= 1 && watermarkTransparency <= 10) transparency = (watermarkTransparency / 10.0F); float[][] colorMatrixElements = { new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 0.0f, transparency, 0.0f}, new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f} }; ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements); imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); int xpos = 0; int ypos = 0; switch (watermarkStatus) { case 1: xpos = (int)(sourceImg.Width * (float).01); ypos = (int)(sourceImg.Height * (float).01); break; case 2: xpos = (int)((sourceImg.Width * (float).50) - (watermark.Width / 2)); ypos = (int)(sourceImg.Height * (float).01); break; case 3: xpos = (int)((sourceImg.Width * (float).99) - (watermark.Width)); ypos = (int)(sourceImg.Height * (float).01); break; case 4: xpos = (int)(sourceImg.Width * (float).01); ypos = (int)((sourceImg.Height * (float).50) - (watermark.Height / 2)); break; case 5: xpos = (int)((sourceImg.Width * (float).50) - (watermark.Width / 2)); ypos = (int)((sourceImg.Height * (float).50) - (watermark.Height / 2)); break; case 6: xpos = (int)((sourceImg.Width * (float).99) - (watermark.Width)); ypos = (int)((sourceImg.Height * (float).50) - (watermark.Height / 2)); break; case 7: xpos = (int)(sourceImg.Width * (float).01); ypos = (int)((sourceImg.Height * (float).99) - watermark.Height); break; case 8: xpos = (int)((sourceImg.Width * (float).50) - (watermark.Width / 2)); ypos = (int)((sourceImg.Height * (float).99) - watermark.Height); break; case 9: xpos = (int)((sourceImg.Width * (float).99) - (watermark.Width)); ypos = (int)((sourceImg.Height * (float).99) - watermark.Height); break; } g.DrawImage(watermark, new Rectangle(xpos, ypos, watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes); ImageCodecInfo ici = SunImgFormat.GetImageCodecInfo(imgExt); EncoderParameters encoderParams = new EncoderParameters(); long[] qualityParam = new long[1]; if (quality < 0 || quality > 100) quality = 80; qualityParam[0] = quality; EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityParam); encoderParams.Param[0] = encoderParam; MemoryStream newMS = new MemoryStream(); if (ici != null) sourceImg.Save(newMS, ici, encoderParams); else sourceImg.Save(newMS,tFormat); watermarkImg.Dispose();//释放资源 sourceImgMS.Dispose();//释放资源 g.Dispose(); sourceImg.Dispose(); watermark.Dispose(); imageAttributes.Dispose(); return newMS; }
/// <summary> /// 生成图片水印 /// </summary> /// <param name="originalPath">源图路径</param> /// <param name="watermarkPath">水印图片路径</param> /// <param name="targetPath">保存路径</param> /// <param name="position">位置</param> /// <param name="opacity">透明度</param> /// <param name="quality">质量</param> public static void GenerateImageWatermark(string originalPath, string watermarkPath, string targetPath, int position, int opacity, int quality) { Image originalImage = null; Image watermarkImage = null; //图片属性 ImageAttributes attributes = null; //画板 Graphics g = null; try { originalImage = Image.FromFile(originalPath); watermarkImage = new Bitmap(watermarkPath); if (watermarkImage.Height >= originalImage.Height || watermarkImage.Width >= originalImage.Width) { originalImage.Save(targetPath); return; } if (quality < 0 || quality > 100) quality = 80; //水印透明度 float iii; if (opacity > 0 && opacity <= 10) iii = (float)(opacity / 10.0F); else iii = 0.5F; //水印位置 int x = 0; int y = 0; switch (position) { case 1: x = (int)(originalImage.Width * (float).01); y = (int)(originalImage.Height * (float).01); break; case 2: x = (int)((originalImage.Width * (float).50) - (watermarkImage.Width / 2)); y = (int)(originalImage.Height * (float).01); break; case 3: x = (int)((originalImage.Width * (float).99) - (watermarkImage.Width)); y = (int)(originalImage.Height * (float).01); break; case 4: x = (int)(originalImage.Width * (float).01); y = (int)((originalImage.Height * (float).50) - (watermarkImage.Height / 2)); break; case 5: x = (int)((originalImage.Width * (float).50) - (watermarkImage.Width / 2)); y = (int)((originalImage.Height * (float).50) - (watermarkImage.Height / 2)); break; case 6: x = (int)((originalImage.Width * (float).99) - (watermarkImage.Width)); y = (int)((originalImage.Height * (float).50) - (watermarkImage.Height / 2)); break; case 7: x = (int)(originalImage.Width * (float).01); y = (int)((originalImage.Height * (float).99) - watermarkImage.Height); break; case 8: x = (int)((originalImage.Width * (float).50) - (watermarkImage.Width / 2)); y = (int)((originalImage.Height * (float).99) - watermarkImage.Height); break; case 9: x = (int)((originalImage.Width * (float).99) - (watermarkImage.Width)); y = (int)((originalImage.Height * (float).99) - watermarkImage.Height); break; } //颜色映射表 ColorMap colorMap = new ColorMap(); colorMap.OldColor = Color.FromArgb(255, 0, 255, 0); colorMap.NewColor = Color.FromArgb(0, 0, 0, 0); ColorMap[] newColorMap = { colorMap }; //颜色变换矩阵,iii是设置透明度的范围0到1中的单精度类型 float[][] newColorMatrix ={ new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 0.0f, iii, 0.0f}, new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f} }; //定义一个 5 x 5 矩阵 ColorMatrix matrix = new ColorMatrix(newColorMatrix); //图片属性 attributes = new ImageAttributes(); attributes.SetRemapTable(newColorMap, ColorAdjustType.Bitmap); attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); //画板 g = Graphics.FromImage(originalImage); //绘制水印 g.DrawImage(watermarkImage, new Rectangle(x, y, watermarkImage.Width, watermarkImage.Height), 0, 0, watermarkImage.Width, watermarkImage.Height, GraphicsUnit.Pixel, attributes); //保存图片 EncoderParameters encoderParams = new EncoderParameters(); encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, new long[] { quality }); if (GetJPEGCodec() != null) originalImage.Save(targetPath, _jpegcodec, encoderParams); else originalImage.Save(targetPath); } catch (Exception ex) { throw ex; } finally { if (g != null) g.Dispose(); if (attributes != null) attributes.Dispose(); if (watermarkImage != null) watermarkImage.Dispose(); if (originalImage != null) originalImage.Dispose(); } }
/// <summary> /// Draws the button on the specified graphics. /// <seealso cref="Graphics"/> /// </summary> /// <param name="graphics">Button graphics object.</param> protected virtual void DrawButton(Graphics graphics) { // Cleat the background with the Back color graphics.Clear(this.BackColor); // Draw background if(this.Vertical) { DrawVerticalButtonBack(graphics); } else { DrawHorizontalButtonBack(graphics); } // Draw image Rectangle imageRect = Rectangle.Empty; if(this.Image != null) { // Calculate image rectangle position imageRect.X = this.ClientRectangle.X + this.offset; imageRect.Y = this.ClientRectangle.Y + (this.ClientRectangle.Height - this.Image.Height) / 2; imageRect.Width = this.Image.Width; imageRect.Height = this.Image.Height; // Shift image by 1 pixel when in pressed state if(this.pressed && this.Vertical) { ++imageRect.X; ++imageRect.Y; } // Replace transparent color (White) with button back color ColorMap[] myColorMap = new ColorMap[1]; myColorMap[0] = new ColorMap(); myColorMap[0].OldColor = Color.White; myColorMap[0].NewColor = this.BackColor; // Create an ImageAttributes object ImageAttributes imageAttr = new ImageAttributes(); imageAttr.SetRemapTable(myColorMap); // Draw image graphics.DrawImage( this.Image, imageRect, 0, 0, this.Image.Width, this.Image.Height, GraphicsUnit.Pixel, imageAttr); imageAttr.Dispose(); } // Draw button text if(this.Text.Length > 0) { Rectangle textRect = new Rectangle(this.ClientRectangle.Location, this.ClientRectangle.Size); textRect.X += this.offset - 2; textRect.Width -= 2 * this.offset; if(this.Image != null) { textRect.X += this.offset + this.Image.Width; textRect.Width -= this.offset + this.Image.Width; } // Shift image by 1 pixel when in pressed state if(this.pressed && this.Vertical) { ++textRect.X; ++textRect.Y; } StringFormat format = new StringFormat(); format.LineAlignment = StringAlignment.Center; format.Alignment = StringAlignment.Center; format.Trimming = StringTrimming.EllipsisCharacter; format.FormatFlags = StringFormatFlags.LineLimit; using( SolidBrush brush = new SolidBrush( (this.SelectedTab) ? textColorSelected : textColorUnSelected ) ) { graphics.DrawString(this.Text, this.Font, brush, textRect, format); } format.Dispose(); } }
/// <summary> /// Raises the RenderItemImage event. /// </summary> /// <param name="e">An ToolStripItemImageRenderEventArgs containing the event data.</param> protected override void OnRenderItemImage(ToolStripItemImageRenderEventArgs e) { // Is this a min/restore/close pendant button if (e.Item.GetType().ToString() == "System.Windows.Forms.MdiControlStrip+ControlBoxMenuItem") { // Get access to the owning form of the mdi control strip Form f = e.ToolStrip.Parent.TopLevelControl as Form; if (f != null) { // Get the mdi control strip instance PropertyInfo piMCS = typeof(Form).GetProperty("MdiControlStrip", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField); if (piMCS != null) { object mcs = piMCS.GetValue(f, null); if (mcs != null) { // Get the min/restore/close internal menu items Type mcsType = mcs.GetType(); FieldInfo fiM = mcsType.GetField("minimize", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField); FieldInfo fiR = mcsType.GetField("restore", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField); FieldInfo fiC = mcsType.GetField("close", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField); if ((fiM != null) && (fiR != null) && (fiC != null)) { ToolStripMenuItem m = fiM.GetValue(mcs) as ToolStripMenuItem; ToolStripMenuItem r = fiR.GetValue(mcs) as ToolStripMenuItem; ToolStripMenuItem c = fiC.GetValue(mcs) as ToolStripMenuItem; if ((m != null) && (r != null) && (c != null)) { // Compare the event provided image with the internal cached ones to discover the type of pendant button we are drawing PaletteButtonSpecStyle specStyle = PaletteButtonSpecStyle.Generic; if (m.Image == e.Image) specStyle = PaletteButtonSpecStyle.PendantMin; else if (r.Image == e.Image) specStyle = PaletteButtonSpecStyle.PendantRestore; else if (c.Image == e.Image) specStyle = PaletteButtonSpecStyle.PendantClose; // A match, means we have a known pendant button if (specStyle != PaletteButtonSpecStyle.Generic) { // Grab the palette pendant details needed for drawing Image paletteImage = KCT.Palette.GetButtonSpecImage(specStyle, PaletteState.Normal); Color transparentColor = KCT.Palette.GetButtonSpecImageTransparentColor(specStyle); // Finally we actually have an image to draw! if (paletteImage != null) { using (ImageAttributes attribs = new ImageAttributes()) { // Setup mapping to make required color transparent ColorMap remap = new ColorMap(); remap.OldColor = transparentColor; remap.NewColor = Color.Transparent; attribs.SetRemapTable(new ColorMap[] { remap }); // Phew, actually draw the darn thing e.Graphics.DrawImage(paletteImage, e.ImageRectangle, 0, 0, e.Image.Width, e.Image.Height, GraphicsUnit.Pixel, attribs); // Do not let base class draw system defined image return; } } } } } } } } } base.OnRenderItemImage(e); }
/// <summary> /// 生成水印图 /// </summary> /// <param name="sourceFilename">源图绝对路径</param> /// <param name="watermarkType">水印类型:文字水印,图片水印</param> /// <param name="watermarkPosition">水印位置</param> /// <param name="watermarkImage">水印图绝对路径</param> /// <param name="watermarkText">水印文字</param> /// <param name="fontSize">字体大小</param> /// <param name="fontColor">字体颜色</param> /// <param name="fontFamily">字体</param> /// <param name="alpha">透明度</param> /// <returns></returns> public static string AddWatermark(string sourceFilename, string watermarkType, int watermarkPosition, string watermarkImage , string watermarkText, int fontSize, string fontColor, string fontFamily, float alpha) { System.Drawing.Image img = System.Drawing.Image.FromFile(sourceFilename); // 封装 GDI+ 位图,此位图由图形图像及其属性的像素数据组成。 Bitmap bmPhoto = new Bitmap(img.Width, img.Height, PixelFormat.Format32bppRgb); // 设定分辨率 bmPhoto.SetResolution(72, 72); System.Drawing.Graphics g = Graphics.FromImage(bmPhoto);//System.Drawing.Graphics.FromImage(img); //设置高质量插值法 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //消除锯齿 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; //g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel); //文件扩展名 string strExt = Path.GetExtension(sourceFilename); //生成的水印图文件名 string strWatermarkFile = sourceFilename.Replace(strExt, "_watermark" + strExt); System.Drawing.Imaging.ImageAttributes imageAttributes = new System.Drawing.Imaging.ImageAttributes(); System.Drawing.Imaging.ColorMap colorMap = new System.Drawing.Imaging.ColorMap(); colorMap.OldColor = System.Drawing.Color.FromArgb(255, 0, 255, 0); colorMap.NewColor = System.Drawing.Color.FromArgb(0, 0, 0, 0); System.Drawing.Imaging.ColorMap[] remapTable = { colorMap }; imageAttributes.SetRemapTable(remapTable, System.Drawing.Imaging.ColorAdjustType.Bitmap); float[][] colorMatrixElements = { new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f }, new float[] { 0.0f, 1.0f, 0.0f, 0.0f, 0.0f }, new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f }, new float[] { 0.0f, 0.0f, 0.0f, alpha, 0.0f }, //水印透明度 new float[] { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f } }; System.Drawing.Imaging.ColorMatrix colorMatrix = new System.Drawing.Imaging.ColorMatrix(colorMatrixElements); imageAttributes.SetColorMatrix(colorMatrix, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap); //水印所在位置 int xpos = 0; int ypos = 0; int intWatermarkWidth = 0; int intWatermarkHeight = 0; System.Drawing.Image watermark = null; if (watermarkType.Equals("图片水印") && File.Exists(watermarkImage)) { //加载水印图片 watermark = new System.Drawing.Bitmap(watermarkImage); intWatermarkWidth = watermark.Width; intWatermarkHeight = watermark.Height; } else if (watermarkType.Equals("文字水印") && watermarkText.Trim().Length > 0) { SizeF size = g.MeasureString(watermarkText, new Font(new FontFamily(fontFamily), fontSize)); intWatermarkWidth = Convert.ToInt32(size.Width); intWatermarkHeight = Convert.ToInt32(size.Height); } switch (watermarkPosition) { case 1: xpos = (int)(img.Width * (float).01); ypos = (int)(img.Height * (float).01); break; case 2: xpos = (int)((img.Width * (float).50) - (intWatermarkWidth / 2)); ypos = (int)(img.Height * (float).01); break; case 3: xpos = (int)((img.Width * (float).99) - (intWatermarkWidth)); ypos = (int)(img.Height * (float).01); break; case 4: xpos = (int)(img.Width * (float).01); ypos = (int)((img.Height * (float).50) - (intWatermarkHeight / 2)); break; case 5: xpos = (int)((img.Width * (float).50) - (intWatermarkWidth / 2)); ypos = (int)((img.Height * (float).50) - (intWatermarkHeight / 2)); break; case 6: xpos = (int)((img.Width * (float).99) - (intWatermarkWidth)); ypos = (int)((img.Height * (float).50) - (intWatermarkHeight / 2)); break; case 7: xpos = (int)(img.Width * (float).01); ypos = (int)((img.Height * (float).99) - intWatermarkHeight); break; case 8: xpos = (int)((img.Width * (float).50) - (intWatermarkWidth / 2)); ypos = (int)((img.Height * (float).99) - intWatermarkHeight); break; case 9: xpos = (int)((img.Width * (float).99) - (intWatermarkWidth)); ypos = (int)((img.Height * (float).99) - intWatermarkHeight); break; } if (watermark != null) { g.DrawImage(watermark, new System.Drawing.Rectangle(xpos, ypos, intWatermarkWidth, intWatermarkHeight), 0, 0, intWatermarkWidth, intWatermarkHeight, System.Drawing.GraphicsUnit.Pixel, imageAttributes); } else { System.Drawing.Font font = new System.Drawing.Font(fontFamily, fontSize); //文字字体 Color fColor = System.Drawing.ColorTranslator.FromHtml(fontColor); Color txtColor = System.Drawing.Color.FromArgb(Convert.ToInt32(alpha * 255), fColor); //文字颜色 System.Drawing.SolidBrush brush = new System.Drawing.SolidBrush(txtColor); g.DrawString(watermarkText, font, brush, xpos, ypos); } System.Drawing.Imaging.ImageCodecInfo[] codecs = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders(); System.Drawing.Imaging.ImageCodecInfo ici = null; foreach (System.Drawing.Imaging.ImageCodecInfo codec in codecs) { if (codec.MimeType.IndexOf("jpeg") > -1) { ici = codec; } } System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters(); long[] qualityParam = new long[1]; qualityParam[0] = 80; //图片质量 System.Drawing.Imaging.EncoderParameter encoderParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityParam); encoderParams.Param[0] = encoderParam; if (ici != null) { bmPhoto.Save(strWatermarkFile, ici, encoderParams); } else { bmPhoto.Save(strWatermarkFile); } g.Dispose(); img.Dispose(); if (watermark != null) { watermark.Dispose(); } imageAttributes.Dispose(); return(strWatermarkFile); }
public System.IO.MemoryStream AddImageSignPic(System.Drawing.Image img, string watermarkFilename, int watermarkStatus, int quality, int watermarkTransparency) { System.Drawing.Graphics graphics = null; System.Drawing.Image image = null; System.Drawing.Imaging.ImageAttributes imageAttributes = null; System.IO.MemoryStream result; try { graphics = System.Drawing.Graphics.FromImage(img); image = new System.Drawing.Bitmap(watermarkFilename); imageAttributes = new System.Drawing.Imaging.ImageAttributes(); System.Drawing.Imaging.ColorMap[] map = new System.Drawing.Imaging.ColorMap[] { new System.Drawing.Imaging.ColorMap { OldColor = System.Drawing.Color.FromArgb(255, 0, 255, 0), NewColor = System.Drawing.Color.FromArgb(0, 0, 0, 0) } }; imageAttributes.SetRemapTable(map, System.Drawing.Imaging.ColorAdjustType.Bitmap); float num = 0.5f; if (watermarkTransparency >= 1 && watermarkTransparency <= 10) { num = (float)watermarkTransparency / 10f; } float[][] array = new float[5][]; float[][] arg_A1_0 = array; int arg_A1_1 = 0; float[] array2 = new float[5]; array2[0] = 1f; arg_A1_0[arg_A1_1] = array2; float[][] arg_B8_0 = array; int arg_B8_1 = 1; float[] array3 = new float[5]; array3[1] = 1f; arg_B8_0[arg_B8_1] = array3; float[][] arg_CF_0 = array; int arg_CF_1 = 2; float[] array4 = new float[5]; array4[2] = 1f; arg_CF_0[arg_CF_1] = array4; float[][] arg_E3_0 = array; int arg_E3_1 = 3; float[] array5 = new float[5]; array5[3] = num; arg_E3_0[arg_E3_1] = array5; array[4] = new float[] { 0f, 0f, 0f, 0f, 1f }; float[][] newColorMatrix = array; System.Drawing.Imaging.ColorMatrix newColorMatrix2 = new System.Drawing.Imaging.ColorMatrix(newColorMatrix); imageAttributes.SetColorMatrix(newColorMatrix2, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap); int x = 0; int y = 0; switch (watermarkStatus) { case 1: x = (int)((float)img.Width * 0.01f); y = (int)((float)img.Height * 0.01f); break; case 2: x = (int)((float)img.Width * 0.5f - (float)(image.Width / 2)); y = (int)((float)img.Height * 0.01f); break; case 3: x = (int)((float)img.Width * 0.99f - (float)image.Width); y = (int)((float)img.Height * 0.01f); break; case 4: x = (int)((float)img.Width * 0.01f); y = (int)((float)img.Height * 0.5f - (float)(image.Height / 2)); break; case 5: x = (int)((float)img.Width * 0.5f - (float)(image.Width / 2)); y = (int)((float)img.Height * 0.5f - (float)(image.Height / 2)); break; case 6: x = (int)((float)img.Width * 0.99f - (float)image.Width); y = (int)((float)img.Height * 0.5f - (float)(image.Height / 2)); break; case 7: x = (int)((float)img.Width * 0.01f); y = (int)((float)img.Height * 0.99f - (float)image.Height); break; case 8: x = (int)((float)img.Width * 0.5f - (float)(image.Width / 2)); y = (int)((float)img.Height * 0.99f - (float)image.Height); break; case 9: x = (int)((float)img.Width * 0.99f - (float)image.Width); y = (int)((float)img.Height * 0.99f - (float)image.Height); break; } graphics.DrawImage(image, new System.Drawing.Rectangle(x, y, image.Width, image.Height), 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel, imageAttributes); System.Drawing.Imaging.ImageCodecInfo[] imageEncoders = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders(); System.Drawing.Imaging.ImageCodecInfo imageCodecInfo = null; System.Drawing.Imaging.ImageCodecInfo[] array6 = imageEncoders; for (int i = 0; i < array6.Length; i++) { System.Drawing.Imaging.ImageCodecInfo imageCodecInfo2 = array6[i]; if (imageCodecInfo2.MimeType.IndexOf("jpeg") > -1) { imageCodecInfo = imageCodecInfo2; } } System.Drawing.Imaging.EncoderParameters encoderParameters = new System.Drawing.Imaging.EncoderParameters(); long[] array7 = new long[1]; if (quality < 0 || quality > 100) { quality = 80; } array7[0] = (long)quality; System.Drawing.Imaging.EncoderParameter encoderParameter = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, array7); encoderParameters.Param[0] = encoderParameter; System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(); if (imageCodecInfo != null) { img.Save(memoryStream, imageCodecInfo, encoderParameters); } result = memoryStream; } catch { System.IO.MemoryStream memoryStream = null; result = memoryStream; } finally { if (graphics != null) { graphics.Dispose(); } if (img != null) { img.Dispose(); } if (image != null) { image.Dispose(); } if (imageAttributes != null) { imageAttributes.Dispose(); } } return(result); }
public static void GenerateImageWatermark(string originalPath, string watermarkPath, string targetPath, int position, int opacity, int quality) { System.Drawing.Image image = null; System.Drawing.Image image2 = null; System.Drawing.Imaging.ImageAttributes imageAttributes = null; System.Drawing.Graphics graphics = null; try { image = System.Drawing.Image.FromFile(originalPath); image2 = new System.Drawing.Bitmap(watermarkPath); if (image2.Height >= image.Height || image2.Width >= image.Width) { image.Save(targetPath); } else { if (quality < 0 || quality > 100) { quality = 80; } float num; if (opacity > 0 && opacity <= 10) { num = (float)opacity / 10f; } else { num = 0.5f; } int x = 0; int y = 0; switch (position) { case 1: x = (int)((float)image.Width * 0.01f); y = (int)((float)image.Height * 0.01f); break; case 2: x = (int)((float)image.Width * 0.5f - (float)(image2.Width / 2)); y = (int)((float)image.Height * 0.01f); break; case 3: x = (int)((float)image.Width * 0.99f - (float)image2.Width); y = (int)((float)image.Height * 0.01f); break; case 4: x = (int)((float)image.Width * 0.01f); y = (int)((float)image.Height * 0.5f - (float)(image2.Height / 2)); break; case 5: x = (int)((float)image.Width * 0.5f - (float)(image2.Width / 2)); y = (int)((float)image.Height * 0.5f - (float)(image2.Height / 2)); break; case 6: x = (int)((float)image.Width * 0.99f - (float)image2.Width); y = (int)((float)image.Height * 0.5f - (float)(image2.Height / 2)); break; case 7: x = (int)((float)image.Width * 0.01f); y = (int)((float)image.Height * 0.99f - (float)image2.Height); break; case 8: x = (int)((float)image.Width * 0.5f - (float)(image2.Width / 2)); y = (int)((float)image.Height * 0.99f - (float)image2.Height); break; case 9: x = (int)((float)image.Width * 0.99f - (float)image2.Width); y = (int)((float)image.Height * 0.99f - (float)image2.Height); break; } System.Drawing.Imaging.ColorMap[] map = new System.Drawing.Imaging.ColorMap[] { new System.Drawing.Imaging.ColorMap { OldColor = System.Drawing.Color.FromArgb(255, 0, 255, 0), NewColor = System.Drawing.Color.FromArgb(0, 0, 0, 0) } }; float[][] array = new float[5][]; float[][] arg_2DE_0 = array; int arg_2DE_1 = 0; float[] array2 = new float[5]; array2[0] = 1f; arg_2DE_0[arg_2DE_1] = array2; float[][] arg_2F5_0 = array; int arg_2F5_1 = 1; array2 = new float[5]; array2[1] = 1f; arg_2F5_0[arg_2F5_1] = array2; float[][] arg_30C_0 = array; int arg_30C_1 = 2; array2 = new float[5]; array2[2] = 1f; arg_30C_0[arg_30C_1] = array2; float[][] arg_320_0 = array; int arg_320_1 = 3; array2 = new float[5]; array2[3] = num; arg_320_0[arg_320_1] = array2; array[4] = new float[] { 0f, 0f, 0f, 0f, 1f }; float[][] newColorMatrix = array; System.Drawing.Imaging.ColorMatrix newColorMatrix2 = new System.Drawing.Imaging.ColorMatrix(newColorMatrix); imageAttributes = new System.Drawing.Imaging.ImageAttributes(); imageAttributes.SetRemapTable(map, System.Drawing.Imaging.ColorAdjustType.Bitmap); imageAttributes.SetColorMatrix(newColorMatrix2, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap); graphics = System.Drawing.Graphics.FromImage(image); graphics.DrawImage(image2, new System.Drawing.Rectangle(x, y, image2.Width, image2.Height), 0, 0, image2.Width, image2.Height, System.Drawing.GraphicsUnit.Pixel, imageAttributes); System.Drawing.Imaging.EncoderParameters encoderParameters = new System.Drawing.Imaging.EncoderParameters(); encoderParameters.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, new long[] { (long)quality }); if (ImageHelper.GetJPEGCodec() != null) { image.Save(targetPath, ImageHelper._jpegcodec, encoderParameters); } else { image.Save(targetPath); } } } catch (System.Exception ex) { throw ex; } finally { if (graphics != null) { graphics.Dispose(); } if (imageAttributes != null) { imageAttributes.Dispose(); } if (image2 != null) { image2.Dispose(); } if (image != null) { image.Dispose(); } } }
private void DrawImage(Graphics g) { Image image = this.Enabled ? ImageEnabled : ((ImageDisabled != null) ? ImageDisabled : ImageEnabled); ImageAttributes imageAttr = null; if (null == image) return; if (m_monochrom) { imageAttr = new ImageAttributes(); // transform the monochrom image // white -> BackColor // black -> ForeColor ColorMap[] colorMap = new ColorMap[2]; colorMap[0] = new ColorMap(); colorMap[0].OldColor = Color.White; colorMap[0].NewColor = this.BackColor; colorMap[1] = new ColorMap(); colorMap[1].OldColor = Color.Black; colorMap[1].NewColor = this.ForeColor; imageAttr.SetRemapTable(colorMap); } Rectangle rect = new Rectangle(0, 0, image.Width, image.Height); if ((!Enabled) && (null == ImageDisabled)) { using (Bitmap bitmapMono = new Bitmap(image, ClientRectangle.Size)) { if (imageAttr != null) { using (Graphics gMono = Graphics.FromImage(bitmapMono)) { gMono.DrawImage(image, new Point[3] { new Point(0, 0), new Point(image.Width - 1, 0), new Point(0, image.Height - 1) }, rect, GraphicsUnit.Pixel, imageAttr); } } ControlPaint.DrawImageDisabled(g, bitmapMono, 0, 0, this.BackColor); } } else { // Three points provided are upper-left, upper-right and // lower-left of the destination parallelogram. Point[] pts = new Point[3]; pts[0].X = (Enabled && m_mouseOver && m_mouseCapture) ? 1 : 0; pts[0].Y = (Enabled && m_mouseOver && m_mouseCapture) ? 1 : 0; pts[1].X = pts[0].X + ClientRectangle.Width; pts[1].Y = pts[0].Y; pts[2].X = pts[0].X; pts[2].Y = pts[1].Y + ClientRectangle.Height; if (imageAttr == null) g.DrawImage(image, pts, rect, GraphicsUnit.Pixel); else g.DrawImage(image, pts, rect, GraphicsUnit.Pixel, imageAttr); } }
/// <summary> /// Helper routine to draw an image taking into account various properties. /// </summary> /// <param name="context">Rendering context.</param> /// <param name="image">Image to be drawn.</param> /// <param name="remapTransparent">Color that should become transparent.</param> /// <param name="imageRect">Destination rectangle.</param> /// <param name="orientation">Visual orientation.</param> /// <param name="effect">Drawing effect.</param> /// <param name="remapColor">Image color to remap.</param> /// <param name="remapNew">New color for remap.</param> protected static void DrawImageHelper(ViewContext context, Image image, Color remapTransparent, Rectangle imageRect, VisualOrientation orientation, PaletteImageEffect effect, Color remapColor, Color remapNew) { Debug.Assert(context != null); // Prevent problems with multiple threads using the same palette images // by only allowing a single thread to draw the provided image at a time lock (_threadLock) { // Validate reference parameter if (context == null) throw new ArgumentNullException("context"); // Use image attributes class to modify image drawing for effects ImageAttributes attribs = new ImageAttributes(); switch (effect) { case PaletteImageEffect.Disabled: attribs.SetColorMatrix(CommonHelper.MatrixDisabled); break; case PaletteImageEffect.GrayScale: attribs.SetColorMatrix(_matrixGrayScale, ColorMatrixFlag.SkipGrays); break; case PaletteImageEffect.GrayScaleRed: attribs.SetColorMatrix(_matrixGrayScaleRed, ColorMatrixFlag.SkipGrays); break; case PaletteImageEffect.GrayScaleGreen: attribs.SetColorMatrix(_matrixGrayScaleGreen, ColorMatrixFlag.SkipGrays); break; case PaletteImageEffect.GrayScaleBlue: attribs.SetColorMatrix(_matrixGrayScaleBlue, ColorMatrixFlag.SkipGrays); break; case PaletteImageEffect.Light: attribs.SetColorMatrix(_matrixLight); break; case PaletteImageEffect.LightLight: attribs.SetColorMatrix(_matrixLightLight); break; case PaletteImageEffect.Dark: attribs.SetColorMatrix(_matrixDark); break; case PaletteImageEffect.DarkDark: attribs.SetColorMatrix(_matrixDarkDark); break; case PaletteImageEffect.Inherit: // Should never happen! Debug.Assert(false); break; } // Do we need to remap a colors in the bitmap? if ((remapTransparent != Color.Empty) || ((remapColor != Color.Empty) && (remapNew != Color.Empty))) { List<ColorMap> colorMaps = new List<ColorMap>(); // Create remapping for the transparent color if (remapTransparent != Color.Empty) { ColorMap remap = new ColorMap(); remap.OldColor = remapTransparent; remap.NewColor = Color.Transparent; colorMaps.Add(remap); } // Create remapping from source to target colors if ((remapColor != Color.Empty) && (remapNew != Color.Empty)) { ColorMap remap = new ColorMap(); remap.OldColor = remapColor; remap.NewColor = remapNew; colorMaps.Add(remap); } attribs.SetRemapTable(colorMaps.ToArray(), ColorAdjustType.Bitmap); } int translateX = 0; int translateY = 0; float rotation = 0f; // Perform any transformations needed for orientation switch (orientation) { case VisualOrientation.Bottom: // Translate to opposite side of origin, so the rotate can // then bring it back to original position but mirror image translateX = imageRect.X * 2 + imageRect.Width; translateY = imageRect.Y * 2 + imageRect.Height; rotation = 180f; break; case VisualOrientation.Left: // Invert the dimensions of the rectangle for drawing upwards imageRect = new Rectangle(imageRect.X, imageRect.Y, imageRect.Height, imageRect.Width); // Translate back from a quarter left turn to the original place translateX = imageRect.X - imageRect.Y; translateY = imageRect.X + imageRect.Y + imageRect.Width; rotation = -90f; break; case VisualOrientation.Right: // Invert the dimensions of the rectangle for drawing upwards imageRect = new Rectangle(imageRect.X, imageRect.Y, imageRect.Height, imageRect.Width); // Translate back from a quarter right turn to the original place translateX = imageRect.X + imageRect.Y + imageRect.Height; translateY = -(imageRect.X - imageRect.Y); rotation = 90f; break; } // Apply the transforms if we have any to apply if ((translateX != 0) || (translateY != 0)) context.Graphics.TranslateTransform(translateX, translateY); if (rotation != 0f) context.Graphics.RotateTransform(rotation); try { // Finally, just draw the image and let the transforms do the rest context.Graphics.DrawImage(image, imageRect, 0, 0, imageRect.Width, imageRect.Height, GraphicsUnit.Pixel, attribs); } catch (ArgumentException) { } finally { if (rotation != 0f) context.Graphics.RotateTransform(-rotation); // Remove the applied transforms if ((translateX != 0) | (translateY != 0)) context.Graphics.TranslateTransform(-translateX, -translateY); } } }
/// <summary> /// 加图片水印 /// </summary> /// <param name="filename">文件名</param> /// <param name="watermarkFilename">水印文件名</param> /// <param name="watermarkStatus">图片水印位置:0=不使用 1=左上 2=中上 3=右上 4=左中 ... 9=右下</param> /// <param name="quality">是否是高质量图片 取值范围0--100</param> /// <param name="watermarkTransparency">图片水印透明度 取值范围1--10 (10为不透明)</param> public static void AddImageSignPic(string Path, string filename, string watermarkFilename, int watermarkStatus, int quality, int watermarkTransparency) { System.Drawing.Image img = System.Drawing.Image.FromFile(Path); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(img); //设置高质量插值法 //g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度 //g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; System.Drawing.Image watermark = new System.Drawing.Bitmap(watermarkFilename); if (watermark.Height >= img.Height || watermark.Width >= img.Width) { return; } System.Drawing.Imaging.ImageAttributes imageAttributes = new System.Drawing.Imaging.ImageAttributes(); System.Drawing.Imaging.ColorMap colorMap = new System.Drawing.Imaging.ColorMap(); colorMap.OldColor = System.Drawing.Color.FromArgb(255, 0, 255, 0); colorMap.NewColor = System.Drawing.Color.FromArgb(0, 0, 0, 0); System.Drawing.Imaging.ColorMap[] remapTable = { colorMap }; imageAttributes.SetRemapTable(remapTable, System.Drawing.Imaging.ColorAdjustType.Bitmap); float transparency = 0.5F; if (watermarkTransparency >= 1 && watermarkTransparency <= 10) { transparency = (watermarkTransparency / 10.0F); } float[][] colorMatrixElements = { new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f }, new float[] { 0.0f, 1.0f, 0.0f, 0.0f, 0.0f }, new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f }, new float[] { 0.0f, 0.0f, 0.0f, transparency, 0.0f }, new float[] { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f } }; System.Drawing.Imaging.ColorMatrix colorMatrix = new System.Drawing.Imaging.ColorMatrix(colorMatrixElements); imageAttributes.SetColorMatrix(colorMatrix, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap); int xpos = 0; int ypos = 0; switch (watermarkStatus) { case 1: xpos = (int)(img.Width * (float).01); ypos = (int)(img.Height * (float).01); break; case 2: xpos = (int)((img.Width * (float).50) - (watermark.Width / 2)); ypos = (int)(img.Height * (float).01); break; case 3: xpos = (int)((img.Width * (float).99) - (watermark.Width)); ypos = (int)(img.Height * (float).01); break; case 4: xpos = (int)(img.Width * (float).01); ypos = (int)((img.Height * (float).50) - (watermark.Height / 2)); break; case 5: xpos = (int)((img.Width * (float).50) - (watermark.Width / 2)); ypos = (int)((img.Height * (float).50) - (watermark.Height / 2)); break; case 6: xpos = (int)((img.Width * (float).99) - (watermark.Width)); ypos = (int)((img.Height * (float).50) - (watermark.Height / 2)); break; case 7: xpos = (int)(img.Width * (float).01); ypos = (int)((img.Height * (float).99) - watermark.Height); break; case 8: xpos = (int)((img.Width * (float).50) - (watermark.Width / 2)); ypos = (int)((img.Height * (float).99) - watermark.Height); break; case 9: xpos = (int)((img.Width * (float).99) - (watermark.Width)); ypos = (int)((img.Height * (float).99) - watermark.Height); break; } g.DrawImage(watermark, new System.Drawing.Rectangle(xpos, ypos, watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, System.Drawing.GraphicsUnit.Pixel, imageAttributes); System.Drawing.Imaging.ImageCodecInfo[] codecs = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders(); System.Drawing.Imaging.ImageCodecInfo ici = null; foreach (System.Drawing.Imaging.ImageCodecInfo codec in codecs) { //if (codec.MimeType.IndexOf("jpeg") > -1) if (codec.MimeType.Contains("jpeg")) { ici = codec; } } System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters(); long[] qualityParam = new long[1]; if (quality < 0 || quality > 100) { quality = 80; } qualityParam[0] = quality; System.Drawing.Imaging.EncoderParameter encoderParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityParam); encoderParams.Param[0] = encoderParam; if (ici != null) { img.Save(filename, ici, encoderParams); } else { img.Save(filename); } g.Dispose(); img.Dispose(); watermark.Dispose(); imageAttributes.Dispose(); }