编程的时候经常用到图像的缩略图。
本文发布一个用于生成指定尺寸的缩略图的简单方法。
1 文本格式
private void button1_Click(object sender, EventArgs e)
{
CreateThumbnail(@"demo.jpg", @"demo_thumb.jpg", 128, 128);
}
private void CreateThumbnail(string imgFile, string thumbnailFile, int w, int h)
{
try
{
using (Image img = Image.FromFile(imgFile))
{
Image.GetThumbnailImageAbort callback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
img.GetThumbnailImage(w, h, callback, IntPtr.Zero).Save(thumbnailFile);
}
}
catch (Exception ex)
{
throw new Exception("Image to Thumbnail error!");
}
}
public bool ThumbnailCallback()
{
return false;
}
2 代码格式
private void button1_Click(object sender, EventArgs e)
{
CreateThumbnail(@"demo.jpg", @"demo_thumb.jpg", 128, 128);
}
private void CreateThumbnail(string imgFile, string thumbnailFile, int w, int h)
{
try
{
using (Image img = Image.FromFile(imgFile))
{
Image.GetThumbnailImageAbort callback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
img.GetThumbnailImage(w, h, callback, IntPtr.Zero).Save(thumbnailFile);
}
}
catch (Exception ex)
{
throw new Exception("Image to Thumbnail error!");
}
}
public bool ThumbnailCallback()
{
return false;
}