[C#] 一个自定义的C#图片处理类,用于生成缩略图信息 →→→→→进入此内容的聊天室

来自 , 2020-11-19, 写在 C#, 查看 128 次.
URL http://www.code666.cn/view/bc37e109
  1. using System;
  2. using System.Drawing.Imaging;
  3. using System.Collections;
  4. using System.Drawing;
  5. using System.IO;
  6.  
  7. namespace DotNet.Utilities
  8. {
  9.     /// <summary>
  10.     /// 枚举,生成缩略图模式
  11.     /// </summary>
  12.     public enum ThumbnailMod : byte
  13.     {
  14.         /// <summary>
  15.         /// HW
  16.         /// </summary>
  17.         HW,
  18.         /// <summary>
  19.         /// W
  20.         /// </summary>
  21.         W,
  22.         /// <summary>
  23.         /// H
  24.         /// </summary>
  25.         H,
  26.         /// <summary>
  27.         /// Cut
  28.         /// </summary>
  29.         Cut
  30.     };
  31.  
  32.     /// <summary>
  33.     /// 操作图片类, 生成缩略图,添加水印
  34.     /// </summary>
  35.     public static class PicDeal
  36.     {
  37.         private static Hashtable htmimes = new Hashtable();
  38.         internal static readonly string AllowExt = ".jpe|.jpeg|.jpg|.png|.tif|.tiff|.bmp";
  39.         #region 生成缩略图
  40.         /// <summary>
  41.         /// 生成缩略图
  42.         /// </summary>
  43.         /// <param name="originalImagePath"></param>
  44.         /// <param name="width"></param>
  45.         /// <param name="height"></param>
  46.         /// <param name="mode"></param>
  47.         /// <returns></returns>
  48.         public static bool MakeThumbnail(string originalImagePath, int width, int height, ThumbnailMod mode)
  49.         {
  50.             string thumbnailPath = originalImagePath.Substring(0, originalImagePath.LastIndexOf('.')) + "s.jpg";
  51.             Image originalImage = Image.FromFile(originalImagePath);
  52.  
  53.             int towidth = width;
  54.             int toheight = height;
  55.  
  56.             int x = 0;
  57.             int y = 0;
  58.             int ow = originalImage.Width;
  59.             int oh = originalImage.Height;
  60.  
  61.             switch (mode)
  62.             {
  63.                 case ThumbnailMod.HW://指定高宽缩放(可能变形)                
  64.                     break;
  65.                 case ThumbnailMod.W://指定宽,高按比例                    
  66.                     toheight = originalImage.Height * width / originalImage.Width;
  67.                     break;
  68.                 case ThumbnailMod.H://指定高,宽按比例
  69.                     towidth = originalImage.Width * height / originalImage.Height;
  70.                     break;
  71.                 case ThumbnailMod.Cut://指定高宽裁减(不变形)                
  72.                     if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
  73.                     {
  74.                         oh = originalImage.Height;
  75.                         ow = originalImage.Height * towidth / toheight;
  76.                         y = 0;
  77.                         x = (originalImage.Width - ow) / 2;
  78.                     }
  79.                     else
  80.                     {
  81.                         ow = originalImage.Width;
  82.                         oh = originalImage.Width * height / towidth;
  83.                         x = 0;
  84.                         y = (originalImage.Height - oh) / 2;
  85.                     }
  86.                     break;
  87.                 default:
  88.                     break;
  89.             }
  90.  
  91.             //新建一个bmp图片
  92.             Image bitmap = new Bitmap(towidth, toheight);
  93.  
  94.             //新建一个画板
  95.             Graphics g = Graphics.FromImage(bitmap);
  96.  
  97.             //设置高质量插值法
  98.             g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
  99.  
  100.             //设置高质量,低速度呈现平滑程度
  101.             g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  102.  
  103.             //清空画布并以透明背景色填充
  104.             g.Clear(System.Drawing.Color.Transparent);
  105.  
  106.             //在指定位置并且按指定大小绘制原图片的指定部分
  107.             g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
  108.                 new Rectangle(x, y, ow, oh),
  109.                 GraphicsUnit.Pixel);
  110.             bool isok = false;
  111.             try
  112.             {
  113.                 //以jpg格式保存缩略图
  114.                 bitmap.Save(thumbnailPath, ImageFormat.Jpeg);
  115.                 isok = true;
  116.             }
  117.             catch (Exception)
  118.             {
  119.                 thumbnailPath = originalImagePath;
  120.             }
  121.             finally
  122.             {
  123.                 originalImage.Dispose();
  124.                 bitmap.Dispose();
  125.                 g.Dispose();
  126.             }
  127.             return isok;
  128.         }
  129.         #endregion
  130.  
  131.         #region 在图片上生成图片水印
  132.         ///// <summary>
  133.         ///// 在图片上生成图片水印
  134.         ///// </summary>
  135.         ///// <param name="Path">原服务器图片路径</param>
  136.         ///// <param name="Path_syp">生成的带图片水印的图片路径</param>
  137.         ///// <param name="Path_sypf">水印图片路径</param>
  138.         /// <summary>
  139.         /// 在图片上生成图片水印
  140.         /// </summary>
  141.         /// <param name="Path">原服务器图片路径</param>
  142.         /// <param name="Path_sypf">水印图片路径</param>
  143.         public static void AddWaterPic(string Path, string Path_sypf)
  144.         {
  145.             try
  146.             {
  147.                 Image image = Image.FromFile(Path);
  148.                 Image copyImage = Image.FromFile(Path_sypf);
  149.                 Graphics g = Graphics.FromImage(image);
  150.                 g.DrawImage(copyImage, new System.Drawing.Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel);
  151.                 g.Dispose();
  152.  
  153.                 image.Save(Path + ".temp");
  154.                 image.Dispose();
  155.                 System.IO.File.Delete(Path);
  156.                 File.Move(Path + ".temp", Path);
  157.             }
  158.             catch
  159.             { }
  160.         }
  161.         #endregion
  162.  
  163.         /// <summary>
  164.         /// 公共方法
  165.         /// </summary>
  166.         private static void GetImgType()
  167.         {
  168.             htmimes[".jpe"] = "image/jpeg";
  169.             htmimes[".jpeg"] = "image/jpeg";
  170.             htmimes[".jpg"] = "image/jpeg";
  171.             htmimes[".png"] = "image/png";
  172.             htmimes[".tif"] = "image/tiff";
  173.             htmimes[".tiff"] = "image/tiff";
  174.             htmimes[".bmp"] = "image/bmp";
  175.         }
  176.  
  177.  
  178.         #region 返回新图片尺寸
  179.         /// <summary>
  180.         /// 返回新图片尺寸
  181.         /// </summary>
  182.         /// <param name="width">原始宽</param>
  183.         /// <param name="height">原始高</param>
  184.         /// <param name="maxWidth">新图片最大宽</param>
  185.         /// <param name="maxHeight">新图片最大高</param>
  186.         /// <returns></returns>
  187.         public static Size ResizeImage(int width, int height, int maxWidth, int maxHeight)
  188.         {
  189.             decimal MAX_WIDTH = (decimal)maxWidth;
  190.             decimal MAX_HEIGHT = (decimal)maxHeight;
  191.             decimal ASPECT_RATIO = MAX_WIDTH / MAX_HEIGHT;
  192.  
  193.             int newWidth, newHeight;
  194.  
  195.             decimal originalWidth = (decimal)width;
  196.             decimal originalHeight = (decimal)height;
  197.  
  198.             if (originalWidth > MAX_WIDTH || originalHeight > MAX_HEIGHT)
  199.             {
  200.                 decimal factor;
  201.                 // determine the largest factor
  202.                 if (originalWidth / originalHeight > ASPECT_RATIO)
  203.                 {
  204.                     factor = originalWidth / MAX_WIDTH;
  205.                     newWidth = Convert.ToInt32(originalWidth / factor);
  206.                     newHeight = Convert.ToInt32(originalHeight / factor);
  207.                 }
  208.                 else
  209.                 {
  210.                     factor = originalHeight / MAX_HEIGHT;
  211.                     newWidth = Convert.ToInt32(originalWidth / factor);
  212.                     newHeight = Convert.ToInt32(originalHeight / factor);
  213.                 }
  214.             }
  215.             else
  216.             {
  217.                 newWidth = width;
  218.                 newHeight = height;
  219.             }
  220.  
  221.             return new Size(newWidth, newHeight);
  222.  
  223.         }
  224.         #endregion
  225.     }
  226. }
  227.  
  228. //csharp/8584

回复 "一个自定义的C#图片处理类,用于生成缩略图信息"

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

captcha