等比例图片缩放

 1  /// <summary>
 2         /// 按比例缩放图片
 3         /// </summary>
 4         /// <param name="directoryPath"></param>
 5         /// <param name="oldFileName"></param>
 6         /// <param name="newFileName"></param>
 7         /// <param name="zoom"></param>
 8         public static void ZoomPic(string directoryPath, string oldFileName, string newFileName, float zoom = 0.7f)
 9         {
10             if (Directory.Exists(directoryPath) == false)
11                 Directory.CreateDirectory(directoryPath);
12 
13             //图片路径
14             var filePath = Path.Combine(directoryPath, oldFileName);
15             try
16             {
17                 
18                 using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
19                 {
20                     System.Drawing.Image image = System.Drawing.Image.FromStream(fs);
21                     //重新定义宽和高
22                     int width = Convert.ToInt32(image.Width * zoom);
23                     int height = Convert.ToInt32(image.Height * zoom);
24                     //更新图片大小
25                     System.Drawing.Image newImg = new Bitmap(width, height);
26                     Graphics g = Graphics.FromImage(newImg);
27                     g.DrawImage(image, 0, 0, width, height);
28                     //保存新图片
29                     newImg.Save(Path.Combine(directoryPath, newFileName));
30                 }
31             }
32             finally
33             {
34                 if (File.Exists(filePath))
35                 {
36                     File.Delete(filePath);
37                 }
38             }
39         }

在此方法中,需要引用 using System.Drawing;

上一篇:Mysql事物原理及锁


下一篇:542,滑动窗口解最小覆盖子串