图片缩放函数

如需转载请注明出处。
 

 1 ///<summary>
2 /// 图片缩放函数
3 ///</summary>
4 private static Bitmap Zoom(string imagePath, string imageName, int requiredWidth, int requiredHeight)
5 {
6 try
7 {
8 using (System.Drawing.Image OriginalImage = System.Drawing.Image.FromFile(Path.Combine(imagePath, imageName)))
9 {
10 int max = Math.Max(OriginalImage.Width, OriginalImage.Height);
11 double prec = (double)requiredWidth / max;
12 int width = (int)(OriginalImage.Width * prec);
13 int height = (int)(OriginalImage.Height * prec);
14
15 System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(width, height);
16 bmp.SetResolution(OriginalImage.HorizontalResolution, OriginalImage.VerticalResolution);
17 System.Drawing.Graphics Graphic = System.Drawing.Graphics.FromImage(bmp);
18
19 Graphic.SmoothingMode = SmoothingMode.AntiAlias;
20 Graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
21 Graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
22 Graphic.DrawImage(OriginalImage, new System.Drawing.Rectangle(0, 0, width, height));
23 Graphic.Dispose();
24
25 OriginalImage.Dispose();
26
27 return bmp;
28 }
29 }
30 catch (Exception Ex)
31 {
32 throw (Ex);
33 }
34 }

 

你可能感兴趣的:(图片缩放)