生成缩略图是一个十分常用功能,找到了一个方法,重写部分代码,实用又好用,.net又一个生成缩略图的方法,不变形
/// <summary>
/// 为图片生成缩略图 by 何问起
/// </summary>
/// <param name="phyPath">原图片的路径</param>
/// <param name="width">缩略图宽</param>
/// <param name="height">缩略图高</param>
/// <returns></returns>
public System.Drawing.Image GetHvtThumbnail(System.Drawing.Image image, int width, int height)
{
//代码是从开源项目HoverTreeCMS中获取的
//更多信息请参考:http://hovertree.com/menu/hovertreecms/
Bitmap m_hovertreeBmp = new Bitmap(width, height);
//从Bitmap创建一个System.Drawing.Graphics
Graphics m_HvtGr = Graphics.FromImage(m_hovertreeBmp);
//设置
m_HvtGr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//下面这个也设成高质量
m_HvtGr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
//下面这个设成High
m_HvtGr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
//把原始图像绘制成上面所设置宽高的缩小图
Rectangle rectDestination = new Rectangle(, , width, height); int m_width, m_height;
if (image.Width * height > image.Height * width)
{
m_height = image.Height;
m_width = (image.Height * width) / height;
}
else
{
m_width = image.Width;
m_height = (image.Width * height) / width;
} m_HvtGr.DrawImage(image, rectDestination, , , m_width, m_height, GraphicsUnit.Pixel); return m_hovertreeBmp;
}
需要引用命名空间:System.Drawing
转载自:http://hovertree.com/hvtart/bjae/9o2rqm6t.htm