星空网 > 软件开发 > ASP.net

C#图片辅助类,形成缩略图

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Drawing; 6 using System.Drawing.Imaging; 7  8 namespace Tools 9 { 10   /// <summary> 11   /// 缩略图创建模式 12   /// </summary> 13   public enum ScaleMode 14   { 15     /// <summary> 16     /// 指定高宽缩放(可能失真) 17     /// </summary> 18     Normal, 19  20     /// <summary> 21     /// 指定宽,高按比例 22     /// </summary> 23     Width, 24  25     /// <summary> 26     /// 指定高,宽按比例 27     /// </summary> 28     Height, 29  30     /// <summary> 31     /// 指定高宽裁减(不失真) 32     /// </summary> 33     Cut 34   } 35  36   public static class ImageHelper 37   { 38     /// <summary> 39     /// 生成缩略图 40     /// </summary> 41     /// <param name="originalImagePath">源图路径(物理路径)</param> 42     /// <param name="thumbnailPath">缩略图路径(物理路径)</param> 43     /// <param name="width">缩略图宽度</param> 44     /// <param name="height">缩略图高度</param> 45     /// <param name="mode">生成缩略图的方式</param>   46     public static void GenerateThumbnail(string originalImagePath, string thumbnailPath, int width, int height, ScaleMode mode) 47     { 48       Image originalImage = Image.FromFile(originalImagePath); 49  50       GenerateThumbnail(originalImage, thumbnailPath, width, height, mode); 51     } 52  53     /// <summary> 54     /// 生成缩略图 55     /// </summary> 56     /// <param name="originalImage">源图</param> 57     /// <param name="thumbnailPath">缩略图路径(物理路径)</param> 58     /// <param name="width">缩略图宽度</param> 59     /// <param name="height">缩略图高度</param> 60     /// <param name="mode">生成缩略图的方式</param>   61     public static void GenerateThumbnail(Image originalImage, string thumbnailPath, int width, int height, ScaleMode mode) 62     { 63       Image image = GenerateThumbnail(originalImage, width, height, mode); 64  65       image.Save(thumbnailPath, ImageFormat.Jpeg); 66  67       image.Dispose(); 68     } 69  70     /// <summary> 71     /// 生成缩略图 72     /// </summary> 73     /// <param name="originalImage">源图</param> 74     /// <param name="width">缩略图宽度</param> 75     /// <param name="height">缩略图高度</param> 76     /// <param name="mode">生成缩略图的方式</param>   77     public static Image GenerateThumbnail(Image originalImage, int width, int height, ScaleMode mode) 78     { 79       int towidth = width; 80       int toheight = height; 81  82       int x = 0; 83       int y = 0; 84       int ow = originalImage.Width; 85       int oh = originalImage.Height; 86  87       switch (mode) 88       { 89         default: 90         case ScaleMode.Normal: { break; } 91         case ScaleMode.Height: 92         { 93           toheight = originalImage.Height * width / originalImage.Width; 94           break; 95         } 96         case ScaleMode.Width: 97         { 98           towidth = originalImage.Width * height / originalImage.Height; 99           break;100         }101         case ScaleMode.Cut:102         {103           if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)104           {105             oh = originalImage.Height;106             ow = originalImage.Height * towidth / toheight;107             y = 0;108             x = (originalImage.Width - ow) / 2;109           }110           else111           {112             ow = originalImage.Width;113             oh = originalImage.Width * height / towidth;114             x = 0;115             y = (originalImage.Height - oh) / 2;116           }117           break;118         }119       }120 121       //新建一个bmp图片122       Image bitmap = new System.Drawing.Bitmap(towidth, toheight);123 124       //新建一个画板125       Graphics g = System.Drawing.Graphics.FromImage(bitmap);126 127       //设置高质量插值法128       g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;129 130       //设置高质量,低速度呈现平滑程度131       g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;132 133       //清空画布并以透明背景色填充134       g.Clear(Color.Transparent);135 136       //在指定位置并且按指定大小绘制原图片的指定部分137       g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),138         new Rectangle(x, y, ow, oh),139         GraphicsUnit.Pixel);140 141       try142       {143         //以jpg格式保存缩略图144         return bitmap;145       }146       catch (System.Exception e)147       {148         throw e;149       }150       finally151       {152         originalImage.Dispose();153         g.Dispose();154       }155     }156   }157 }

 




原标题:C#图片辅助类,形成缩略图

关键词:C#

C#
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

深度解读!亚马逊广告类型和运作类型:https://www.ikjzd.com/articles/102925
跨境贸易人民币结算的十年和未来发展趋势:https://www.ikjzd.com/articles/102926
跨境电商为什么要做独立站?独立站有哪些优势?:https://www.ikjzd.com/articles/102927
返校季选品第二弹,让销量来得更猛烈一些!:https://www.ikjzd.com/articles/102928
wish针对取消订单的罚款政策10月17日起生效:https://www.ikjzd.com/articles/102929
速卖通布局全球化,俄罗斯是重头戏!:https://www.ikjzd.com/articles/102932
你心目中的十大旅游景点有哪些?:https://www.vstour.cn/a/404234.html
万盛旅游景点分布图 万盛旅游景点介绍:https://www.vstour.cn/a/404235.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流