protected static void DrawIcon(Graphics graphics, Image imgPanelIcon, int x, int y) { if (graphics == null) { throw new ArgumentException( string.Format( System.Globalization.CultureInfo.CurrentUICulture, StaticResource.IDS_ArgumentException, typeof(Graphics).Name)); } if (imgPanelIcon == null) { throw new ArgumentException( string.Format( System.Globalization.CultureInfo.CurrentUICulture, StaticResource.IDS_ArgumentException, typeof(Image).Name)); } int iconWidth = imgPanelIcon.Width; int iconHeight = imgPanelIcon.Height; Rectangle rectangleIcon = new Rectangle(x, y, iconWidth, iconHeight); using (System.Drawing.Imaging.ImageAttributes imageAttribute = new System.Drawing.Imaging.ImageAttributes()) { imageAttribute.SetColorKey(Color.Magenta, Color.Magenta); //颜色改为透明 System.Drawing.Imaging.ColorMap colorMap = new System.Drawing.Imaging.ColorMap(); colorMap.OldColor = Color.FromArgb(0, 60, 166); //改颜色 colorMap.NewColor = Color.Black; imageAttribute.SetRemapTable(new System.Drawing.Imaging.ColorMap[] { colorMap }); graphics.DrawImage(imgPanelIcon, rectangleIcon, 0, 0, iconWidth, iconHeight, GraphicsUnit.Pixel, imageAttribute); } }
private Image MergeSignature(Image sigImage, Image docImage) { // Create a new empty bitmap var bitmap = new Bitmap(720, 480); // Set up transparency color mapping var colorMap = new System.Drawing.Imaging.ColorMap[1]; colorMap[0] = new System.Drawing.Imaging.ColorMap(); colorMap[0].OldColor = Color.White; colorMap[0].NewColor = Color.Transparent; var attr = new System.Drawing.Imaging.ImageAttributes(); attr.SetRemapTable(colorMap); // Execute the merge using (Graphics grfx = Graphics.FromImage(bitmap)) { grfx.DrawImage(docImage, 0, 0); grfx.DrawImage(sigImage, new Rectangle(200, 200, sigImage.Width, sigImage.Height), 0, 0, sigImage.Width, sigImage.Height, GraphicsUnit.Pixel, attr); grfx.Save(); } return((Image)bitmap); }
public static Bitmap ReplaceColourInBitmap(Bitmap source, System.Drawing.Imaging.ColorMap[] remap) { Bitmap newmap = new Bitmap(source.Width, source.Height); System.Drawing.Imaging.ImageAttributes ia = new System.Drawing.Imaging.ImageAttributes(); ia.SetRemapTable(remap, System.Drawing.Imaging.ColorAdjustType.Bitmap); using (Graphics gr = Graphics.FromImage(newmap)) gr.DrawImage(source, new Rectangle(0, 0, source.Width, source.Height), 0, 0, source.Width, source.Height, GraphicsUnit.Pixel, ia); return(newmap); }
// 添加图片水印 public static string addWaterMark(string filepath, string fileName) { System.Drawing.Image image = System.Drawing.Image.FromFile(filepath + fileName); 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); Image watermark = new Bitmap(AppDomain.CurrentDomain.BaseDirectory + @"Content/image/logo.png"); 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.3f, 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) / 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(); //保存加水印过后的图片,删除原始图片 fileName = DateTime.Now.ToFileTime() + new Random().Next(100, 999) + System.IO.Path.GetExtension(fileName); b.Save(filepath + fileName); b.Dispose(); image.Dispose(); if (System.IO.File.Exists(filepath)) { System.IO.File.Delete(filepath); } return(fileName); }
public static Bitmap ReplaceColourInBitmap(Bitmap source, System.Drawing.Imaging.ColorMap[] remap) { Bitmap newmap = new Bitmap(source.Width, source.Height); System.Drawing.Imaging.ImageAttributes ia = new System.Drawing.Imaging.ImageAttributes(); ia.SetRemapTable(remap, System.Drawing.Imaging.ColorAdjustType.Bitmap); using (Graphics gr = Graphics.FromImage(newmap)) { gr.DrawImage(source, new Rectangle(0, 0, source.Width, source.Height), 0, 0, source.Width, source.Height, GraphicsUnit.Pixel, ia); } return newmap; }
/// <summary> /// Draws the Chevron Image at the specified location. /// </summary> /// <param name="graphics">The Graphics to draw on.</param> /// <param name="imgChevron">Chevron Image to draw.</param> /// <param name="chevronBackColor"></param> /// <param name="iPositionX">The left position of the Chevron Image</param> protected static void DrawChevron(Graphics graphics, Image imgChevron, Rectangle imageRectangle, Color chevronBackColor) { if (graphics == null) { throw new ArgumentException( string.Format( System.Globalization.CultureInfo.CurrentUICulture, Resources.IDS_ArgumentException, typeof(Graphics).Name)); } if (imgChevron == null) { throw new ArgumentException( string.Format( System.Globalization.CultureInfo.CurrentUICulture, Resources.IDS_ArgumentException, typeof(Image).Name)); } int chevronPositionX = imageRectangle.Left; int chevronPositionY = ImagePaddingTop; Rectangle rectangleChevron = new Rectangle( chevronPositionX + (imageRectangle.Width / 3) - 2, chevronPositionY + 3, 9, 9); using (System.Drawing.Imaging.ImageAttributes imageAttribute = new System.Drawing.Imaging.ImageAttributes()) { imageAttribute.SetColorKey(Color.White, Color.White); System.Drawing.Imaging.ColorMap colorMap = new System.Drawing.Imaging.ColorMap(); colorMap.OldColor = Color.FromArgb(0, 60, 166); colorMap.NewColor = chevronBackColor; imageAttribute.SetRemapTable(new System.Drawing.Imaging.ColorMap[] { colorMap }); using (LinearGradientBrush brushInnerCircle = new LinearGradientBrush( new Rectangle(chevronPositionX, chevronPositionY, 16, 16), Color.FromArgb(128, Color.White), Color.FromArgb(0, Color.White), LinearGradientMode.Vertical)) { graphics.FillPath(brushInnerCircle, GetPath(new Rectangle(chevronPositionX, chevronPositionY, 15, 15), 5)); } graphics.DrawImage(imgChevron, rectangleChevron, 0, 0, 9, 9, GraphicsUnit.Pixel, imageAttribute); } }
public static Bitmap ReplaceColor(this Image image, Color oldColor, Color newColor) { var bmp = new Bitmap(image.Width, image.Height); using (var gr = Graphics.FromImage(bmp)) { var imageAttrs = new System.Drawing.Imaging.ImageAttributes(); var colorMap = new System.Drawing.Imaging.ColorMap() { OldColor = oldColor, NewColor = newColor }; imageAttrs.SetRemapTable(new[] { colorMap }); gr.DrawImage(image, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, imageAttrs); } return(bmp); }
private static void GenerateImageResourcesOfAminoAcid(char aa, string filename, bool pos, Color color) { try { Assembly assembly = Assembly.GetExecutingAssembly(); if (!filename.StartsWith("PeSA")) { filename = "PeSA.Engine.Resources." + filename; } Stream stream = assembly.GetManifestResourceStream(filename); if (stream == null) { return; } Image image = Image.FromStream(stream); System.Drawing.Imaging.ImageAttributes imageAttributes = new System.Drawing.Imaging.ImageAttributes(); int width = image.Width; int height = image.Height; System.Drawing.Imaging.ColorMap colorMap = new System.Drawing.Imaging.ColorMap(); colorMap.OldColor = Color.Black; colorMap.NewColor = color; System.Drawing.Imaging.ColorMap[] remapTable = { colorMap }; imageAttributes.SetRemapTable(remapTable, System.Drawing.Imaging.ColorAdjustType.Bitmap); Bitmap bmp = new Bitmap(width, height); using (var gr = Graphics.FromImage(bmp)) { gr.DrawImage(image, new Rectangle(0, 0, width, height), 0, 0, width, height, GraphicsUnit.Pixel, imageAttributes); if (pos) { SetImage(PositiveImages, aa, bmp); } else { SetImage(NegativeImages, aa, bmp); } } } catch (Exception exc) { } }
public static System.Drawing.Bitmap ChangeImageColor(Bitmap source, System.Drawing.Color OldColor, System.Drawing.Color NewColor) { Bitmap Result = new Bitmap(source.Width, source.Height); Graphics g = Graphics.FromImage(Result); using (Bitmap bmp = new Bitmap(source)) { // Set the image attribute's color mappings System.Drawing.Imaging.ColorMap[] colorMap = new System.Drawing.Imaging.ColorMap[1]; colorMap[0] = new System.Drawing.Imaging.ColorMap(); colorMap[0].OldColor = OldColor; colorMap[0].NewColor = NewColor; System.Drawing.Imaging.ImageAttributes attr = new System.Drawing.Imaging.ImageAttributes(); attr.SetRemapTable(colorMap); // Draw using the color map System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height); g.DrawImage(bmp, rect, 0, 0, rect.Width, rect.Height, GraphicsUnit.Pixel, attr); } return(Result); }
/// <summary> /// 图片等比缩放 /// </summary> /// <remarks>吴剑 2011-01-21</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) { //创建目录 DME_Files.InitFolder(DME_Path.GetDirectoryName(savePath)); //原始图片(获取原始图片创建对象,并使用流中嵌入的颜色管理信息) 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 System.Drawing.Font("黑体", 12); System.Drawing.Brush brushWater = new System.Drawing.SolidBrush(System.Drawing.Color.White); gWater.DrawString(watermarkText, fontWater, brushWater, 10, 10); gWater.Dispose(); } } //透明图片水印 if (watermarkImage != "") { if (DME_Files.FileExists(watermarkImage)) { //获取水印图片 using (System.Drawing.Image wrImage = System.Drawing.Image.FromFile(watermarkImage)) { //水印绘制条件:原始图片宽高均大于或等于水印图片 if (initImage.Width >= wrImage.Width && initImage.Height >= wrImage.Height) { System.Drawing.Graphics gWater = System.Drawing.Graphics.FromImage(initImage); //透明属性 System.Drawing.Imaging.ImageAttributes imgAttributes = 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 }; imgAttributes.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.5f, 0.0f},//透明度:0.5 new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f} }; System.Drawing.Imaging.ColorMatrix wmColorMatrix = new System.Drawing.Imaging.ColorMatrix(colorMatrixElements); imgAttributes.SetColorMatrix(wmColorMatrix, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap); gWater.DrawImage(wrImage, new System.Drawing.Rectangle(initImage.Width - wrImage.Width, initImage.Height - wrImage.Height, wrImage.Width, wrImage.Height), 0, 0, wrImage.Width, wrImage.Height, System.Drawing.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(System.Drawing.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 System.Drawing.Font("宋体", 10); System.Drawing.Brush brushWater = new System.Drawing.SolidBrush(System.Drawing.Color.White); gWater.DrawString(watermarkText, fontWater, brushWater, 10, 10); gWater.Dispose(); } } //透明图片水印 if (watermarkImage != "") { if (DME_Files.FileExists(watermarkImage)) { //获取水印图片 using (System.Drawing.Image wrImage = System.Drawing.Image.FromFile(watermarkImage)) { //水印绘制条件:原始图片宽高均大于或等于水印图片 if (newImage.Width >= wrImage.Width && newImage.Height >= wrImage.Height) { System.Drawing.Graphics gWater = System.Drawing.Graphics.FromImage(newImage); //透明属性 System.Drawing.Imaging.ImageAttributes imgAttributes = 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 }; imgAttributes.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.5f, 0.0f},//透明度:0.5 new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f} }; System.Drawing.Imaging.ColorMatrix wmColorMatrix = new System.Drawing.Imaging.ColorMatrix(colorMatrixElements); imgAttributes.SetColorMatrix(wmColorMatrix, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap); gWater.DrawImage(wrImage, new System.Drawing.Rectangle(newImage.Width - wrImage.Width, newImage.Height - wrImage.Height, wrImage.Width, wrImage.Height), 0, 0, wrImage.Width, wrImage.Height, System.Drawing.GraphicsUnit.Pixel, imgAttributes); gWater.Dispose(); } wrImage.Dispose(); } } } //保存缩略图 newImage.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg); //释放资源 newG.Dispose(); newImage.Dispose(); initImage.Dispose(); } }
/// <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(); }
/// <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(); }
/// <summary> /// Draws the icon image at the specified location. /// </summary> /// <param name="graphics">The Graphics to draw on.</param> /// <param name="imgPanelIcon">icon image to draw.</param> /// <param name="imageRectangle">A rectangle structure that specifies the bounds of the linear gradient.</param> /// <param name="foreColorImage">The foreground color of this image</param> /// <param name="iconPositionY">The vertical position for the icon image</param> protected static void DrawIcon(Graphics graphics, Image imgPanelIcon, Rectangle imageRectangle, Color foreColorImage, int iconPositionY) { if (graphics == null) { throw new ArgumentException( string.Format( System.Globalization.CultureInfo.CurrentUICulture, Resources.IDS_ArgumentException, typeof(Graphics).Name)); } if (imgPanelIcon == null) { throw new ArgumentException( string.Format( System.Globalization.CultureInfo.CurrentUICulture, Resources.IDS_ArgumentException, typeof(Image).Name)); } int iconPositionX = imageRectangle.Left; int iconWidth = imgPanelIcon.Width; int iconHeight = imgPanelIcon.Height; Rectangle rectangleIcon = new Rectangle( iconPositionX + (iconWidth / 2) - 1, iconPositionY + (iconHeight / 2) - 1, imgPanelIcon.Width, imgPanelIcon.Height - 1); using (System.Drawing.Imaging.ImageAttributes imageAttribute = new System.Drawing.Imaging.ImageAttributes()) { imageAttribute.SetColorKey(Color.Magenta, Color.Magenta); System.Drawing.Imaging.ColorMap colorMap = new System.Drawing.Imaging.ColorMap(); colorMap.OldColor = Color.FromArgb(0, 60, 166); colorMap.NewColor = foreColorImage; imageAttribute.SetRemapTable(new System.Drawing.Imaging.ColorMap[] { colorMap }); graphics.DrawImage(imgPanelIcon, rectangleIcon, 0, 0, iconWidth, iconHeight, GraphicsUnit.Pixel, imageAttribute); } }
/// <summary> /// 在图片上生成图片水印 /// </summary> /// <param name="sourcePath">原图片路径</param> /// <param name="targetPath">另存为路径</param> /// <param name="waterMarkFilePath">水印图片路径</param> /// <param name="positionType">图片水印位置:UL,UM , UR , ML , MM , MR , BL , BM , BR ,CUSTORM 为自定义.</param> /// <param name="quality">是否是高质量图片 取值范围0--100</param> /// <param name="watermarkTransparency">图片水印透明度 取值范围1--10 (10为不透明)</param> /// <returns></returns> public static bool PictureWatermark(string sourcePath, string targetPath, string waterMarkFilePath, PicWaterMarkPosition positionType, int quality, int watermarkTransparency) { System.Drawing.Image img = System.Drawing.Image.FromFile(sourcePath); System.Drawing.Bitmap outPut = new System.Drawing.Bitmap(img); try { #region using using (Graphics g = Graphics.FromImage(outPut)) { //设置高质量插值法 //g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度 //g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; System.Drawing.Image markImg = new System.Drawing.Bitmap(waterMarkFilePath); if (markImg.Height >= img.Height || markImg.Width >= img.Width) { return false; } 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; #region position switch (positionType) { case PicWaterMarkPosition.UL: xpos = (int)(img.Width * (float).01); ypos = (int)(img.Height * (float).01); break; case PicWaterMarkPosition.UM: xpos = (int)((img.Width * (float).50) - (markImg.Width / 2)); ypos = (int)(img.Height * (float).01); break; case PicWaterMarkPosition.UR: xpos = (int)((img.Width * (float).99) - (markImg.Width)); ypos = (int)(img.Height * (float).01); break; case PicWaterMarkPosition.ML: xpos = (int)(img.Width * (float).01); ypos = (int)((img.Height * (float).50) - (markImg.Height / 2)); break; case PicWaterMarkPosition.MM: xpos = (int)((img.Width * (float).50) - (markImg.Width / 2)); ypos = (int)((img.Height * (float).50) - (markImg.Height / 2)); break; case PicWaterMarkPosition.MR: xpos = (int)((img.Width * (float).99) - (markImg.Width)); ypos = (int)((img.Height * (float).50) - (markImg.Height / 2)); break; case PicWaterMarkPosition.BL: xpos = (int)(img.Width * (float).01); ypos = (int)((img.Height * (float).99) - markImg.Height); break; case PicWaterMarkPosition.BM: xpos = (int)((img.Width * (float).50) - (markImg.Width / 2)); ypos = (int)((img.Height * (float).99) - markImg.Height); break; case PicWaterMarkPosition.BR: xpos = (int)((img.Width * (float).99) - (markImg.Width)); ypos = (int)((img.Height * (float).99) - markImg.Height); break; } #endregion position g.DrawImage(markImg, new System.Drawing.Rectangle(xpos, ypos, markImg.Width, markImg.Height), 0, 0, markImg.Width, markImg.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) { 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) { outPut.Save(targetPath, ici, encoderParams); } else { outPut.Save(targetPath); } g.Dispose(); markImg.Dispose(); imageAttributes.Dispose(); } #endregion using } catch (Exception) { return false; } finally { if (outPut != null) { outPut.Dispose(); } if (img != null) { img.Dispose(); } } return true; }
/// <summary> /// 图片水印 /// </summary> /// <param name="ImgFile">原图文件地址</param> /// <param name="WaterImg">水印图片地址</param> /// <param name="sImgPath">水印图片保存地址</param> /// <param name="Alpha">水印透明度设置</param> /// <param name="iScale">水印图片在原图上的显示比例</param> /// <param name="intDistance">水印图片在原图上的边距确定,以图片的右边和下边为准,当设定的边距超过一定大小后参数会自动失效</param> public void zzsImgWater( string ImgFile , string WaterImg , string sImgPath , float Alpha , float iScale , int intDistance ) { try { //装载图片 FileStream fs = new FileStream(ImgFile, FileMode.Open); BinaryReader br = new BinaryReader(fs); byte[] bytes = br.ReadBytes((int)fs.Length); br.Close(); fs.Close(); MemoryStream ms = new MemoryStream(bytes); System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(ms); //System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(ImgFile); int imgPhotoWidth = imgPhoto.Width; int imgPhotoHeight = imgPhoto.Height; System.Drawing.Image imgWatermark = new Bitmap(WaterImg); int imgWatermarkWidth = imgWatermark.Width; int imgWatermarkHeight = imgWatermark.Height; //计算水印图片尺寸 decimal aScale = Convert.ToDecimal(iScale); decimal pScale = 0.05M; decimal MinScale = aScale - pScale; decimal MaxScale = aScale + pScale; int imgWatermarkWidthNew = imgWatermarkWidth; int imgWatermarkHeightNew = imgWatermarkHeight; if (imgPhotoWidth >= imgWatermarkWidth && imgPhotoHeight >= imgWatermarkHeight && imgPhotoWidth >= imgPhotoHeight) { if (imgWatermarkWidth > imgWatermarkHeight) { if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7)) && (Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7) <= MaxScale)) { } else { imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale); imgWatermarkHeightNew = Convert.ToInt32((imgPhotoWidth * aScale / imgWatermarkWidth) * imgWatermarkHeight); } } else if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7)) && (Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7) <= MaxScale)) { } else { imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale); imgWatermarkWidthNew = Convert.ToInt32((imgPhotoHeight * aScale / imgWatermarkHeight) * imgWatermarkWidth); } } if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgWatermarkWidth >= imgWatermarkHeight) { imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale); imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight); } if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight <= imgPhotoHeight && imgPhotoWidth >= imgPhotoHeight) { imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale); imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight); } if (imgWatermarkWidth <= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgPhotoWidth >= imgPhotoHeight) { imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale); imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth); } if (imgPhotoWidth >= imgWatermarkWidth && imgPhotoHeight >= imgWatermarkHeight && imgPhotoWidth <= imgPhotoHeight) { if (imgWatermarkWidth > imgWatermarkHeight) { if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7)) && (Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7) <= MaxScale)) { } else { imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale); imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight); } } else if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7)) && (Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7) <= MaxScale)) { } else { imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale); imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth); } } if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgWatermarkWidth <= imgWatermarkHeight) { imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale); imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth); } if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight <= imgPhotoHeight && imgPhotoWidth <= imgPhotoHeight) { imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale); imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight); } if (imgWatermarkWidth <= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgPhotoWidth <= imgPhotoHeight) { imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale); imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth); } //将原图画出来 Bitmap bmPhoto = new Bitmap(imgPhotoWidth, imgPhotoHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb); bmPhoto.SetResolution(72, 72); Graphics gbmPhoto = Graphics.FromImage(bmPhoto); gbmPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; gbmPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; gbmPhoto.Clear(Color.White); gbmPhoto.DrawImage( imgPhoto , new Rectangle(0, 0, imgPhotoWidth, imgPhotoHeight) , 0 , 0 , imgPhotoWidth , imgPhotoHeight , GraphicsUnit.Pixel ); Bitmap bmWatermark = new Bitmap(bmPhoto); bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); Graphics gWatermark = Graphics.FromImage(bmWatermark); //指定高质量显示水印图片质量 gWatermark.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; gWatermark.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 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, Alpha, 0.0f }, new float[] { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f } }; System.Drawing.Imaging.ColorMatrix wmColorMatrix = new System.Drawing.Imaging.ColorMatrix(colorMatrixElements); imageAttributes.SetColorMatrix(wmColorMatrix, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap); //确定水印边距 int xPos = imgPhotoWidth - imgWatermarkWidthNew; int yPos = imgPhotoHeight - imgWatermarkHeightNew; int xPosOfWm = 0; int yPosOfWm = 0; if (xPos > intDistance) { xPosOfWm = xPos - intDistance; } else { xPosOfWm = xPos; } if (yPos > intDistance) { yPosOfWm = yPos - intDistance; } else { yPosOfWm = yPos; } gWatermark.DrawImage( imgWatermark , new Rectangle(xPosOfWm, yPosOfWm, imgWatermarkWidthNew, imgWatermarkHeightNew) , 0 , 0 , imgWatermarkWidth , imgWatermarkHeight , GraphicsUnit.Pixel , imageAttributes ); imgPhoto = bmWatermark; //以jpg格式保存图片 imgPhoto.Save(sImgPath, System.Drawing.Imaging.ImageFormat.Jpeg); //销毁对象 gbmPhoto.Dispose(); gWatermark.Dispose(); imgPhoto.Dispose(); imgWatermark.Dispose(); } catch (Exception ex) { } }
/// <summary> /// 指定された色を塗る /// </summary> private void btn_paint_Click(object sender, EventArgs e) { try { if (btn_selectcolor1.BackColor == btn_selectcolor2.BackColor) { DialogResult result = MessageBox.Show( "1色で塗り潰されます。" + Environment.NewLine + "2色で塗り分ける場合は、画像選択からテンプレートを選び直してください。", "お知らせ", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation); if (result == DialogResult.Cancel) { return; } } //描画先とするImageオブジェクトを作成する Bitmap canvas = new Bitmap(pictureBox1.Image.Width, pictureBox1.Image.Height); //Bitmap canvas = new Bitmap(global::VI_mark.Properties.Settings.Default.Width, // global::VI_mark.Properties.Settings.Default.Height); //ImageオブジェクトのGraphicsオブジェクトを作成する Graphics g = Graphics.FromImage(canvas); //ColorMapオブジェクトの配列(カラーリマップテーブル)を作成 System.Drawing.Imaging.ColorMap[] cms = new System.Drawing.Imaging.ColorMap[] { new System.Drawing.Imaging.ColorMap(), new System.Drawing.Imaging.ColorMap() }; //SkyBlueを色1に変換する cms[0].OldColor = cmr[0].OldColor; cms[0].NewColor = btn_selectcolor1.BackColor; //PowderBlueを色2に変換する cms[1].OldColor = cmr[1].OldColor; cms[1].NewColor = btn_selectcolor2.BackColor; //色情報を保存 cmr[0].OldColor = btn_selectcolor1.BackColor; cmr[1].OldColor = btn_selectcolor2.BackColor; //ImageAttributesオブジェクトの作成 System.Drawing.Imaging.ImageAttributes ia = new System.Drawing.Imaging.ImageAttributes(); //ColorMapを設定 ia.SetRemapTable(cms); //色を変換して画像を描画 g.DrawImage(pictureBox1.Image, new Rectangle(0, 0, pictureBox1.Image.Width, pictureBox1.Image.Height), 0, 0, pictureBox1.Image.Width, pictureBox1.Image.Height, GraphicsUnit.Pixel, ia); //Graphicsオブジェクトのリソースを解放する g.Dispose(); // pictureBox1に表示する pictureBox1.SizeMode = PictureBoxSizeMode.Zoom; pictureBox1.Image = canvas; } catch (Exception ex) { MessageBox.Show(ex.Message, "エラー"); } finally { string s = @" ( ・∀・) | | ガッ と ) | | Y /ノ 人 / ) < >__Λ∩ _/し' //. V`Д´)/ (_フ彡 /"; MessageBox.Show(s, "ぬるぽ"); } }
/// <summary> /// 文字和Logo图片水印 /// </summary> /// <param name="ImgFile">原图文件地址</param> /// <param name="WaterImg">水印图片地址</param> /// <param name="TextFont">水印文字信息</param> /// <param name="sImgPath">生存水印图片后的保存地址</param> /// <param name="ImgAlpha">水印图片的透明度</param> /// <param name="imgiScale">水印图片在原图上的显示比例</param> /// <param name="intimgDistance">水印图片在原图上的边距确定,以图片的右边和下边为准,当设定的边距超过一定大小后参数会自动失效</param> /// <param name="texthScale">水印文字高度位置,从图片底部开始计算,0-1</param> /// <param name="textwidthFont">文字块在图片中所占宽度比例 0-1</param> /// <param name="textAlpha">文字透明度 其数值的范围在0到255</param> public void zzsImgTextWater( string ImgFile , string WaterImg , string TextFont , string sImgPath , float ImgAlpha , float imgiScale , int intimgDistance , float texthScale , float textwidthFont , int textAlpha ) { try { FileStream fs = new FileStream(ImgFile, FileMode.Open); BinaryReader br = new BinaryReader(fs); byte[] bytes = br.ReadBytes((int)fs.Length); br.Close(); fs.Close(); MemoryStream ms = new MemoryStream(bytes); System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(ms); //System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(ImgFile); int imgPhotoWidth = imgPhoto.Width; int imgPhotoHeight = imgPhoto.Height; Bitmap bmPhoto = new Bitmap(imgPhotoWidth, imgPhotoHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb); bmPhoto.SetResolution(72, 72); Graphics gbmPhoto = Graphics.FromImage(bmPhoto); gbmPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; gbmPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; gbmPhoto.DrawImage( imgPhoto , new Rectangle(0, 0, imgPhotoWidth, imgPhotoHeight) , 0 , 0 , imgPhotoWidth , imgPhotoHeight , GraphicsUnit.Pixel ); //建立字体大小的数组,循环找出适合图片的水印字体 int[] sizes = new int[] { 1000, 800, 700, 650, 600, 560, 540, 500, 450, 400, 380, 360, 340, 320, 300, 280, 260, 240, 220, 200, 180, 160, 140, 120, 100, 80, 72, 64, 48, 32, 28, 26, 24, 20, 28, 16, 14, 12, 10, 8, 6, 4, 2 }; System.Drawing.Font crFont = null; System.Drawing.SizeF crSize = new SizeF(); for (int i = 0; i < 43; i++) { crFont = new Font("arial", sizes[i], FontStyle.Bold); crSize = gbmPhoto.MeasureString(TextFont, crFont); if ((ushort)crSize.Width < (ushort)imgPhotoWidth * textwidthFont) { break; } } //设置水印字体的位置 int yPixlesFromBottom = (int)(imgPhotoHeight * texthScale); float yPosFromBottom = ((imgPhotoHeight - yPixlesFromBottom) - (crSize.Height / 2)); float xCenterOfImg = (imgPhotoWidth * 1 / 2); System.Drawing.StringFormat StrFormat = new System.Drawing.StringFormat(); StrFormat.Alignment = System.Drawing.StringAlignment.Center; // System.Drawing.SolidBrush semiTransBrush2 = new System.Drawing.SolidBrush(Color.FromArgb(textAlpha, 0, 0, 0)); gbmPhoto.DrawString( TextFont , crFont , semiTransBrush2 , new System.Drawing.PointF(xCenterOfImg + 1, yPosFromBottom + 1) , StrFormat ); System.Drawing.SolidBrush semiTransBrush = new System.Drawing.SolidBrush(Color.FromArgb(textAlpha, 255, 255, 255)); gbmPhoto.DrawString( TextFont , crFont , semiTransBrush , new System.Drawing.PointF(xCenterOfImg, yPosFromBottom) , StrFormat ); System.Drawing.Image imgWatermark = new Bitmap(WaterImg); int imgWatermarkWidth = imgWatermark.Width; int imgWatermarkHeight = imgWatermark.Height; //计算水印图片尺寸 decimal aScale = Convert.ToDecimal(imgiScale); decimal pScale = 0.05M; decimal MinScale = aScale - pScale; decimal MaxScale = aScale + pScale; int imgWatermarkWidthNew = imgWatermarkWidth; int imgWatermarkHeightNew = imgWatermarkHeight; if (imgPhotoWidth >= imgWatermarkWidth && imgPhotoHeight >= imgWatermarkHeight && imgPhotoWidth >= imgPhotoHeight) { if (imgWatermarkWidth > imgWatermarkHeight) { if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7)) && (Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7) <= MaxScale)) { } else { imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale); imgWatermarkHeightNew = Convert.ToInt32((imgPhotoWidth * aScale / imgWatermarkWidth) * imgWatermarkHeight); } } else if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7)) && (Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7) <= MaxScale)) { } else { imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale); imgWatermarkWidthNew = Convert.ToInt32((imgPhotoHeight * aScale / imgWatermarkHeight) * imgWatermarkWidth); } } if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgWatermarkWidth >= imgWatermarkHeight) { imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale); imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight); } if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight <= imgPhotoHeight && imgPhotoWidth >= imgPhotoHeight) { imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale); imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight); } if (imgWatermarkWidth <= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgPhotoWidth >= imgPhotoHeight) { imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale); imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth); } if (imgPhotoWidth >= imgWatermarkWidth && imgPhotoHeight >= imgWatermarkHeight && imgPhotoWidth <= imgPhotoHeight) { if (imgWatermarkWidth > imgWatermarkHeight) { if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7)) && (Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7) <= MaxScale)) { } else { imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale); imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight); } } else if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7)) && (Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7) <= MaxScale)) { } else { imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale); imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth); } } if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgWatermarkWidth <= imgWatermarkHeight) { imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale); imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth); } if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight <= imgPhotoHeight && imgPhotoWidth <= imgPhotoHeight) { imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale); imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight); } if (imgWatermarkWidth <= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgPhotoWidth <= imgPhotoHeight) { imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale); imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth); } //将原图画出来 Bitmap bmWatermark = new Bitmap(bmPhoto); bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); Graphics gWatermark = Graphics.FromImage(bmWatermark); //指定高质量显示水印图片质量 gWatermark.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; gWatermark.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 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, ImgAlpha, 0.0f }, new float[] { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f } }; System.Drawing.Imaging.ColorMatrix wmColorMatrix = new System.Drawing.Imaging.ColorMatrix(colorMatrixElements); imageAttributes.SetColorMatrix(wmColorMatrix, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap); //确定水印边距 int xPos = imgPhotoWidth - imgWatermarkWidthNew; int yPos = imgPhotoHeight - imgWatermarkHeightNew; int xPosOfWm = 0; int yPosOfWm = 0; if (xPos > intimgDistance) { xPosOfWm = xPos - intimgDistance; } else { xPosOfWm = xPos; } if (yPos > intimgDistance) { yPosOfWm = yPos - intimgDistance; } else { yPosOfWm = yPos; } gWatermark.DrawImage( imgWatermark , new Rectangle(xPosOfWm, yPosOfWm, imgWatermarkWidthNew, imgWatermarkHeightNew) , 0 , 0 , imgWatermarkWidth , imgWatermarkHeight , GraphicsUnit.Pixel , imageAttributes ); imgPhoto = bmWatermark; //以jpg格式保存图片 imgPhoto.Save(sImgPath, System.Drawing.Imaging.ImageFormat.Jpeg); //销毁对象 gbmPhoto.Dispose(); gWatermark.Dispose(); bmPhoto.Dispose(); imgPhoto.Dispose(); imgWatermark.Dispose(); } catch (Exception ex) { } }
/// <summary> /// Draws the Chevron Image at the specified location. /// </summary> /// <param name="graphics">The Graphics to draw on.</param> /// <param name="imgChevron">Chevron image to draw.</param> /// <param name="imageRectangle">A Rectangle structure that specifies the bounds of the linear gradient.</param> /// <param name="foreColorImage">the foreground color of this image</param> /// <param name="bDrawBackground">Gets or sets a value indicating whether the background of the close and expand images is drawn</param> protected static void DrawChevron(Graphics graphics, Image imgChevron, Rectangle imageRectangle, Color foreColorImage, bool bDrawBackground, int chevronPositionY) { if (graphics == null) { throw new ArgumentException( string.Format( System.Globalization.CultureInfo.CurrentUICulture, Resources.IDS_ArgumentException, typeof(Graphics).Name)); } if (imgChevron == null) { throw new ArgumentException( string.Format( System.Globalization.CultureInfo.CurrentUICulture, Resources.IDS_ArgumentException, typeof(Image).Name)); } int chevronPositionX = imageRectangle.Left; Rectangle rectangleChevron = new Rectangle( chevronPositionX + (imageRectangle.Width / 3) - 2, chevronPositionY + 3, 9, 9); using (System.Drawing.Imaging.ImageAttributes imageAttribute = new System.Drawing.Imaging.ImageAttributes()) { imageAttribute.SetColorKey(Color.Magenta, Color.Magenta); System.Drawing.Imaging.ColorMap colorMap = new System.Drawing.Imaging.ColorMap(); colorMap.OldColor = Color.FromArgb(0, 60, 166); colorMap.NewColor = foreColorImage; imageAttribute.SetRemapTable(new System.Drawing.Imaging.ColorMap[] { colorMap }); if (bDrawBackground == true) { using (LinearGradientBrush brushInnerCircle = new LinearGradientBrush( new Rectangle(chevronPositionX, chevronPositionY, 16, 16), Color.FromArgb(128, Color.White), Color.FromArgb(0, Color.White), LinearGradientMode.Vertical)) { graphics.FillPath(brushInnerCircle, GetPath(new Rectangle(chevronPositionX, chevronPositionY, 15, 15), 5)); } } graphics.DrawImage(imgChevron, rectangleChevron, 0, 0, 9, 9, GraphicsUnit.Pixel, imageAttribute); } }
protected override void WndProc( ref Message m ) { base.WndProc( ref m ); switch( m.Msg ) { case win32.WM_PAINT : //this.BorderStyle= BorderStyle.None ; 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; System.Drawing.Imaging.ImageAttributes imgAttrib = new System.Drawing.Imaging.ImageAttributes(); System.Drawing.Imaging.ColorMap[] colorMap = new System.Drawing.Imaging.ColorMap[1]; colorMap[0] = new System.Drawing.Imaging.ColorMap(); colorMap[0].OldColor = Color.White; colorMap[0].NewColor = Color.Transparent; imgAttrib.SetRemapTable( colorMap ); Graphics g = Graphics.FromImage( bmpResult ); g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //Rectangle rect = new Rectangle(0, 0, this.Width, this.Height); System.Drawing.Drawing2D.LinearGradientBrush lgBrush = new System.Drawing.Drawing2D.LinearGradientBrush( r, this._colorTop , this._colorDown , this._orientacion ); g.FillRectangle(lgBrush,r); g.DrawImage( bmpCaptured, r, 1 , 1, this.ClientRectangle.Width, this.ClientRectangle.Height, GraphicsUnit.Pixel, imgAttrib ); g.Dispose(); _pictureBox.Image = ( Image )bmpResult.Clone(); break; case win32.WM_HSCROLL: case win32.WM_VSCROLL: this.Invalidate(); // repaint // if you use scrolling then add these two case statements break; } }