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 { int i = 0; public MarkImage() { InitializeComponent(); } private void MarkImage_Load(object sender, EventArgs e) { } public void MarkToImage(string InUploadImagePath, string inLogoImagePath, string InSavePath, string strType) { Bitmap imgTemp = new Bitmap(System.Windows.Forms.Application.StartupPath + @"\Img\" + InUploadImagePath); Graphics gType = Graphics.FromImage(imgTemp); GraphicsPath typePath = new GraphicsPath(); Bitmap img = new Bitmap(System.Windows.Forms.Application.StartupPath + @"\" + inLogoImagePath); try { 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 { typePath.Dispose(); imgTemp.Dispose(); img.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() { 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); Graphics gType = Graphics.FromImage(imgTemp); GraphicsPath typePath = new GraphicsPath(); Bitmap img = new Bitmap(System.Windows.Forms.Application.StartupPath + @"\" + "ddK.png"); if (imgTemp.Width >= 400 && imgTemp.Height >= 150) { try { 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 pngB = System.Windows.Forms.Application.StartupPath + @"\ImgBak\" + strFile; 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); } catch (Exception de) { MessageBox.Show(de.Message); } finally { typePath.Dispose(); } } } } } delegateMethodEND en = new delegateMethodEND(EndList); this.BeginInvoke(en); this.BeginInvoke(bu, new object[] { true }); } /// <summary> 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 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, 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; } } } }
|