/// <summary> /// Set brightness for the image /// </summary> /// <param name="image">input bitmap</param> /// <param name="value">value from -255 to 255</param> /// <returns></returns> public static Bitmap SetBrightness(Bitmap image, int value) { var tempBitmap = image; var finalValue = value / 255.0f; var newBitmap = new Bitmap(tempBitmap.Width, tempBitmap.Height); var newGraphics = Graphics.FromImage(newBitmap); float[][] floatColorMatrix = { new float[] { 1, 0, 0, 0, 0 }, new float[] { 0, 1, 0, 0, 0 }, new float[] { 0, 0, 1, 0, 0 }, new float[] { 0, 0, 0, 1, 0 }, new[] { finalValue, finalValue, finalValue, 1, 1 } }; var newColorMatrix = new System.Drawing.Imaging.ColorMatrix(floatColorMatrix); var attributes = new System.Drawing.Imaging.ImageAttributes(); attributes.SetColorMatrix(newColorMatrix); newGraphics.DrawImage(tempBitmap, new System.Drawing.Rectangle(0, 0, tempBitmap.Width, tempBitmap.Height), 0, 0, tempBitmap.Width, tempBitmap.Height, GraphicsUnit.Pixel, attributes); attributes.Dispose(); newGraphics.Dispose(); return(newBitmap); }
private static Bitmap Parlaklik(Bitmap bmp, int deger) { System.Drawing.Bitmap TempBitmap = bmp; float finalValue = (float)deger / 255.0f; System.Drawing.Bitmap NewBitmap = new System.Drawing.Bitmap(TempBitmap.Width, TempBitmap.Height); System.Drawing.Graphics NewGraphics = System.Drawing.Graphics.FromImage(NewBitmap); float[][] FloatColorMatrix = { new float[] { 1, 0, 0, 0, 0 }, new float[] { 0, 1, 0, 0, 0 }, new float[] { 0, 0, 1, 0, 0 }, new float[] { 0, 0, 0, 1, 0 }, new float[] { finalValue, finalValue, finalValue, 1, 1 }, }; System.Drawing.Imaging.ColorMatrix NewColorMatrix = new System.Drawing.Imaging.ColorMatrix(FloatColorMatrix); System.Drawing.Imaging.ImageAttributes Attributes = new System.Drawing.Imaging.ImageAttributes(); Attributes.SetColorMatrix(NewColorMatrix); NewGraphics.DrawImage(TempBitmap, new System.Drawing.Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), 0, 0, TempBitmap.Width, TempBitmap.Height, System.Drawing.GraphicsUnit.Pixel, Attributes); Attributes.Dispose(); NewGraphics.Dispose(); return(NewBitmap); }
public static Bitmap AdjustBrightness(this Image Image, Color contrastMultiplier, Color brightnessBonus) { var TempBitmap = Image; var NewBitmap = new Bitmap(TempBitmap.Width, TempBitmap.Height); var NewGraphics = Graphics.FromImage(NewBitmap); float[][] FloatColorMatrix = { new[] { contrastMultiplier.R/255.0f, 0, 0, 0, 0 }, new[] { 0, contrastMultiplier.G/255.0f, 0, 0, 0 }, new[] { 0, 0, contrastMultiplier.B/255.0f, 0, 0 }, new float[] { 0, 0, 0, 1, 0 }, new[] { brightnessBonus.R/255.0f, brightnessBonus.G/255.0f, brightnessBonus.B/255.0f, 1, 1 } }; var NewColorMatrix = new ColorMatrix(FloatColorMatrix); var Attributes = new ImageAttributes(); Attributes.SetColorMatrix(NewColorMatrix); NewGraphics.DrawImage(TempBitmap, new Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), 0, 0, TempBitmap.Width, TempBitmap.Height, GraphicsUnit.Pixel, Attributes); Attributes.Dispose(); NewGraphics.Dispose(); return NewBitmap; }
public static void DrawBackgroundImage(Graphics g, Image backgroundImage, Color backColor, ImageLayout backgroundImageLayout, Rectangle bounds, Rectangle clipRect, Point scrollOffset, RightToLeft rightToLeft) { if (g == null) { throw new ArgumentNullException("g"); } if (backgroundImageLayout == ImageLayout.Tile) { using (TextureBrush brush = new TextureBrush(backgroundImage, WrapMode.Tile)) { if (scrollOffset != Point.Empty) { Matrix transform = brush.Transform; transform.Translate((float) scrollOffset.X, (float) scrollOffset.Y); brush.Transform = transform; } g.FillRectangle(brush, clipRect); return; } } Rectangle rect = CalculateBackgroundImageRectangle(bounds, backgroundImage, backgroundImageLayout); if ((rightToLeft == RightToLeft.Yes) && (backgroundImageLayout == ImageLayout.None)) { rect.X += clipRect.Width - rect.Width; } using (SolidBrush brush2 = new SolidBrush(backColor)) { g.FillRectangle(brush2, clipRect); } if (!clipRect.Contains(rect)) { if ((backgroundImageLayout == ImageLayout.Stretch) || (backgroundImageLayout == ImageLayout.Zoom)) { rect.Intersect(clipRect); g.DrawImage(backgroundImage, rect); } else if (backgroundImageLayout == ImageLayout.None) { rect.Offset(clipRect.Location); Rectangle destRect = rect; destRect.Intersect(clipRect); Rectangle rectangle3 = new Rectangle(Point.Empty, destRect.Size); g.DrawImage(backgroundImage, destRect, rectangle3.X, rectangle3.Y, rectangle3.Width, rectangle3.Height, GraphicsUnit.Pixel); } else { Rectangle rectangle4 = rect; rectangle4.Intersect(clipRect); Rectangle rectangle5 = new Rectangle(new Point(rectangle4.X - rect.X, rectangle4.Y - rect.Y), rectangle4.Size); g.DrawImage(backgroundImage, rectangle4, rectangle5.X, rectangle5.Y, rectangle5.Width, rectangle5.Height, GraphicsUnit.Pixel); } } else { ImageAttributes imageAttr = new ImageAttributes(); imageAttr.SetWrapMode(WrapMode.TileFlipXY); g.DrawImage(backgroundImage, rect, 0, 0, backgroundImage.Width, backgroundImage.Height, GraphicsUnit.Pixel, imageAttr); imageAttr.Dispose(); } }
//this function change the brightness of the image public static Bitmap AdjustBrightness(Bitmap Image, int Value) { System.Drawing.Bitmap TempBitmap = Image; float FinalValue = (float)Value / 255.0f; //calculate the scaling value System.Drawing.Bitmap NewBitmap = new System.Drawing.Bitmap(TempBitmap.Width, TempBitmap.Height); System.Drawing.Graphics NewGraphics = System.Drawing.Graphics.FromImage(NewBitmap); float[][] FloatColorMatrix = { new float[] { 1, 0, 0, 0, 0 }, new float[] { 0, 1, 0, 0, 0 }, new float[] { 0, 0, 1, 0, 0 }, new float[] { 0, 0, 0, 1, 0 }, new float[] { FinalValue, FinalValue, FinalValue, 1, 1 } }; //create the matrix for brightness calculation System.Drawing.Imaging.ColorMatrix NewColorMatrix = new System.Drawing.Imaging.ColorMatrix(FloatColorMatrix); System.Drawing.Imaging.ImageAttributes Attributes = new System.Drawing.Imaging.ImageAttributes(); Attributes.SetColorMatrix(NewColorMatrix); //redraw the image NewGraphics.DrawImage(TempBitmap, new System.Drawing.Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), 0, 0, TempBitmap.Width, TempBitmap.Height, System.Drawing.GraphicsUnit.Pixel, Attributes); Attributes.Dispose(); NewGraphics.Dispose(); return(NewBitmap); }
/// <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()); } }
public static Image Set(Image image, float opacity) { Bitmap bmp = new Bitmap(image.Width, image.Height); Graphics gfx = Graphics.FromImage(bmp); ColorMatrix cmx = new ColorMatrix(); cmx.Matrix33 = opacity; ImageAttributes ia = new ImageAttributes(); ia.SetColorMatrix(cmx, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); gfx.DrawImage(image, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, ia); ia.Dispose(); gfx.Dispose(); return bmp; }
private static void ApplyColorMatrix(Bitmap b , ref ColorMatrix matrix) { ImageAttributes ImgAtt = new ImageAttributes(); //Bitmap bmpMatrix=new Bitmap(b.Width, b.Height); Graphics grMatrix = Graphics.FromImage(b); ImgAtt.SetColorMatrix(matrix); grMatrix.DrawImage(b, new Rectangle(0, 0, b.Width, b.Height), 0, 0, b.Width, b.Height, GraphicsUnit.Pixel, ImgAtt); //b = bmpMatrix; grMatrix.Dispose(); ImgAtt.Dispose(); }
/// <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); } }
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 Bitmap AdjustBrightness(Bitmap Image, int Value) { Bitmap TempBitmap = Image; float FinalValue = (float)Value / 255.0f; Bitmap NewBitmap = new Bitmap(TempBitmap.Width, TempBitmap.Height); Graphics NewGraphics = Graphics.FromImage(NewBitmap); float[][] FloatColorMatrix = { new float[] {1, 0, 0, 0, 0}, new float[] {0, 1, 0, 0, 0}, new float[] {0, 0, 1, 0, 0}, new float[] {0, 0, 0, 1, 0}, new float[] {FinalValue, FinalValue, FinalValue, 1, 1} }; ColorMatrix NewColorMatrix = new ColorMatrix(FloatColorMatrix); ImageAttributes Attributes = new ImageAttributes(); Attributes.SetColorMatrix(NewColorMatrix); NewGraphics.DrawImage(TempBitmap, new Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), 0, 0, TempBitmap.Width, TempBitmap.Height, GraphicsUnit.Pixel, Attributes); Attributes.Dispose(); NewGraphics.Dispose(); return NewBitmap; }
public void AdjustBrightness(Bitmap image, int value) { Bitmap previewBitmap = image; if (isFilter == true) { previewBitmap = contrastTemp; } if (isContrast == true) { previewBitmap = brightnessTemp; } else { previewBitmap = temp; } //System.Drawing.Bitmap TempBitmap = image; float FinalValue = (float)value / 255.0f; System.Drawing.Bitmap NewBitmap = new System.Drawing.Bitmap(previewBitmap.Width, previewBitmap.Height); System.Drawing.Graphics NewGraphics = System.Drawing.Graphics.FromImage(NewBitmap); float[][] FloatColorMatrix = { new float[] { 1, 0, 0, 0, 0 }, new float[] { 0, 1, 0, 0, 0 }, new float[] { 0, 0, 1, 0, 0 }, new float[] { 0, 0, 0, 1, 0 }, new float[] { FinalValue, FinalValue, FinalValue, 1, 1 } }; System.Drawing.Imaging.ColorMatrix NewColorMatrix = new System.Drawing.Imaging.ColorMatrix(FloatColorMatrix); System.Drawing.Imaging.ImageAttributes Attributes = new System.Drawing.Imaging.ImageAttributes(); Attributes.SetColorMatrix(NewColorMatrix); NewGraphics.DrawImage(previewBitmap, new System.Drawing.Rectangle(0, 0, previewBitmap.Width, previewBitmap.Height), 0, 0, previewBitmap.Width, previewBitmap.Height, System.Drawing.GraphicsUnit.Pixel, Attributes); Attributes.Dispose(); NewGraphics.Dispose(); imgBox.Image = NewBitmap; }
/// <summary> /// Verilen Ölçülerde Bitmap'i boyutlandırır. /// </summary> /// <param name="Height">Yükselik Değeri</param> /// <param name="Width">Genişlik Değeri</param> /// <param name="OrjBitmap">Orijinal Bitmap</param> public Bitmap ImageResize(Bitmap OrjBitmap, int Width, int Height) { try { Bitmap thumb = new Bitmap(Width, Height); Graphics mGraphs = Graphics.FromImage(thumb); if (OrjBitmap.RawFormat != ImageFormat.Png) { mGraphs.InterpolationMode = InterpolationMode.High; mGraphs.CompositingQuality = CompositingQuality.HighQuality; mGraphs.SmoothingMode = SmoothingMode.AntiAlias; mGraphs.PixelOffsetMode = PixelOffsetMode.HighQuality; } else { mGraphs.SmoothingMode = SmoothingMode.None; mGraphs.InterpolationMode = InterpolationMode.NearestNeighbor; mGraphs.SmoothingMode = SmoothingMode.None; mGraphs.PixelOffsetMode = PixelOffsetMode.None; } System.Drawing.Imaging.ImageAttributes attr = new System.Drawing.Imaging.ImageAttributes(); attr.SetWrapMode(System.Drawing.Drawing2D.WrapMode.TileFlipXY); mGraphs.DrawImage(OrjBitmap, 0, 0, thumb.Width, thumb.Height); mGraphs.Dispose(); attr.Dispose(); //thumb.Dispose(); return(thumb); } catch (Exception) { return(null); } }
public static Image chGrayscaleImage(Image img) { Bitmap ret = new Bitmap(img.Width, img.Height); Graphics g = Graphics.FromImage(ret); System.Drawing.Imaging.ColorMatrix _cm = new System.Drawing.Imaging.ColorMatrix( new float[][] { new float[] { 0.299F, 0.299F, 0.299F, 0, 0 }, new float[] { 0.587F, 0.587F, 0.587F, 0, 0 }, new float[] { 0.114F, 0.114F, 0.114F, 0, 0 }, new float[] { 0, 0, 0, 1, 0 }, new float[] { 0, 0, 0, 0, 1 } }); System.Drawing.Imaging.ImageAttributes ia = new System.Drawing.Imaging.ImageAttributes(); ia.SetColorMatrix(_cm); g.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, ia); g.Dispose(); ia.Dispose(); return(ret); }
/// <summary> /// 在已经存在的画布上水印 /// </summary> /// <param name="graphics"></param> /// <param name="imageWidth"></param> /// <param name="imageHeight"></param> /// <param name="watermarImagePath"></param> /// <param name="WaterMarkPositions"></param> /// <param name="alpha">透明度,取值0-1之间的小数</param> public static Graphics AddWaterMarkImage(Graphics graphics, int imageWidth, int imageHeight, string watermarImagePath, WaterMarkPositions WaterMarkPositions, float alpha) { if (graphics != null && File.Exists(watermarImagePath)) { using (Image image = new Bitmap(watermarImagePath)) { using (ImageAttributes imageAttr = new ImageAttributes()) { ColorMap map = new ColorMap(); map.OldColor = Color.FromArgb(0xff, 0, 0xff, 0); map.NewColor = Color.FromArgb(0, 0, 0, 0); ColorMap[] mapArray = new ColorMap[] { map }; imageAttr.SetRemapTable(mapArray, ColorAdjustType.Bitmap); float[][] numArray2 = new float[5][]; float[] numArray3 = new float[5]; numArray3[0] = 1f; numArray2[0] = numArray3; numArray3 = new float[5]; numArray3[1] = 1f; numArray2[1] = numArray3; numArray3 = new float[5]; numArray3[2] = 1f; numArray2[2] = numArray3; numArray3 = new float[5]; numArray3[3] = alpha; numArray2[3] = numArray3; numArray3 = new float[5]; numArray3[4] = 1f; numArray2[4] = numArray3; float[][] newColorMatrix = numArray2; ColorMatrix matrix = new ColorMatrix(newColorMatrix); imageAttr.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); int x = 0; int y = 0; switch (WaterMarkPositions) { case WaterMarkPositions.LeftTop: x = 10; y = 10; break; case WaterMarkPositions.CenterTop: x = (imageWidth - image.Width) / 2; y = 10; break; case WaterMarkPositions.RightTop: x = imageWidth - image.Width - 10; y = 10; break; case WaterMarkPositions.LeftMiddle: x = 10; y = (imageHeight - image.Height) / 2; break; case WaterMarkPositions.Center: x = (imageWidth - image.Width) / 2; y = (imageHeight - image.Height) / 2; break; case WaterMarkPositions.RightMiddle: x = imageWidth - image.Width - 10; y = (imageHeight - image.Height) / 2; break; case WaterMarkPositions.LeftBottom: x = 10; y = imageHeight - image.Height - 10; break; case WaterMarkPositions.CenterBottom: x = (imageWidth - image.Width) / 2; y = imageHeight - image.Height - 10; break; case WaterMarkPositions.RightBottom: x = imageWidth - image.Width - 10; y = imageHeight - image.Height - 10; break; } graphics.DrawImage(image, new Rectangle(x, y, image.Width, image.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttr); image.Dispose(); imageAttr.Dispose(); } } } return graphics; }
private void PaintIcon(Graphics g, Bitmap bmp, Rectangle bounds, Color foreColor) { Rectangle bmpRect = new Rectangle(this.DataGridView.RightToLeftInternal ? bounds.Right - DATAGRIDVIEWROWHEADERCELL_iconMarginWidth - DATAGRIDVIEWROWHEADERCELL_iconsWidth : bounds.Left + DATAGRIDVIEWROWHEADERCELL_iconMarginWidth, bounds.Y + (bounds.Height - DATAGRIDVIEWROWHEADERCELL_iconsHeight)/2, DATAGRIDVIEWROWHEADERCELL_iconsWidth, DATAGRIDVIEWROWHEADERCELL_iconsHeight); colorMap[0].NewColor = foreColor; colorMap[0].OldColor = Color.Black; ImageAttributes attr = new ImageAttributes(); attr.SetRemapTable(colorMap, ColorAdjustType.Bitmap); g.DrawImage(bmp, bmpRect, 0, 0, DATAGRIDVIEWROWHEADERCELL_iconsWidth, DATAGRIDVIEWROWHEADERCELL_iconsHeight, GraphicsUnit.Pixel, attr); attr.Dispose(); }
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); }
/// <summary> /// 加图片水印 /// </summary> /// <param name="filename">文件名</param> /// <param name="watermarkFilename">水印文件名</param> /// <param name="watermarkStatus">图片水印位置 0=不使用 1=左上 2=中上 3=右上 4=左中 9=右下</param> /// <param name="quality">附加图片质量,1是 0不是</param> /// <param name="watermarkTransparency">水印的透明度 1--10 10为不透明</param> public static void AddImageSignPic(Image img, string filename, string watermarkFilename, int watermarkStatus, int quality, int watermarkTransparency) { 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); //g.DrawImage(watermark, new System.Drawing.Rectangle(xpos, ypos, watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, System.Drawing.GraphicsUnit.Pixel); 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) { try { img.Save(filename, ici, encoderParams); } catch (Exception ex) { System.Console.WriteLine(ex.Message); } } else { img.Save(filename); } g.Dispose(); img.Dispose(); watermark.Dispose(); imageAttributes.Dispose(); }
/// <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); }
/// <summary> /// Constrasts an image. /// http://bobpowell.net/image_contrast.aspx /// </summary> /// <param name="bmp"></param> /// <param name="value"></param> /// <returns></returns> public static Image Contrast(Image bmp, float value) { Image bmpNew = null; try { var matrix = new float[][] { new float[] {value, 0, 0, 0, 0}, new float[] {0, value, 0, 0, 0}, new float[] {0, 0, value, 0, 0}, new float[] {0, 0, 0, 1f, 0}, //including the BLATANT FUDGE new float[] {0.001f, 0.001f, 0.001f, 0, 1f} }; ColorMatrix cm = new ColorMatrix(matrix); ImageAttributes ia = new ImageAttributes(); ia.SetColorMatrix(cm); bmpNew = new Bitmap(bmp.Width, bmp.Height); ((Bitmap)bmpNew).SetResolution(bmp.HorizontalResolution, bmp.VerticalResolution); using (Graphics g = Graphics.FromImage(bmpNew)) { g.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, ia); ia.Dispose(); } } catch { if (bmpNew != null) { bmpNew.Dispose(); bmpNew = null; } } return bmpNew; }
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> /// Resizes the specified image to the specifed size. /// </summary> /// <param name="image">The image to resize.</param> /// <param name="width">The width in pixels of the resized image.</param> /// <param name="height">The height in pixels of the resized image.</param> /// <param name="dispose">The value indicating whether to dispose the specified image after returning the new resized bitmap.</param> /// <returns>The specifed image, resized to the specified size.</returns> /// <exception cref="System.ArgumentNullException"> /// Thrown when 'image' is null. /// </exception> /// <exception cref="System.ArgumentOutOfRangeException"> /// Thrown when 'width' is less than 0. /// - OR - /// Thrown when 'height' is less than 0. /// </exception> public static Bitmap ResizeImage(Image image, int width, int height, bool dispose = true) { // http://stackoverflow.com/questions/1922040/resize-an-image-c-sharp if (image == null) throw new ArgumentNullException(nameof(image)); if (width < 0) throw new ArgumentOutOfRangeException(nameof(width), width, "'" + nameof(width) + "' cannot be less than 0."); if (height < 0) throw new ArgumentOutOfRangeException(nameof(height), height, "'" + nameof(height) + "' cannot be less than 0."); Bitmap bitmap = new Bitmap(width, height); Graphics graphics = Graphics.FromImage(bitmap); ImageAttributes attributes = new ImageAttributes(); #region >> Sets settings for high quality resizing bitmap.SetResolution(image.HorizontalResolution, image.VerticalResolution); graphics.CompositingMode = CompositingMode.SourceCopy; graphics.CompositingQuality = CompositingQuality.HighQuality; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; attributes.SetWrapMode(WrapMode.TileFlipXY); #endregion graphics.DrawImage(image, new Rectangle(0, 0, width, height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes); if (dispose) image.Dispose(); graphics.Dispose(); attributes.Dispose(); return bitmap; }
private void ContrastTrackBar_Scroll(object sender, EventArgs e) { contrast = 0.04f * ContrastTrackBar.Value; Bitmap bmp = new Bitmap(picture.Width, picture.Height); Graphics g = Graphics.FromImage(bmp); ImageAttributes imageAttr = new ImageAttributes(); ColorMatrix cm = new ColorMatrix(new float[][] { new float[]{contrast,0f,0f,0f,0f}, new float[]{0f,contrast,0f,0f,0f}, new float[]{0f,0f,contrast,0f,0f}, new float[]{0f,0f,0f,1f,0f}, new float[]{0.001f,0.001f,0.001f,0f,1f}}); imageAttr.SetColorMatrix(cm); g.DrawImage(picture, new Rectangle(0, 0, picture.Width, picture.Height), 0, 0, picture.Width, picture.Height, GraphicsUnit.Pixel, imageAttr); g.Dispose(); imageAttr.Dispose(); pictureBox1.Image = bmp; SaveAction(bmp); }
/// <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(); }
public static string CreateWeaterPicture(string paraOldFileName, string paraNewsFileName, string AddPicture, string FileExtensionName) { System.Drawing.Image image = System.Drawing.Image.FromFile(paraOldFileName); System.Drawing.Image copyImage = System.Drawing.Image.FromFile(AddPicture); ImageAttributes imageAttributes = new ImageAttributes(); ColorMap colorMap = new ColorMap(); colorMap.OldColor = Color.FromArgb(200, 0, 200, 0); colorMap.NewColor = Color.FromArgb(0, 0, 0, 0); ColorMap[] remapTable = { colorMap }; imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap); Single WaterMark_Transparency = 0.9f; 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, WaterMark_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); Graphics g = Graphics.FromImage(image); //设定合成图像的质量 g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; //设置高质量插值法 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; int xPos = image.Width - copyImage.Width - 10; int yPos = image.Height - copyImage.Height - 10; int WaterMark_Position = 4; switch (WaterMark_Position)//==========从图片的左上角开始算起(0,0) { case 1: xPos = 10; yPos = 10; break; case 2: xPos = image.Width - copyImage.Width - 10; yPos = 10; break; case 3: xPos = 10; yPos = image.Height - copyImage.Height - 10; break; case 4: xPos = image.Width - copyImage.Width - 10; yPos = image.Height - copyImage.Height - 10; break; } g.DrawImage(copyImage, new Rectangle(xPos, yPos, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel, imageAttributes); switch (FileExtensionName.ToLower()) { case ".jpg": case ".jpeg": image.Save(System.Web.HttpContext.Current.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); break; case ".gif": image.Save(System.Web.HttpContext.Current.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif); break; case ".bmp": image.Save(System.Web.HttpContext.Current.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Bmp); break; case ".png": image.Save(System.Web.HttpContext.Current.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png); break; } g.Dispose(); image.Save(paraNewsFileName); image.Dispose(); imageAttributes.Dispose(); HttpContext.Current.Response.Clear(); if (File.Exists(paraOldFileName) == true) { File.Delete(paraOldFileName); } return paraNewsFileName; }
/// <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(); } }
private static void MakeTransparent(string p) { FileStream fs = new FileStream(p, FileMode.Open, FileAccess.Read); MemoryStream ms = new MemoryStream(); fs.CopyTo(ms); ms.Position = 0; fs.Close(); using (Image original = new Bitmap(ms)) { using (Bitmap image = new Bitmap(original.Width, original.Height, PixelFormat.Format32bppArgb)) { Graphics g = Graphics.FromImage(image); ImageAttributes ia = new ImageAttributes(); ia.SetColorKey(Color.FromArgb(0, 0xde, 0), Color.FromArgb(33, 255, 5)); g.DrawImage( original, new Rectangle(0, 0, original.Width, original.Height), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel, ia); image.Save(p, ImageFormat.Png); g.Dispose(); ia.Dispose(); } } }
/// <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(); } }
private static void DrawImageColorized(Graphics graphics, Image image, Rectangle destination, ColorMatrix matrix) { if (graphics == null) { throw new ArgumentNullException("graphics"); } ImageAttributes imageAttrs = new ImageAttributes(); imageAttrs.SetColorMatrix(matrix); graphics.DrawImage(image, destination, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttrs, null, IntPtr.Zero); imageAttrs.Dispose(); }
protected virtual void OnRenderItemImage (ToolStripItemImageRenderEventArgs e) { bool need_dispose = false; Image i = e.Image; if (e.Item.RightToLeft == RightToLeft.Yes && e.Item.RightToLeftAutoMirrorImage == true) { i = CreateMirrorImage (i); need_dispose = true; } if (e.Item.ImageTransparentColor != Color.Empty) { ImageAttributes ia = new ImageAttributes (); ia.SetColorKey (e.Item.ImageTransparentColor, e.Item.ImageTransparentColor); e.Graphics.DrawImage (i, e.ImageRectangle, 0, 0, i.Width, i.Height, GraphicsUnit.Pixel, ia); ia.Dispose (); } else e.Graphics.DrawImage (i, e.ImageRectangle); if (need_dispose) i.Dispose (); ToolStripItemImageRenderEventHandler eh = (ToolStripItemImageRenderEventHandler)Events [RenderItemImageEvent]; if (eh != null) eh (this, e); }
/// <summary> /// 加图片水印 /// </summary> /// <param name="img">图片</param> /// <param name="watermarkPath">水印文件名</param> public static MemoryStream AddImageWatermark(Image img, string watermarkPath) { int watermarkStatus = 9;//水印位置 int watermarkTransparency = 5;//水印透明度 Graphics g; try { g = Graphics.FromImage(img); } catch (Exception) { Bitmap bitmap = new Bitmap(img.Width, img.Height); g = Graphics.FromImage(bitmap); g.DrawImage(img, 0, 0, img.Width, img.Height); } //设置高质量插值法 //g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度 //g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; Image watermark = new Bitmap(watermarkPath); if (watermark.Height >= img.Height || watermark.Width >= img.Width) { try { return AddImageSignText(img, "易车网", watermarkStatus, 80, "Tahoma", 12); } catch (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)(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); MemoryStream s = new MemoryStream(); img.Save(s, GDI.GetEncoder(ImageFormat.Jpeg), GDI.LosslessParam); g.Dispose(); img.Dispose(); watermark.Dispose(); imageAttributes.Dispose(); return s; }
/// <summary> /// This gets called to instruct the element to draw itself in the appropriate spot of the graphics object /// </summary> /// <param name="g">The graphics object to draw to</param> /// <param name="printing">Boolean, true if this is being drawn to a print document</param> public override void Draw(Graphics g, bool printing) { //This color matrix is used to adjust how the image is drawn to the graphics object float bright = _brightness / 255.0F; float cont = (_contrast + 255.0F) / 255.0F; float[][] colorArray = {new[] {cont, 0, 0, 0, 0}, new[] {0, cont, 0, 0, 0}, new[] {0, 0, cont, 0, 0}, new float[] {0, 0, 0, 1, 0}, new[] {bright, bright, bright, 0, 1}}; ColorMatrix cm = new ColorMatrix(colorArray); ImageAttributes imgAttrib = new ImageAttributes(); imgAttrib.SetColorMatrix(cm); //Defines a parallelgram where the image is to be drawn PointF[] destPoints = new[]{LocationF, new PointF(LocationF.X + Size.Width, LocationF.Y), new PointF(LocationF.X, LocationF.Y + Size.Height)}; Rectangle srcRect; //When printing we use this code if (printing) { //Open the original and gets its rectangle Bitmap original = new Bitmap(_fileName); srcRect = new Rectangle(0, 0, _bitmap.Width, _bitmap.Height); //Modifies the parallelogram if we are preserving aspect ration if (_preserveAspectRatio) { if ((Size.Width / original.Width) < (Size.Height / original.Height)) destPoints[2] = new PointF(LocationF.X, LocationF.Y + (Size.Width * original.Height / original.Width)); else destPoints[1] = new PointF(LocationF.X + (Size.Height * original.Width / original.Height), LocationF.Y); } //Draws the bitmap g.DrawImage(_bitmap, destPoints, srcRect, GraphicsUnit.Pixel, imgAttrib); //Clean up and return imgAttrib.Dispose(); original.Dispose(); return; } //If we are not resizing and in Draft mode if (Resizing == false && Draft) { if (File.Exists(_fileName)) { if ((_bitmap == null) || (_bitmap != null && _bitmap.Width != Convert.ToInt32(Size.Width))) { Bitmap original = new Bitmap(_fileName); if (_bitmap != null) _bitmap.Dispose(); _bitmap = new Bitmap(Convert.ToInt32(Size.Width), Convert.ToInt32(Size.Width * original.Height / original.Width), PixelFormat.Format32bppArgb); Graphics graph = Graphics.FromImage(_bitmap); graph.DrawImage(original, 0, 0, _bitmap.Width, _bitmap.Height); original.Dispose(); graph.Dispose(); } } } if (_bitmap == null) return; //Modifies the parallelogram if we are preserving aspect ration if (_preserveAspectRatio) { if ((Size.Width / _bitmap.Width) < (Size.Height / _bitmap.Height)) destPoints[2] = new PointF(LocationF.X, LocationF.Y + (Size.Width * _bitmap.Height / _bitmap.Width)); else destPoints[1] = new PointF(LocationF.X + (Size.Height * _bitmap.Width / _bitmap.Height), LocationF.Y); } //Draws the bitmap to the screen srcRect = new Rectangle(0, 0, _bitmap.Width, _bitmap.Height); g.DrawImage(_bitmap, destPoints, srcRect, GraphicsUnit.Pixel, imgAttrib); }
public static void GrayScaleImage(Image source, float lum) { // create image attributes to blit image into itself with. ImageAttributes ia = new ImageAttributes(); // create gray scale color matrix that will also translate colors according to brightness. // NOTE: The alpha translate is 0.0 so that transparent pixels will be unaffected. ColorMatrix cm = new ColorMatrix(); cm[0, 0] = kR; cm[0, 1] = kR; cm[0, 2] = kR; cm[0, 3] = 0.0f; cm[0, 4] = 0.0f; cm[1, 0] = kG; cm[1, 1] = kG; cm[1, 2] = kG; cm[1, 3] = 0.0f; cm[1, 4] = 0.0f; cm[2, 0] = kB; cm[2, 1] = kB; cm[2, 2] = kB; cm[2, 3] = 0.0f; cm[2, 4] = 0.0f; cm[3, 0] = 0.000f; cm[3, 1] = 0.000f; cm[3, 2] = 0.000f; cm[3, 3] = 1.0f; cm[3, 4] = 0.0f; cm[4, 0] = lum; cm[4, 1] = lum; cm[4, 2] = lum; cm[4, 3] = 0.0f; cm[4, 4] = 1.0f; // associate matrix with attributes ia.SetColorMatrix(cm); // get a graphics for source image Graphics g = Graphics.FromImage(source); // set compositing mode to copy over g.CompositingMode = CompositingMode.SourceCopy; // turn off aliasing etc g.InterpolationMode = InterpolationMode.NearestNeighbor; g.SmoothingMode = SmoothingMode.None; // create destination rectangle Rectangle d = new Rectangle(0, 0, source.Width, source.Height); // paint into self g.DrawImage(source, d, 0, 0, source.Width, source.Height, GraphicsUnit.Pixel, ia); // cleanup ia.Dispose(); g.Dispose(); }
public static Bitmap ColorizeBitmap(Bitmap original, SColorF color) { Bitmap newBitmap = new Bitmap(original.Width, original.Height); Graphics g = Graphics.FromImage(newBitmap); ColorMatrix cm = new ColorMatrix(); cm.Matrix33 = color.A; cm.Matrix00 = color.R; cm.Matrix11 = color.G; cm.Matrix22 = color.B; cm.Matrix44 = 1; ImageAttributes ia = new ImageAttributes(); ia.SetColorMatrix(cm); g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel, ia); ia.Dispose(); g.Dispose(); return newBitmap; }
/// <summary> /// Uniformly scale RGB channels to change brightness. useful values are -1 to 1 with 0.0f /// causing no change /// </summary> /// <param name="source"></param> /// <param name="luminance"></param> public static void SetBrightness(Image source, float bt) { // create image attributes ImageAttributes ia = new ImageAttributes(); // create color matrix used to scale the channels ColorMatrix cm = new ColorMatrix(); cm[0, 0] = 1.0f; cm[0, 1] = 0.0f; cm[0, 2] = 0.0f; cm[0, 3] = 0.0f; cm[0, 4] = 0.0f; cm[1, 0] = 0.0f; cm[1, 1] = 1.0f; cm[1, 2] = 0.0f; cm[1, 3] = 0.0f; cm[1, 4] = 0.0f; cm[2, 0] = 0.0f; cm[2, 1] = 0.0f; cm[2, 2] = 1.0f; cm[2, 3] = 0.0f; cm[2, 4] = 0.0f; cm[3, 0] = 0.0f; cm[3, 1] = 0.0f; cm[3, 2] = 0.0f; cm[3, 3] = 1.0f; cm[3, 4] = 0.0f; cm[4, 0] = bt; cm[4, 1] = bt; cm[4, 2] = bt; cm[4, 3] = 0.0f; cm[4, 4] = 1.0f; // set matrix into attributes ia.SetColorMatrix(cm); // get a graphics for source image Graphics g = Graphics.FromImage(source); // set compositing mode to copy over g.CompositingMode = CompositingMode.SourceCopy; // turn off aliasing etc g.InterpolationMode = InterpolationMode.NearestNeighbor; g.SmoothingMode = SmoothingMode.None; // create destination rectangle Rectangle d = new Rectangle(0, 0, source.Width, source.Height); // paint into self g.DrawImage(source, d, 0, 0, source.Width, source.Height, GraphicsUnit.Pixel, ia); // cleanup ia.Dispose(); g.Dispose(); }
public override void Render(GdiGraphicsRenderer renderer) { GdiGraphicsWrapper graphics = renderer.GraphicsWrapper; SvgImageElement iElement = (SvgImageElement)element; ImageAttributes imageAttributes = new ImageAttributes(); string sOpacity = iElement.GetPropertyValue("opacity"); if (sOpacity != null && sOpacity.Length > 0) { double opacity = SvgNumber.ParseNumber(sOpacity); ColorMatrix myColorMatrix = new ColorMatrix(); myColorMatrix.Matrix00 = 1.00f; // Red myColorMatrix.Matrix11 = 1.00f; // Green myColorMatrix.Matrix22 = 1.00f; // Blue myColorMatrix.Matrix33 = (float)opacity; // alpha myColorMatrix.Matrix44 = 1.00f; // w imageAttributes.SetColorMatrix(myColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); } float width = (float)iElement.Width.AnimVal.Value; float height = (float)iElement.Height.AnimVal.Value; //Rectangle destRect = new Rectangle(Convert.ToInt32(iElement.X.AnimVal.Value), // Convert.ToInt32(iElement.Y.AnimVal.Value), // Convert.ToInt32(width), Convert.ToInt32(height)); RectangleF destRect = new RectangleF((float)iElement.X.AnimVal.Value, (float)iElement.Y.AnimVal.Value, (float)iElement.Width.AnimVal.Value, (float)iElement.Height.AnimVal.Value); Image image = null; if (iElement.IsSvgImage) { SvgWindow wnd = GetSvgWindow(); _embeddedRenderer.BackColor = Color.Empty; _embeddedRenderer.Render(wnd.Document); image = _embeddedRenderer.RasterImage; } else { image = GetBitmap(iElement); } if (image != null) { //graphics.DrawImage(this, image, destRect, 0f, 0f, // image.Width, image.Height, GraphicsUnit.Pixel, imageAttributes); // code extracted from FitToViewbox SvgPreserveAspectRatio spar = (SvgPreserveAspectRatio)iElement.PreserveAspectRatio.AnimVal ?? new SvgPreserveAspectRatio("none", iElement); double[] translateAndScale = spar.FitToViewBox(new SvgRect(0, 0, image.Width, image.Height), new SvgRect(destRect.X, destRect.Y, destRect.Width, destRect.Height)); graphics.TranslateTransform((float)translateAndScale[0], (float)translateAndScale[1]); graphics.ScaleTransform((float)translateAndScale[2], (float)translateAndScale[3]); graphics.DrawImage(this, image, new Rectangle(0, 0, image.Width, image.Height), 0f, 0f, image.Width, image.Height, GraphicsUnit.Pixel, imageAttributes); image.Dispose(); image = null; } if (_embeddedRenderer != null) { _embeddedRenderer.Dispose(); _embeddedRenderer = null; } if (imageAttributes != null) { imageAttributes.Dispose(); imageAttributes = null; } }
/// <summary> /// Redraw image into itself using the given gamma correction. 0.1 to 5.0 are allowed. 2.0 produces no change /// </summary> /// <param name="source"></param> /// <param name="luminance"></param> public static void SetGamma(Image source, float _gamma) { // clamp to range float gamma = Math.Max(0.1f, Math.Min(_gamma, 5.0f)); // create image attributes ImageAttributes ia = new ImageAttributes(); ia.SetGamma(gamma); // get a graphics for source image Graphics g = Graphics.FromImage(source); // set compositing mode to copy over g.CompositingMode = CompositingMode.SourceCopy; // turn off aliasing etc g.InterpolationMode = InterpolationMode.NearestNeighbor; g.SmoothingMode = SmoothingMode.None; // create destination rectangle Rectangle d = new Rectangle(0, 0, source.Width, source.Height); // paint into self g.DrawImage(source, d, 0, 0, source.Width, source.Height, GraphicsUnit.Pixel, ia); // cleanup ia.Dispose(); g.Dispose(); }
/// <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(); }
/// <summary> /// Threshold the image using the given value. /// </summary> /// <param name="source image ( will be overwritten with thresholded image)"></param> /// <param name="the threshold value ( 0..1)"></param> public static void ThresholdImage(Image source, float t) { // create image attributes to blit image into itself with. ImageAttributes ia = new ImageAttributes(); ia.SetThreshold(t); // get a graphics for source image Graphics g = Graphics.FromImage(source); // set compositing mode to copy over g.CompositingMode = CompositingMode.SourceCopy; // turn off aliasing etc g.InterpolationMode = InterpolationMode.NearestNeighbor; g.SmoothingMode = SmoothingMode.None; // create destination rectangle Rectangle d = new Rectangle(0, 0, source.Width, source.Height); // paint into self g.DrawImage(source, d, 0, 0, source.Width, source.Height, GraphicsUnit.Pixel, ia); // cleanup ia.Dispose(); g.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(); }
// While still in design this function draws directly to the component. // Once design is complete it will paint to a image, then the image painted to the component for a little speed boost. private void DrawKillCircles(Graphics g, Kill kKill, KillDisplayDetails kddKillDetails) { PointF pntLineStart = new PointF((float)kKill.KillerLocation.X, (float)kKill.KillerLocation.Y); PointF pntLineEnd = new PointF((float)kKill.VictimLocation.X, (float)kKill.VictimLocation.Y); PointF pntLineHalfway = new PointF(pntLineStart.X - (pntLineStart.X - pntLineEnd.X) / 2, pntLineStart.Y - (pntLineStart.Y - pntLineEnd.Y) / 2 - 3); PointF pntLineHalfway2 = new PointF(pntLineStart.X - (pntLineStart.X - pntLineEnd.X) / 2, pntLineStart.Y - (pntLineStart.Y - pntLineEnd.Y) / 2 - 4); LinearGradientBrush killBrush = this.GetKillColour(this.KillColours, kKill, kddKillDetails); GraphicsPath gpKillCircles = new GraphicsPath(); gpKillCircles.AddEllipse(new Rectangle(kKill.KillerLocation.X - this.ErrorRadius, kKill.KillerLocation.Y - this.ErrorRadius, this.ErrorRadius * 2, this.ErrorRadius * 2)); gpKillCircles.AddEllipse(new Rectangle(kKill.VictimLocation.X - this.ErrorRadius, kKill.VictimLocation.Y - this.ErrorRadius, this.ErrorRadius * 2, this.ErrorRadius * 2)); gpKillCircles.FillMode = FillMode.Winding; //GraphicsPath gpKill = new GraphicsPath(); GraphicsPath gpKill = (GraphicsPath)gpKillCircles.Clone(); gpKill.AddClosedCurve(new PointF[] { pntLineStart, pntLineHalfway, pntLineEnd, pntLineHalfway2 }); //gpKill.AddEllipse(new Rectangle(kKill.KillerLocation.X - this.ErrorRadius, kKill.KillerLocation.Y - this.ErrorRadius, this.ErrorRadius * 2, this.ErrorRadius * 2)); //gpKill.AddEllipse(new Rectangle(kKill.VictimLocation.X - this.ErrorRadius, kKill.VictimLocation.Y - this.ErrorRadius, this.ErrorRadius * 2, this.ErrorRadius * 2)); gpKill.FillMode = FillMode.Winding; GraphicsPath gpKillOutline = (GraphicsPath)gpKill.Clone(); //GraphicsPath gpKillOutline = new GraphicsPath(gpKill.PathPoints, gpKill.PathTypes); Matrix gpKillMatrix = new Matrix(); gpKillOutline.Widen(this.m_pTwoWidth, gpKillMatrix, 0.01F); Region reKillOutline = new Region(gpKillOutline); reKillOutline.Exclude(gpKill); reKillOutline.Exclude(gpKillCircles); Region reKill = new Region(gpKill); reKill.Union(gpKillCircles); //Region reKillDropshadow = new Region(gpKill); //reKillDropshadow.Union(gpKillCircles); //reKillDropshadow.Union(reKillOutline); //reKillDropshadow.Translate(0.4F, 1.0F); if (reKill.IsVisible(this.ClientPointToGame(this.PointToClient(Cursor.Position))) == true) { kddKillDetails.IsMouseOver = true; kddKillDetails.Opacity = 1.0F; } else { kddKillDetails.IsMouseOver = false; } //g.FillRegion(new SolidBrush(Color.FromArgb((int)(64.0F * kddKillDetails.Opacity), Color.Black)), reKillDropshadow); g.FillRegion(killBrush, reKill); g.FillRegion(new SolidBrush(Color.FromArgb((int)(255.0F * kddKillDetails.Opacity), Color.Black)), reKillOutline); if (this.LoadedMapImagePack != null) { Image imgDeathIcon = null; if (kKill.Headshot == true) { imgDeathIcon = this.LoadedMapImagePack.CompensateImageRotation(this.LoadedMapImagePack.GetIcon("Headshot")); } else { imgDeathIcon = this.LoadedMapImagePack.CompensateImageRotation(this.LoadedMapImagePack.GetIcon("Death")); } if (imgDeathIcon != null) { ColorMatrix colormatrix = new ColorMatrix(); colormatrix.Matrix00 = 1.0F; colormatrix.Matrix11 = 1.0F; colormatrix.Matrix22 = 1.0F; colormatrix.Matrix33 = kddKillDetails.Opacity; colormatrix.Matrix44 = 1.0F; ImageAttributes imgattr = new ImageAttributes(); imgattr.SetColorMatrix(colormatrix); Rectangle destRect = new Rectangle((int)pntLineEnd.X - 12, (int)pntLineEnd.Y - 12, 24, 24); g.DrawImage(imgDeathIcon, destRect, 0, 0, imgDeathIcon.Width, imgDeathIcon.Height, GraphicsUnit.Pixel, imgattr); imgattr.Dispose(); imgDeathIcon.Dispose(); } } this.DrawMapText(g, kKill.Victim.SoldierName, kKill.VictimLocation, 16, kddKillDetails.Opacity); this.DrawMapText(g, kKill.Killer.SoldierName, kKill.KillerLocation, 16, kddKillDetails.Opacity); killBrush.Dispose(); gpKillCircles.Dispose(); gpKill.Dispose(); gpKillOutline.Dispose(); gpKillMatrix.Dispose(); reKill.Dispose(); }