[C#] c#实现图片生成缩略图 →→→→→进入此内容的聊天室

来自 , 2019-03-13, 写在 C#, 查看 125 次.
URL http://www.code666.cn/view/651468b3
  1.     private void MakeThumbnail(string sourcePath, string newPath, int width, int height)
  2.     {
  3.         System.Drawing.Image ig = System.Drawing.Image.FromFile(sourcePath);
  4.         int towidth = width;
  5.         int toheight = height;
  6.         int x = 0;
  7.         int y = 0;
  8.         int ow = ig.Width;
  9.         int oh = ig.Height;
  10.         if ((double)ig.Width / (double)ig.Height > (double)towidth / (double)toheight)
  11.         {
  12.             oh = ig.Height;
  13.             ow = ig.Height * towidth / toheight;
  14.             y = 0;
  15.             x = (ig.Width - ow) / 2;
  16.  
  17.         }
  18.         else
  19.         {
  20.             ow = ig.Width;
  21.             oh = ig.Width * height / towidth;
  22.             x = 0;
  23.             y = (ig.Height - oh) / 2;
  24.         }
  25.         System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
  26.         System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
  27.         g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
  28.         g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  29.         g.Clear(System.Drawing.Color.Transparent);
  30.         g.DrawImage(ig, new System.Drawing.Rectangle(0, 0, towidth, toheight), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel);
  31.         try
  32.         {
  33.             bitmap.Save(newPath, System.Drawing.Imaging.ImageFormat.Jpeg);
  34.         }
  35.         catch (Exception ex)
  36.         {
  37.             throw ex;
  38.         }
  39.         finally
  40.         {
  41.             ig.Dispose();
  42.             bitmap.Dispose();
  43.             g.Dispose();
  44.         }
  45.  
  46.     }
  47. //csharp/6081

回复 "c#实现图片生成缩略图"

这儿你可以回复上面这条便签

captcha