柔城

SOSOFT articles

.net又一个生成缩略图的方法,不变形

生成缩略图是一个十分常用功能,找到了一个方法,重写部分代码,实用又好用,.net又一个生成缩略图的方法,不变形

 

 1         /// <summary> 
 2         /// 为图片生成缩略图 by 何问起
 3         /// </summary> 
 4         /// <param name="phyPath">原图片的路径</param> 
 5         /// <param name="width">缩略图宽</param> 
 6         /// <param name="height">缩略图高</param> 
 7         /// <returns></returns> 
 8         public System.Drawing.Image GetHvtThumbnail(System.Drawing.Image image, int width, int height)
 9         {
10           //代码是从开源项目HoverTreeCMS中获取的
11           //更多信息请参考:http://hovertree.com/menu/hovertreecms/
12             Bitmap m_hovertreeBmp = new Bitmap(width, height);
13             //从Bitmap创建一个System.Drawing.Graphics 
14             Graphics m_HvtGr = Graphics.FromImage(m_hovertreeBmp);
15             //设置  
16             m_HvtGr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
17             //下面这个也设成高质量 
18             m_HvtGr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
19             //下面这个设成High 
20             m_HvtGr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
21             //把原始图像绘制成上面所设置宽高的缩小图 
22             Rectangle rectDestination = new Rectangle(0, 0, width, height);
23 
24             int m_width, m_height;
25             if (image.Width * height > image.Height * width)
26             {
27                 m_height = image.Height;
28                 m_width = (image.Height * width) / height;
29             }
30             else
31             {
32                 m_width = image.Width;
33                 m_height = (image.Width * height) / width;
34             }
35 
36             m_HvtGr.DrawImage(image, rectDestination, 0, 0, m_width, m_height, GraphicsUnit.Pixel);
37 
38             return m_hovertreeBmp;
39         }

 

 

需要引用命名空间:System.Drawing

转载自:http://hovertree.com/hvtart/bjae/9o2rqm6t.htm

ASP.NET开源CMS  http://www.cnblogs.com/sosoft/p/cms.html

开发技术文章收集 http://www.cnblogs.com/sosoft/p/kaifajishu.html

 

posted on 2015-11-26 03:42  柔城  阅读(2464)  评论(0编辑  收藏  举报

导航