码迷,mamicode.com
首页 > 其他好文 > 详细

等比例缩放图片

时间:2020-09-12 21:51:46      阅读:46      评论:0      收藏:0      [点我收藏+]

标签:params   idt   高度   param   tran   rom   div   uri   style   

 1  /// <summary>
 2         /// 等比例缩放图片
 3         /// </summary>
 4         /// <param name="bitmap">图片</param>
 5         /// <param name="destHeight">高度</param>
 6         /// <param name="destWidth">宽度</param>
 7         /// <returns></returns>
 8         private Bitmap ZoomImage(Bitmap bitmap, int destHeight, int destWidth)
 9         {
10             try
11             {
12                 System.Drawing.Image sourImage = bitmap;
13                 int width = 0, height = 0;
14                 //按比例缩放           
15                 int sourWidth = sourImage.Width;
16                 int sourHeight = sourImage.Height;
17                 if (sourHeight > destHeight || sourWidth > destWidth)
18                 {
19                     if ((sourWidth * destHeight) > (sourHeight * destWidth))
20                     {
21                         width = destWidth;
22                         height = (destWidth * sourHeight) / sourWidth;
23                     }
24                     else
25                     {
26                         height = destHeight;
27                         width = (sourWidth * destHeight) / sourHeight;
28                     }
29                 }
30                 else
31                 {
32                     width = sourWidth;
33                     height = sourHeight;
34                 }
35                 Bitmap destBitmap = new Bitmap(destWidth, destHeight);
36                 Graphics g = Graphics.FromImage(destBitmap);
37                 g.Clear(Color.Transparent);
38                 //设置画布的描绘质量         
39                 g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
40                 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
41                 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
42                 g.DrawImage(sourImage, new Rectangle((destWidth - width) / 2, (destHeight - height) / 2, width, height), 0, 0, sourImage.Width, sourImage.Height, GraphicsUnit.Pixel);
43                 g.Dispose();
44                 //设置压缩质量     
45                 System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters();
46                 long[] quality = new long[1];
47                 quality[0] = 100;
48                 System.Drawing.Imaging.EncoderParameter encoderParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
49                 encoderParams.Param[0] = encoderParam;
50                 sourImage.Dispose();
51                 return destBitmap;
52             }
53             catch
54             {
55                 return bitmap;
56             }
57         }

 

 

等比例缩放图片

标签:params   idt   高度   param   tran   rom   div   uri   style   

原文地址:https://www.cnblogs.com/s666/p/13594252.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!