采用下面的方法可以得到质量很高的缩略图:
using System.Drawing;
using System.Drawing.Design;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
/// <summary>
/// 产生缩略图
/// </summary>
/// <param name="original_image_file">源文件</param>
/// <param name="object_width">缩略图宽度</param>
/// <param name="object_height">缩略图高度</param>
public static void GetThumbnailImage(string original_image_file, int object_width, int object_height)
{
int actual_width = 0;
int actual_heigh = 0;
string outputfilename = original_image_file + ".jpg";
System.Drawing.Bitmap original_image = new Bitmap(original_image_file);//读取源文件
actual_width = original_image.Width;
actual_heigh = original_image.Height;
Bitmap img = new Bitmap(object_width, object_height);
img.SetResolution(180f, 180f);
Graphics gdiobj = Graphics.FromImage(img);
gdiobj.CompositingQuality = CompositingQuality.HighQuality;
gdiobj.SmoothingMode = SmoothingMode.HighQuality;
gdiobj.InterpolationMode = InterpolationMode.HighQualityBicubic;
gdiobj.PixelOffsetMode = PixelOffsetMode.HighQuality;
gdiobj.FillRectangle(new SolidBrush(Color.White), 0, 0,object_width, object_height);
Rectangle destrect = new Rectangle(0, 0,object_width, object_height);
gdiobj.DrawImage(original_image, destrect, 0, 0, actual_width,actual_heigh, GraphicsUnit.Pixel);
System.Drawing.Imaging.EncoderParameters ep = new System.Drawing.Imaging.EncoderParameters(1);
ep.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)100);
System.Drawing.Imaging.ImageCodecInfo ici = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()[0];
if (ici != null)
{
img.Save(outputfilename, ici, ep);
}
else
{
img.Save(outputfilename, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}