网上搜集的一个比较实用的图片操作类

namespace System.Images.ImagesHelper
002 {
003     /**//// <summary>
004     ///ImgOperate 的摘要说明
005     /// </summary>
006     public class ImagesHelper
007     {
008         图片处理类构造方法#region 图片处理类构造方法
009         /**//// <summary>
010         /// 图片处理类构造方法
011         /// </summary>
012         public ImagesHelper()
013         {
014             //
015             //TODO: 在此处添加构造函数逻辑
016             //
017         }
018         #endregion
019         用于缩小图片#region 用于缩小图片
020         //************************************************************//
021         //下面给出三个简单的方法,后面两个方法是扩展
022         //************************************************************//
023         /**//// <summary>
024         ///用于缩小图片
025         /// </summary>
026         /// <param name="strOldPic">原始图片路径</param>
027         /// <param name="strNewPic">新图片路径</param>
028         /// <param name="intWidth">新图片宽度</param>
029         /// <param name="intHeight">新图片高度</param>
030         /// <param name="isDeleteOldPic">是否删除原始图片</param>
031         public void ReducePic(string strOldPic, string strNewPic, int intWidth, int intHeight, bool isDeleteOldPic)
032         {
033             System.Drawing.Bitmap objPic, objNewPic;
034             try
035             {
036                 objPic = new System.Drawing.Bitmap(strOldPic);
037                 objNewPic = new System.Drawing.Bitmap(objPic, intWidth, intHeight);
038                 objNewPic.Save(strNewPic);
039                 objPic.Dispose();
040                 objNewPic.Dispose();
041                 if (isDeleteOldPic)
042                 {
043                     if (File.Exists(strOldPic))
044                     {
045                         File.Delete(strOldPic);
046                     }
047                 }
048   
049             }
050             catch (Exception exp) { throw exp; }
051             finally
052             {
053                 objPic = null;
054                 objNewPic = null;
055             }
056         }
057         /**//// <summary>
058         /// 将图片缩放至指定类型指定值
059         /// </summary>
060         /// <param name="strOldPic">原始图片路径</param>
061         /// <param name="strNewPic">新图片路径</param>
062         /// <param name="intValue">指定值</param>
063         /// <param name="type">缩放类型(W/w为指定宽度,H/h为指定高度)</param>
064         /// <param name="isDeleteOldPic">是否删除原始图片</param>
065         public  void ReducePic(string strOldPic, string strNewPic, int intValue, string type, bool isDeleteOldPic)
066         {
067             System.Drawing.Bitmap objPic, objNewPic;
068             int intWidth = 0;
069             int intHeight = 0;
070             try
071             {
072                 objPic = new System.Drawing.Bitmap(strOldPic);
073                 if (type == "W" || type == "w")
074                 {
075                     intWidth = intValue;
076                     intHeight = Convert.ToInt32(((intWidth * 1.0) / (objPic.Width * 1.0)) * objPic.Height);
077                 }
078                 else if (type == "H" || type == "h")
079                 {
080                     intHeight = intValue;
081                     intWidth = Convert.ToInt32(((intHeight * 1.0) / (objPic.Height * 1.0)) * objPic.Width);
082                 }
083                 else
084                 {
085                     throw new Exception("未指定图片缩放类型");
086                 }
087                 objNewPic = new System.Drawing.Bitmap(objPic, intWidth, intHeight);
088                 objNewPic.Save(strNewPic);
089                 objPic.Dispose();
090                 objNewPic.Dispose();
091                 if (isDeleteOldPic)
092                 {
093                     if (File.Exists(strOldPic))
094                     {
095                         File.Delete(strOldPic);
096                     }
097                 }
098   
099             }
100             catch (Exception exp) { throw exp; }
101             finally
102             {
103                 objPic = null;
104                 objNewPic = null;
105             }
106         }
107         /**//// <summary>
108         /// 用于缩放图片
109         /// </summary>
110         /// <param name="strOldPic">原始图片路径</param>
111         /// <param name="strNewPic">新图片路径</param>
112         /// <param name="balance">缩放比例</param>
113         /// <param name="isDeleteOldPic">是否删除原始图片</param>
114         public  void ReducePic(string strOldPic, string strNewPic, double balance, bool isDeleteOldPic)
115         {
116             System.Drawing.Bitmap objPic, objNewPic;
117             try
118             {
119                 objPic = new System.Drawing.Bitmap(strOldPic);
120                 int intWidth = Convert.ToInt32((objPic.Width) * balance);
121                 int intHeight = Convert.ToInt32((objPic.Height) * balance);
122                 objNewPic = new System.Drawing.Bitmap(objPic, intWidth, intHeight);
123                 objNewPic.Save(strNewPic);
124                 objPic.Dispose();
125                 objNewPic.Dispose();
126                 if (isDeleteOldPic)
127                 {
128                     if (File.Exists(strOldPic))
129                     {
130                         File.Delete(strOldPic);
131                     }
132                 }
133   
134             }
135             catch (Exception exp) { throw exp; }
136             finally
137             {
138                 objPic = null;
139                 objNewPic = null;
140             }
141         }
142         /**//// <summary>
143         /// 获取图片指定部分
144         /// </summary>
145         /// <param name="pPath">图片路径</param>
146         /// <param name="pSavePath">保存路径</param>
147         /// <param name="pPartStartPointX">目标图片开始绘制处的坐标X值(通常为)</param>
148         /// <param name="pPartStartPointY">目标图片开始绘制处的坐标Y值(通常为)</param>
149         /// <param name="pPartWidth">目标图片的宽度</param>
150         /// <param name="pPartHeight">目标图片的高度</param>
151         /// <param name="pOrigStartPointX">原始图片开始截取处的坐标X值</param>
152         /// <param name="pOrigStartPointY">原始图片开始截取处的坐标Y值</param>
153         public void ReducePic(string pPath, string pSavedPath, int pPartStartPointX, int pPartStartPointY, int pPartWidth, int pPartHeight, int pOrigStartPointX, int pOrigStartPointY)
154         {
155             System.Drawing.Image originalImg = System.Drawing.Image.FromFile(pPath);
156             Bitmap partImg = new Bitmap(pPartWidth, pPartHeight);
157             Graphics graphics = Graphics.FromImage(partImg);
158             Rectangle destRect = new Rectangle(new Point(pPartStartPointX, pPartStartPointY), new Size(pPartWidth, pPartHeight));//目标位置
159             Rectangle origRect = new Rectangle(new Point(pOrigStartPointX, pOrigStartPointY), new Size(pPartWidth, pPartHeight));//原图位置(默认从原图中截取的图片大小等于目标图片的大小)
160             graphics.DrawImage(originalImg, destRect, origRect, GraphicsUnit.Pixel);
161             partImg.Save(pSavedPath, ImageFormat.Jpeg);
162         }
163         #endregion
164         添加水印文字#region 添加水印文字
165         /**//// <summary>
166         /// 为上传的图片添加水印文字
167         /// </summary>
168         /// <param name="Path">原始图片路径</param>
169         /// <param name="Path_sy">水印图片路径</param>
170         /// <param name="WaterText">水印文字</param>
171         public  void AddWaterText(string Path, string Path_sy, string WaterText)
172         {
173             string addText = WaterText;
174             System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
175             System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
176             g.DrawImage(image, 0, 0, image.Width, image.Height);
177             System.Drawing.Font f = new System.Drawing.Font("Verdana", 10);
178             System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.LightSlateGray);
179             g.DrawString(addText, f, b, image.Width - 100, image.Height - 20);
180             g.Dispose();
181             image.Save(Path_sy);
182             image.Dispose();
183         }
184         #endregion
185         为图片增加水印图片#region 为图片增加水印图片
186         /**//// <summary>
187         /// 在图片上生成图片水印
188         /// </summary>
189         /// <param name="Path">原服务器图片路径</param>
190         /// <param name="Path_syp">生成的带图片水印的图片路径</param>
191         /// <param name="Path_sypf">水印图片路径</param>
192         public  void AddWaterPic(string Path, string Path_syp, string Path_sypf)
193         {
194             System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
195             System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Path_sypf);
196             System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
197             g.DrawImage(copyImage, new System.Drawing.Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel);
198             g.Dispose();
199             image.Save(Path_syp);
200             image.Dispose();
201         }
202         #endregion
203         获取图片的高度和宽度#region 获取图片的高度和宽度
204         /**//// <summary>
205         /// 获取图片的高度和宽度
206         /// </summary>
207         /// <param name="imgurl">img图片的地址</param>
208         /// <returns></returns>
209         public int[] GetImgWidth(string imgurl)
210         {
211             System.Drawing.Image image;
212             image = System.Drawing.Image.FromFile(imgurl);
213             int imgwidth = image.Width;
214             int imgHeight = image.Height;
215             int[] img = { imgwidth, imgHeight };
216             return img;
217         }
218         #endregion
219         压缩图片#region 压缩图片
220         /**//// <summary>
221         /// 对图片进行压缩
222         /// </summary>
223         /// <param name="filePath">原始图片路径</param>
224         /// <param name="filePath_ystp">压缩后的图片路径</param>
225         public void DensityPic(string filePath, string filePath_ystp) //压缩图片 
226         {
227             Bitmap bmp = new Bitmap(filePath);
228             //ImageCoderInfo 
229             ImageCodecInfo ici = null;
230             //Encoder 
231             System.Drawing.Imaging.Encoder ecd = null;
232             //EncoderParameter 
233             EncoderParameter ept = null;
234             //EncoderParameters 
235             EncoderParameters eptS = null;
236             try
237             {
238                 ici = this.getImageCoderInfo("image/jpeg");
239                 ecd = System.Drawing.Imaging.Encoder.Quality;
240                 eptS = new EncoderParameters(1);
241                 ept = new EncoderParameter(ecd, 10L);
242                 eptS.Param[0] = ept;
243                 bmp.Save(filePath_ystp, ici, eptS);
244             
245   
246   
247             catch (Exception ex)
248             {
249                 throw new Exception(ex.Message);
250             }
251             finally
252             {
253                 bmp.Dispose();
254                 ept.Dispose();
255                 eptS.Dispose();
256             }
257         }
258         private ImageCodecInfo getImageCoderInfo(string coderType)// 获取图片编码类型信息 
259         {
260             ImageCodecInfo[] iciS = ImageCodecInfo.GetImageEncoders();
261             ImageCodecInfo retIci = null;
262             foreach (ImageCodecInfo ici in iciS)
263             {
264                 if (ici.MimeType.Equals(coderType))
265                     retIci = ici;
266             }
267   
268             return retIci;
269         }
270         #endregion
271         获取图片的指定部分#region 获取图片的指定部分
272         /**//**/
273         /**//// <summary>
274         /// 获取图片指定部分
275         /// </summary>
276         /// <param name="pPath">图片路径</param>
277         /// <param name="pSavePath">保存路径</param>
278         /// <param name="pPartStartPointX">目标图片开始绘制处的坐标X值(通常为)</param>
279         /// <param name="pPartStartPointY">目标图片开始绘制处的坐标Y值(通常为)</param>
280         /// <param name="pPartWidth">目标图片的宽度</param>
281         /// <param name="pPartHeight">目标图片的高度</param>
282         /// <param name="pOrigStartPointX">原始图片开始截取处的坐标X值</param>
283         /// <param name="pOrigStartPointY">原始图片开始截取处的坐标Y值</param>
284         /// <param name="pFormat">保存格式,通常可以是jpeg</param>
285         public void GetPart(string pPath, string pSavedPath, int pPartStartPointX, int pPartStartPointY, int pPartWidth, int pPartHeight, int pOrigStartPointX, int pOrigStartPointY)
286         {
287             string normalJpgPath = pSavedPath;
288   
289             using (System.Drawing.Image originalImg = System.Drawing.Image.FromFile(pPath))
290             {
291                 Bitmap partImg = new Bitmap(pPartWidth, pPartHeight);
292                 Graphics graphics = Graphics.FromImage(partImg);
293                 Rectangle destRect = new Rectangle(new Point(pPartStartPointX, pPartStartPointY), new Size(pPartWidth, pPartHeight));//目标位置
294                 Rectangle origRect = new Rectangle(new Point(pOrigStartPointX, pOrigStartPointY), new Size(pPartWidth, pPartHeight));//原图位置(默认从原图中截取的图片大小等于目标图片的大小)
295   
296   
297                 /**////文字水印  
298                 System.Drawing.Graphics G = System.Drawing.Graphics.FromImage(partImg);
299                 System.Drawing.Font f = new Font("Lucida Grande", 6);
300                 System.Drawing.Brush b = new SolidBrush(Color.Gray);
301                 G.Clear(Color.White);
302                 graphics.DrawImage(originalImg, destRect, origRect, GraphicsUnit.Pixel);
303                 G.DrawString("TianTang.Com", f, b, 0, 0);
304                 G.Dispose();
305   
306                 originalImg.Dispose();
307                 if (File.Exists(normalJpgPath))
308                 {
309                     File.SetAttributes(normalJpgPath, FileAttributes.Normal);
310                     File.Delete(normalJpgPath);
311                 }
312                 partImg.Save(normalJpgPath, ImageFormat.Jpeg);
313             }
314         }
315         #endregion
316         获取图片的指定部分#region 获取图片的指定部分
317         /**//**/
318         /**//// <summary>
319         /// 获取按比例缩放的图片指定部分
320         /// </summary>
321         /// <param name="pPath">图片路径</param>
322         /// <param name="pSavePath">保存路径</param>
323         /// <param name="pPartStartPointX">目标图片开始绘制处的坐标X值(通常为)</param>
324         /// <param name="pPartStartPointY">目标图片开始绘制处的坐标Y值(通常为)</param>
325         /// <param name="pPartWidth">目标图片的宽度</param>
326         /// <param name="pPartHeight">目标图片的高度</param>
327         /// <param name="pOrigStartPointX">原始图片开始截取处的坐标X值</param>
328         /// <param name="pOrigStartPointY">原始图片开始截取处的坐标Y值</param>
329         /// <param name="imageWidth">缩放后的宽度</param>
330         /// <param name="imageHeight">缩放后的高度</param>
331         public void GetPart(string pPath, string pSavedPath, int pPartStartPointX, int pPartStartPointY, int pPartWidth, int pPartHeight, int pOrigStartPointX, int pOrigStartPointY, int imageWidth, int imageHeight)
332         {
333             string normalJpgPath = pSavedPath;
334             using (System.Drawing.Image originalImg = System.Drawing.Image.FromFile(pPath))
335             {
336                 if (originalImg.Width == imageWidth && originalImg.Height == imageHeight)
337                 {
338                     GetPart(pPath, pSavedPath, pPartStartPointX, pPartStartPointY, pPartWidth, pPartHeight, pOrigStartPointX, pOrigStartPointY);
339                     return;
340                 }
341   
342                 System.Drawing.Image.GetThumbnailImageAbort callback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
343                 System.Drawing.Image zoomImg = originalImg.GetThumbnailImage(imageWidth, imageHeight, callback, IntPtr.Zero);//缩放
344                 Bitmap partImg = new Bitmap(pPartWidth, pPartHeight);
345   
346                 Graphics graphics = Graphics.FromImage(partImg);
347                 Rectangle destRect = new Rectangle(new Point(pPartStartPointX, pPartStartPointY), new Size(pPartWidth, pPartHeight));//目标位置
348                 Rectangle origRect = new Rectangle(new Point(pOrigStartPointX, pOrigStartPointY), new Size(pPartWidth, pPartHeight));//原图位置(默认从原图中截取的图片大小等于目标图片的大小)
349   
350                 /**////文字水印  
351                 System.Drawing.Graphics G = System.Drawing.Graphics.FromImage(partImg);
352                 System.Drawing.Font f = new Font("Lucida Grande", 6);
353                 System.Drawing.Brush b = new SolidBrush(Color.Gray);
354                 G.Clear(Color.White);
355   
356                 graphics.DrawImage(zoomImg, destRect, origRect, GraphicsUnit.Pixel);
357                 G.DrawString("TianTang.Com", f, b, 0, 0);
358                 G.Dispose();
359   
360                 originalImg.Dispose();
361                 if (File.Exists(normalJpgPath))
362                 {
363                     File.SetAttributes(normalJpgPath, FileAttributes.Normal);
364                     File.Delete(normalJpgPath);
365                 }
366                 partImg.Save(normalJpgPath, ImageFormat.Jpeg);
367             }
368         }
369         public bool ThumbnailCallback()
370         {
371             return false;
372         }
373         #endregion
374     }
375
}
阅读更多
个人分类: C#
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭