谈到获取缩略图,C#的Image类提供了一个GetThumbnailImage的方法获取,但是我试了一下~得到的缩略图相当模糊~
那怎么获取高清的呢?查了MSDN,拼命死锁后得到这样一个启示:缩略图不就是原图按比例缩小,那我们将原图读入按比例缩放后再在画布上重绘一个不就可以了么?
OK~思路有了~那我们就试试~
下面先贴出Image.GetThumbnailImage的方法:
/// <summary>
/// 获取源图像的缩略图
/// </summary>
/// <param name="Percent">设置原图的缩略比</param>
/// <param name="OriginFilePath">原图的路径</param>
/// <param name="TargetFilePath">要保存的缩略图的路径</param>
/// <returns>表明此次操作是否成功</returns>
private bool GetMicroImage(double Percent,string OriginFilePath, string TargetFilePath)
{
try
{
Bitmap bt = new Bitmap(120,120); //创建Bitmap实例
Graphics g = Graphics.FromImage(bt); //创建Graphics实例
g.Clear(Color.White); //设置画布背景颜色为白色
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
Image OrginImage = Image.FromFile(OriginFilePath); //获取源图像的Image实例
Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback); //定义委托
int ImageWidth = Convert.ToInt32(OrginImage.Width * Percent); //设置高度
int ImageHeight = Convert.ToInt32(OrginImage.Height * Percent); //设置宽度
Image MicroImage = OrginImage.GetThumbnailImage(ImageWidth, ImageWidth, callb, IntPtr.Zero); //获取源图像的缩略图
if (ImageWidth > ImageHeight) //如果原图宽度大于高度
{
g.DrawImage(MicroImage, 0, (int)(120 - ImageHeight) / 2, ImageWidth, ImageHeight); //缩放图片
}
else
{
g.DrawImage(MicroImage, (int)(120 - ImageWidth) / 2, 0, ImageWidth, ImageHeight);
}
g.DrawRectangle(new Pen(Color.Gray), 0, 0, 119, 119); //绘制缩略图的边框
bt.Save(TargetFilePath + @"/" + this.OriginFileName.ToString() + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg); //保存缩略图
this.OriginFileName++;
bt.Dispose(); //释放对象
MicroImage.Dispose();
OrginImage.Dispose();
return true;
}
catch(Exception e)
{
MessageBox.Show("错误信息:" + e.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
然后贴出我写的:
/// <summary>
/// 高质量缩放图片
/// </summary>
/// <param name="OriginFilePath">源图的路径</param>
/// <param name="TargetFilePath">存储缩略图的路径</param>
/// <param name="DestWidth">缩放后图片宽度</param>
/// <param name="DestHeight">缩放后图片高度</param>
/// <returns>表明此次操作是否成功</returns>
private bool GetMicroImage(string OriginFilePath, string TargetFilePath,int DestWidth,int DestHeight)
{
Image OriginImage = Image.FromFile(OriginFilePath);
System.Drawing.Imaging.ImageFormat thisFormat = OriginImage.RawFormat;
//按比例缩放
int sW = 0, sH = 0;
int ImageWidth = OriginImage.Width;
int ImageHeight = OriginImage.Height;
if (ImageWidth > DestWidth || ImageHeight > DestHeight)
{
if ((ImageWidth * DestWidth) > (ImageHeight * DestHeight))
{
sW = DestWidth;
sH = (DestHeight * ImageHeight) / ImageWidth;
}
else
{
sH = DestHeight;
sW = (DestWidth * ImageWidth) / ImageHeight;
}
}
else
{
sW = ImageWidth;
sH = ImageHeight;
}
Bitmap bt = new Bitmap(DestWidth,DestHeight); //根据指定大小创建Bitmap实例
using(Graphics g = Graphics.FromImage(bt))
{
g.Clear(Color.White);
//设置画布的描绘质量
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(OriginImage,new Rectangle((DestWidth - sW) / 2,(DestHeight - sH) / 2,sW,sH));
g.Dispose();
}
System.Drawing.Imaging.EncoderParameters EncoderParams = new System.Drawing.Imaging.EncoderParameters(); //取得内置的编码器
long[] Quality = new long[1];
Quality[0] = 100;
System.Drawing.Imaging.EncoderParameter EncoderParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality,Quality);
EncoderParams.Param[0] = EncoderParam;
try
{
//获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象
System.Drawing.Imaging.ImageCodecInfo[] arrayICI = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
System.Drawing.Imaging.ImageCodecInfo jpegICI = null;
for (int i = 0; i < arrayICI.Length; i++)
{
if (arrayICI[i].FormatDescription.Equals("JPEG"))
{
jpegICI = arrayICI[i]; //设置为JPEG编码方式
break;
}
}
if (jpegICI != null) //保存缩略图
{
bt.Save(TargetFilePath + @"/" + this.OriginFileName.ToString() + ".jpg", jpegICI, EncoderParams);
}
else
{
bt.Save(TargetFilePath + @"/" + this.OriginFileName.ToString() + ".jpg", thisFormat);
}
this.OriginFileName++;
OriginImage.Dispose();
return true;
}
catch (System.Runtime.InteropServices.ExternalException e1) //GDI+发生一般错误
{
MessageBox.Show("错误信息:" + e1.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
catch (Exception e2)
{
MessageBox.Show("错误信息:" + e2.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}