发布时间:2017-09-22编辑:佚名阅读(1791)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; using System.Drawing.Text; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.Threading; namespace FontDemo { public partial class MarkImage : Form { //使用方法: //在软件根目录放水印图片,命名为“ddK.png” //在软件根目录下的img文件夹放要添加水印的图片 //在软件根目录下的imgbak文件夹为合成后输出文件夹 int i = 0; public MarkImage() { InitializeComponent(); } private void MarkImage_Load(object sender, EventArgs e) { } /// <summary> /// 保存图片 " 图片水印 " /// </summary> /// <param name="InUploadImagePath">原服务器图片路径</param> ///<param name="inLogoImagePath">生成的带图片水印的图片路径</param> ///<param name="InSavePath">水印图片路径</param> ///<param name="strType">原图片文件类型</param> public void MarkToImage(string InUploadImagePath, string inLogoImagePath, string InSavePath, string strType) { //FileStream fs = new FileStream(System.Windows.Forms.Application.StartupPath + @"\Img\" + InUploadImagePath, FileMode.Open, FileAccess.ReadWrite); //System.Drawing.Image img = System.Drawing.Image.FromStream(fs, false, false); //Bitmap bmp = new Bitmap(img); //img.Dispose(); //fs.Close(); //System.Drawing.Image Image = bmp; //System.Drawing.Image newimage = System.Drawing.Image.FromFile(System.Windows.Forms.Application.StartupPath + @"\" + inLogoImagePath); //Graphics g = Graphics.FromImage(Image); //g.DrawImage(newimage, new Rectangle(Image.Width - newimage.Width, Image.Height - newimage.Height, newimage.Width, newimage.Height), 0, 0, newimage.Width, newimage.Height, GraphicsUnit.Pixel); //System.Drawing.Image Image = System.Drawing.Image.FromFile(System.Windows.Forms.Application.StartupPath + @"\Img\" +InUploadImagePath); //System.Drawing.Image newimage = System.Drawing.Image.FromFile(System.Windows.Forms.Application.StartupPath + @"\" +inLogoImagePath); //Graphics g = Graphics.FromImage(Image); //g.DrawImage(newimage, new Rectangle(Image.Width - newimage.Width, Image.Height - newimage.Height, newimage.Width, newimage.Height), 0, 0, newimage.Width, newimage.Height, GraphicsUnit.Pixel); //g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //g.InterpolationMode = InterpolationMode.HighQualityBicubic; //g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; //Image.Save(System.Windows.Forms.Application.StartupPath + @"\ImgBak\" + InSavePath); //string pith = "C:\\Documents and Settings\\Administrator\\桌面\\临时目录\\IMg-09-07-21\\FontDemo\\FontDemo\\bin\\Debug\\ImgBak\\" + InSavePath; //Image.Save(pith); //System.Windows.Forms.Application.StartupPath = "C:\\Documents and Settings\\Administrator\\桌面\\临时目录\\IMg-09-07-21\\FontDemo\\FontDemo\\bin\\Debug" Bitmap imgTemp = new Bitmap(System.Windows.Forms.Application.StartupPath + @"\Img\" + InUploadImagePath);//创建Image对象,大小为200 * 200 Graphics gType = Graphics.FromImage(imgTemp);//在Image对象上创建Graphics对象g2 GraphicsPath typePath = new GraphicsPath(); Bitmap img = new Bitmap(System.Windows.Forms.Application.StartupPath + @"\" + inLogoImagePath);//创建Image对象,大小为200 * 200 try { //gType.DrawImage(img, 590, 1100, img.Width, img.Height); gType.DrawImage(img, new Rectangle(imgTemp.Width - img.Width, imgTemp.Height - img.Height, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel); string ps = System.Windows.Forms.Application.StartupPath + @"\ImgBak\" + InUploadImagePath; imgTemp.Save(ps, System.Drawing.Imaging.ImageFormat.Png); } catch (Exception de) { MessageBox.Show(de.Message); } finally { //MyItem item1 = new MyItem(); //item1.id = i.ToString(); //item1.name = InUploadImagePath + " - 已生成.."; //i++; //this.listBox1.Items.Add(item1); //this.pictureBox1.Image = imgTemp; typePath.Dispose(); imgTemp.Dispose(); img.Dispose(); //newimage.Dispose(); } } private void button1_Click(object sender, EventArgs e) { this.listBox1.Items.Clear(); MethodInvoker mi = new MethodInvoker(Add); mi.BeginInvoke(null, null); } public class MyItem : object { public string name; public string id; public override string ToString() { // TODO: 添加 MyItem.ToString 实现 return name; } } public delegate void delegateMethod(string fileName, Bitmap imgTem); public void Add() { DelegateChangeButton bu = new DelegateChangeButton(ButtonEnable); this.BeginInvoke(bu, new object[] { false }); string sd = AppDomain.CurrentDomain.BaseDirectory + "Img"; string[] files = Directory.GetFiles(sd); foreach (string f in files) { if (f.LastIndexOf("\\") != -1) { string strFile = f.Substring(f.LastIndexOf("\\") + 1); string strType = strFile.Substring(strFile.Length - 3, 3); if (strType.ToLower() == "bmp" || strType.ToLower() == "png" || strType.ToLower() == "jpg") { Bitmap imgTemp = new Bitmap(System.Windows.Forms.Application.StartupPath + @"\Img\" + strFile);//创建Image对象,大小为200 * 200 Graphics gType = Graphics.FromImage(imgTemp);//在Image对象上创建Graphics对象g2 GraphicsPath typePath = new GraphicsPath(); Bitmap img = new Bitmap(System.Windows.Forms.Application.StartupPath + @"\" + "ddK.png");//创建Image对象,大小为200 * 200 if (imgTemp.Width >= 400 && imgTemp.Height >= 150) { try { ////EncoderParameter p; ////EncoderParameters ps; ////ps = new EncoderParameters(1); ////p = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.CompressionLZW); ////ps.Param[0] = p; ////gType.DrawImage(img, new Rectangle(imgTemp.Width - img.Width, imgTemp.Height - img.Height, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel); ////gType.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; ////gType.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; ////gType.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; ////string pngB = System.Windows.Forms.Application.StartupPath + @"\ImgBak\" + strFile; ////imgTemp.Save(pngB, GetCodecInfo("image/bmp"), ps); //EncoderParameter p; //EncoderParameters ps; //ps = new EncoderParameters(1); ////p = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionRle); //long sdd = (long)EncoderValue.CompressionRle; //p = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.TransformRotate270); //ps.Param[0] = p; gType.DrawImage(img, new Rectangle(imgTemp.Width - img.Width, imgTemp.Height - img.Height, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel); //gType.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //gType.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //gType.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; string pngB = System.Windows.Forms.Application.StartupPath + @"\ImgBak\" + strFile; //imgTemp.Save(pngB, GetCodecInfo("image/jpeg"), ps); //imgTemp.Save(pngB, System.Drawing.Imaging.ImageFormat.Png); EncoderParameters encoderParams = new EncoderParameters(); long[] quality = new long[1]; quality[0] = 92; //压缩比例,决定图片大小的重要因素。 EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality); encoderParams.Param[0] = encoderParam; imgTemp.Save(System.Windows.Forms.Application.StartupPath + @"\ImgBak\" + strFile, GetCodecInfo("image/jpeg"), encoderParams); if (listBox1.InvokeRequired) { delegateMethod m = new delegateMethod(Referesh); this.BeginInvoke(m, new object[] { strFile, imgTemp }); } else { Referesh(strFile, imgTemp); } Thread.Sleep(100); //EncoderParameters encoderParams = new EncoderParameters(); //long[] quality = new long[1]; //quality[0] = 90; //EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality); //encoderParams.Param[0] = encoderParam; //Bitmap imgLast = new Bitmap(pngB);//创建Image对象 //imgLast.Save(System.Windows.Forms.Application.StartupPath + @"\images\" + strFile, GetCodecInfo("image/jpeg"), encoderParams); // SmallPic(pngB, System.Windows.Forms.Application.StartupPath + @"\images\" + strFile, imgTemp.Width, imgTemp.Height); } catch (Exception de) { MessageBox.Show(de.Message); } finally { typePath.Dispose(); //imgTemp.Dispose(); //img.Dispose(); } } } } } delegateMethodEND en = new delegateMethodEND(EndList); this.BeginInvoke(en); this.BeginInvoke(bu, new object[] { true }); } /**//// <summary> /// 保存JPG时用 /// </summary> /// <param name="mimeType"></param> /// <returns>得到指定mimeType的ImageCodecInfo</returns> private ImageCodecInfo GetCodecInfo(string mimeType) { ImageCodecInfo[] CodecInfo = ImageCodecInfo.GetImageEncoders(); foreach (ImageCodecInfo ici in CodecInfo) { if (ici.MimeType == mimeType) return ici; } return null; } public void ButtonEnable(bool flag) { this.button1.Enabled = flag; } public void Referesh(string fileName, Bitmap imgTem) { listBox1.Items.Add(DateTime.Now.ToShortTimeString() + @"分 - ImgBak\" + fileName + " 已生成"); listBox1.SelectedIndex = listBox1.Items.Count - 1; this.pictureBox1.Image = imgTem; Application.DoEvents(); } public void EndList() { listBox1.Items.Add(DateTime.Now.ToShortTimeString() + @"分 -网站所有水印图生成结束!"); listBox1.SelectedIndex = listBox1.Items.Count - 1; } public delegate void delegateMethodEND(); public delegate void DelegateChangeButton(bool flag); //public delegate void DelegateChangeIMG(Bitmap imgTem); public bool SmallPic(string strOldPic, string strNewPic, int widthEx, int heightEx) {//提供原始图片,目标路径,以及想要生成的缩略图尺寸 System.Drawing.Bitmap objPic, objNewPic; objPic = new System.Drawing.Bitmap(strOldPic); int width, height; width = objPic.Width; height = objPic.Height; if ((int)(height / heightEx) >= (int)(width / widthEx)) {//高度压缩比例大于宽度 SmallPicW(strOldPic, strNewPic, heightEx, widthEx); objPic.Dispose(); return true; } else {//高度压缩比例小于宽度 SmallPicH(strOldPic, strNewPic, widthEx, heightEx); objPic.Dispose(); return true; } } ///下面这个函数处理高度压缩比例小于宽度的情况 private void SmallPicH(string strOldPic, string strNewPic, int intWidth, int descHeight) { System.Drawing.Bitmap objPic, smallObjPic, objNewPic; objPic = null; objNewPic = null; smallObjPic = null; try { objPic = new System.Drawing.Bitmap(strOldPic); //将原始图片放入到内存里 int intHeight = (intWidth * objPic.Height) / objPic.Width; smallObjPic = new System.Drawing.Bitmap(objPic, intWidth, intHeight); objNewPic = new System.Drawing.Bitmap(intWidth, descHeight, PixelFormat.Format64bppPArgb); //在内存里生成一张跟缩略图尺寸一样的空图片。 objNewPic.SetResolution(objPic.HorizontalResolution, objPic.VerticalResolution); Graphics grPhoto = Graphics.FromImage(objNewPic); if (objPic.Width < intWidth) { grPhoto.FillRectangle(System.Drawing.Brushes.White, 0, 0, intWidth, descHeight); grPhoto.DrawImage(smallObjPic, new Rectangle((intWidth - objPic.Width) / 2, (descHeight - objPic.Height) / 2, objPic.Width, objPic.Height)); } else { grPhoto.FillRectangle(System.Drawing.Brushes.White, 0, 0, intWidth, descHeight); grPhoto.DrawImage(smallObjPic, new Rectangle(0, (descHeight - intHeight) / 2, intWidth, intHeight) , 0, 0, intWidth, intHeight, System.Drawing.GraphicsUnit.Pixel); } #region 保存图片 EncoderParameters encoderParams = new EncoderParameters(); long[] quality = new long[1]; quality[0] = 90; EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality); encoderParams.Param[0] = encoderParam; //获得包含对应的图片编解码程序 ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo jpegICI = null; for (int x = 0; x < arrayICI.Length; x++) { if (arrayICI[x].FormatDescription.Equals("JPEG")) { jpegICI = arrayICI[x];// break; } } if (jpegICI != null) { objNewPic.Save(strNewPic, jpegICI, encoderParams); } else { objNewPic.Save(strNewPic, System.Drawing.Imaging.ImageFormat.Jpeg); } objPic.Dispose(); objPic = null; objNewPic.Dispose(); objNewPic = null; smallObjPic.Dispose(); smallObjPic = null; #endregion } catch (Exception exp) { } finally { if (objPic != null) objPic.Dispose(); if (objNewPic != null) objNewPic.Dispose(); if (smallObjPic != null) smallObjPic.Dispose(); objPic = null; objNewPic = null; smallObjPic = null; } } ///以下函数是处理高度压缩比例大于宽度的情况 private void SmallPicW(string strOldPic, string strNewPic, int intHeight, int descWidth) { System.Drawing.Bitmap objPic, smallObjPic, objNewPic; objPic = null; objNewPic = null; smallObjPic = null; try { objPic = new System.Drawing.Bitmap(strOldPic); int intWidth = (intHeight * objPic.Width) / objPic.Height; smallObjPic = new System.Drawing.Bitmap(objPic, intWidth, intHeight); objNewPic = new System.Drawing.Bitmap(descWidth, intHeight, PixelFormat.Format64bppPArgb); objNewPic.SetResolution(objPic.HorizontalResolution, objPic.VerticalResolution); Graphics grPhoto = Graphics.FromImage(objNewPic); if (objPic.Height < intHeight) {// grPhoto.FillRectangle(System.Drawing.Brushes.White, 0, 0, descWidth, intHeight); Rectangle r = new Rectangle((descWidth - objPic.Width) / 2, (intHeight - objPic.Height) / 2, intWidth, objPic.Height); grPhoto.DrawImage(smallObjPic, r); } else { grPhoto.FillRectangle(System.Drawing.Brushes.White, 0, 0, descWidth, intHeight); grPhoto.DrawImage(smallObjPic, new Rectangle((descWidth - intWidth) / 2, 0, intWidth, intHeight), 0, 0, intWidth, intHeight, System.Drawing.GraphicsUnit.Pixel); } #region EncoderParameters encoderParams = new EncoderParameters(); long[] quality = new long[1]; quality[0] = 90; EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality); encoderParams.Param[0] = encoderParam; // ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo jpegICI = null; for (int x = 0; x < arrayICI.Length; x++) { if (arrayICI[x].FormatDescription.Equals("JPEG")) { jpegICI = arrayICI[x];// break; } } if (jpegICI != null) { objNewPic.Save(strNewPic, jpegICI, encoderParams); } else { objNewPic.Save(strNewPic, System.Drawing.Imaging.ImageFormat.Jpeg); } objPic.Dispose(); objPic = null; objNewPic.Dispose(); objNewPic = null; smallObjPic.Dispose(); smallObjPic = null; #endregion } catch (Exception exp) { throw exp; } finally { if (objPic != null) objPic.Dispose(); if (objNewPic != null) objNewPic.Dispose(); if (smallObjPic != null) smallObjPic.Dispose(); objPic = null; smallObjPic = null; objNewPic = null; } } } }
关键字: C# 水印 图片 合成 Graphics 压缩 图像质量
0人
0人
0人
0人