C# 图片裁剪

C# 图片裁剪

分类: c# DataBase 1559人阅读 评论(3) 收藏 举报
[c-sharp] view plaincopy
  1. /// <summary>  
  2. /// 缩小裁剪图片  
  3. /// </summary>  
  4. /// <param name="int_Width">要缩小裁剪图片宽度</param>  
  5. /// <param name="int_Height">要缩小裁剪图片长度</param>  
  6. /// <param name="input_ImgUrl">要处理图片路径</param>  
  7. /// <param name="out_ImgUrl">处理完毕图片路径</param>  
  8. public void ImgReduceCutOut(int int_Width, int int_Height, string input_ImgUrl, string out_ImgUrl)  
  9. {  
  10.     // ===上传标准图大小===  
  11.     int int_Standard_Width = 160;  
  12.     int int_Standard_Height = 160;  
  13.   
  14.     int Reduce_Width = 0; // 缩小的宽度  
  15.     int Reduce_Height = 0; // 缩小的高度  
  16.     int CutOut_Width = 0; // 裁剪的宽度  
  17.     int CutOut_Height = 0; // 裁剪的高度  
  18.     int level = 100; //缩略图的质量 1-100的范围  
  19.   
  20.     // ===获得缩小,裁剪大小===  
  21.     if (int_Standard_Height * int_Width / int_Standard_Width > int_Height)  
  22.     {  
  23.         Reduce_Width = int_Width;  
  24.         Reduce_Height = int_Standard_Height * int_Width / int_Standard_Width;  
  25.         CutOut_Width = int_Width;  
  26.         CutOut_Height = int_Height;  
  27.     }  
  28.     else if (int_Standard_Height * int_Width / int_Standard_Width < int_Height)  
  29.     {  
  30.         Reduce_Width = int_Standard_Width * int_Height / int_Standard_Height;  
  31.         Reduce_Height = int_Height;  
  32.         CutOut_Width = int_Width;  
  33.         CutOut_Height = int_Height;  
  34.     }  
  35.     else  
  36.     {  
  37.         Reduce_Width = int_Width;  
  38.         Reduce_Height = int_Height;  
  39.         CutOut_Width = int_Width;  
  40.         CutOut_Height = int_Height;  
  41.     }  
  42.   
  43.     // ===通过连接创建Image对象===  
  44.     System.Drawing.Image oldimage = System.Drawing.Image.FromFile(Server.MapPath(input_ImgUrl));  
  45.   
  46.     // ===缩小图片===  
  47.     System.Drawing.Image thumbnailImage = oldimage.GetThumbnailImage(Reduce_Width, Reduce_Height, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);  
  48.     Bitmap bm = new Bitmap(thumbnailImage);  
  49.   
  50.     // ===处理JPG质量的函数===  
  51.     ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();  
  52.     ImageCodecInfo ici = null;  
  53.     foreach (ImageCodecInfo codec in codecs)  
  54.     {  
  55.         if (codec.MimeType == "image/jpeg")  
  56.             ici = codec;  
  57.     }  
  58.     EncoderParameters ep = new EncoderParameters();  
  59.     ep.Param[0] = new EncoderParameter(Encoder.Quality, (long)level);  
  60.   
  61.     //bm.Save(Server.MapPath("2.jpg"), ici, ep);  
  62.   
  63.     // ===裁剪图片===  
  64.     Rectangle cloneRect = new Rectangle(0, 0, CutOut_Width, CutOut_Height);  
  65.     PixelFormat format = bm.PixelFormat;  
  66.     Bitmap cloneBitmap = bm.Clone(cloneRect, format);  
  67.   
  68.     // ===保存图片===  
  69.     cloneBitmap.Save(Server.MapPath(out_ImgUrl), ici, ep);  
原文地址:https://www.cnblogs.com/xianyin05/p/3071140.html