/// <summary>
/// 创建缩略图
/// </summary>
/// <param name="bigImgPath">大图在硬盘上的位置</param>
/// <param name="smallImgPath">缩略图在硬盘上的位置</param>
/// <param name="minW">生成的小图的宽</param>
/// <param name="minH">生成的小图的高</param>
private void CreateThumImg(string bigImgPath,string smallImgPath,int minW=300,int minH=300)
{
try
{
using (System.Drawing.Image img = System.Drawing.Image.FromFile(bigImgPath))
{
int srcWidth = img.Width;
int srcHeight = img.Height;
int thumWidth = minW;
int thumHeight = minH;
if ((decimal)srcWidth / srcHeight <= (decimal)thumWidth / thumHeight)
{
thumWidth = minW;
thumHeight = thumWidth * srcHeight / srcWidth;
}
else
{
thumHeight = minH;
thumWidth = thumHeight * srcWidth / srcHeight;
}
Bitmap bmp = new Bitmap(thumWidth, thumHeight);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp);
//高清
g.SmoothingMode = SmoothingMode.HighQuality;
//高质量
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.High;
g.Clear(Color.Black);
//重绘原始图
Rectangle rec = new Rectangle(0, 0, thumWidth, thumHeight);
g.DrawImage(img, rec, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel);
g.Dispose();
//保存图像
bmp.Save(smallImgPath);
//释放资源
bmp.Dispose();
img.Dispose();
}
}
catch (Exception ex)
{
}
}
.net生成高清缩略图备忘
最新推荐文章于 2018-10-08 20:21:45 发布