c#.net 生成清晰缩略图的解决方案
Bitmap类的GetThumbnailImage方法可以从指定的图像文件中生成缩略图,但是这种方式生成的缩略图清晰度不稳定,有时生成的缩略图的质量很差——模糊不清!
为什么会这样?为什么生成的图片是模糊的?
像JPEG这样格式的图像可能把缩略图存在同一个文件中。如果我们使用System.Drawing.Bitmap的GetThumbnailImage方法会检测文件中是否存在缩略图,如果找到该缩略图,它就会返回你所设定的宽高的缩略图版本。如果图像的缩略图的版本比你要求的大小要小,就会发生如下问题:生成的缩略图就会变得模糊,我们知道拉伸一个图像会导致图像质量的下降。
解决方案:
解决方法十分简单!我们利用System.Drawing.Image对象来装载源图像, 把图像缩放到你需要的比例,而又能保持高质量,接着保存就搞定了!
下面通过ASP .NET C#来演示代码片段:
//你应该包含这些名称空间
using System.Drawing;
using System.Drawing.Drawing2D
//下面开始
//本例中假定了两个变量:
String src="c:/myImages/a.jpg"; //源图像文件的绝对路径
String dest="c:/myImages/a_th.jpg"; //生成的缩略图图像文件的绝对路径
int thumbWidth=132; //要生成的缩略图的宽度
System.Drawing.Image image = System.Drawing.Image.FromFile(src); //利用Image对象装载源图像
//接着创建一个System.Drawing.Bitmap对象,并设置你希望的缩略图的宽度和高度。
int srcWidth=image.Width;
int srcHeight=image.Height;
int thumbHeight=(srcHeight/srcWidth)*thumbWidth;
Bitmap bmp = new Bitmap(thumbWidth, thumbHeight);
//从Bitmap创建一个System.Drawing.Graphics对象,用来绘制高质量的缩小图。
System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
//设置 System.Drawing.Graphics对象的SmoothingMode属性为HighQuality
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//下面这个也设成高质量
gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
//下面这个设成High
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//把原始图像绘制成上面所设置宽高的缩小图
System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, thumbWidth, thumbHeight);
gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel);
//保存图像,大功告成!
bmp.Save(dest);
//最后别忘了释放资源
bmp.Dispose();
image.Dispose();
using System.Drawing;
using System.Drawing.Drawing2D
//下面开始
//本例中假定了两个变量:
String src="c:/myImages/a.jpg"; //源图像文件的绝对路径
String dest="c:/myImages/a_th.jpg"; //生成的缩略图图像文件的绝对路径
int thumbWidth=132; //要生成的缩略图的宽度
System.Drawing.Image image = System.Drawing.Image.FromFile(src); //利用Image对象装载源图像
//接着创建一个System.Drawing.Bitmap对象,并设置你希望的缩略图的宽度和高度。
int srcWidth=image.Width;
int srcHeight=image.Height;
int thumbHeight=(srcHeight/srcWidth)*thumbWidth;
Bitmap bmp = new Bitmap(thumbWidth, thumbHeight);
//从Bitmap创建一个System.Drawing.Graphics对象,用来绘制高质量的缩小图。
System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
//设置 System.Drawing.Graphics对象的SmoothingMode属性为HighQuality
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//下面这个也设成高质量
gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
//下面这个设成High
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//把原始图像绘制成上面所设置宽高的缩小图
System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, thumbWidth, thumbHeight);
gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel);
//保存图像,大功告成!
bmp.Save(dest);
//最后别忘了释放资源
bmp.Dispose();
image.Dispose();
参考:
[记录]高品质图像保存代码
[C#]
/**//// <param name="fileName">图像名</param>
/// <param name="quality">品质</param>
public static void SaveImage(string fileName, int quality)
{
Bitmap myBitmap= new Bitmap(fileName);
System.Drawing.Imaging.EncoderParameters myEncoderParameters =
new System.Drawing.Imaging.EncoderParameters(1);
System.Drawing.Imaging.EncoderParameter myEncoderParameter =
new System.Drawing.Imaging.EncoderParameter(
System.Drawing.Imaging.Encoder.Quality, quality);
myEncoderParameters.Param[0] = myEncoderParameter;
System.Drawing.Imaging.ImageCodecInfo myImageCodecInfo;
myImageCodecInfo = GetEncoderInfo("image/jpeg");
string ext = myImageCodecInfo.FilenameExtension.Split(';')[0];
ext = System.IO.Path.GetExtension(ext).ToLower();
string saveName= System.IO.Path.ChangeExtension(fileName, ext);
//保存
myBitmap.Save(saveName, myImageCodecInfo, myEncoderParameters);
}
//获取MimeType
private static System.Drawing.Imaging.ImageCodecInfo
GetEncoderInfo(string mineType)
{
System.Drawing.Imaging.ImageCodecInfo [] myEncoders=
System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
foreach (System.Drawing.Imaging.ImageCodecInfo myEncoder in myEncoders)
if (myEncoder.MimeType == mineType)
return myEncoder;
return null;
}
[VB.NET]
Public Shared Sub SaveImage()Sub SaveImage(ByVal fileName As String, _
ByVal quality As Integer)
Dim myBitmap As Bitmap = New Bitmap(fileName)
Dim myEncoderParameters As _
New System.Drawing.Imaging.EncoderParameters(1)
Dim myEncoderParameter As _
New System.Drawing.Imaging.EncoderParameter( _
System.Drawing.Imaging.Encoder.Quality,quality)
myEncoderParameters.Param(0) = myEncoderParameter
Dim myImageCodecInfo As System.Drawing.Imaging.ImageCodecInfo
myImageCodecInfo = GetEncoderInfo("image/jpeg")
Dim ext As String = myImageCodecInfo.FilenameExtension.Split(";"c)(0)
ext = System.IO.Path.GetExtension(ext).ToLower()
Dim saveName As String = _
System.IO.Path.ChangeExtension(fileName, ext)
'保存
myBitmap.Save(saveName, myImageCodecInfo, myEncoderParameters)
End Sub
'获取MimeType
Private Shared Function GetEncoderInfo()Function GetEncoderInfo(ByVal mineType As String) _
As System.Drawing.Imaging.ImageCodecInfo
Dim myEncoders() As System.Drawing.Imaging.ImageCodecInfo = _
System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()
Dim myEncoder As System.Drawing.Imaging.ImageCodecInfo
For Each myEncoder In myEncoders
If myEncoder.MimeType = mineType Then
Return myEncoder
End If
Next
Return Nothing
End Function
[C#]
/**//// <param name="fileName">图像名</param>
/// <param name="quality">品质</param>
public static void SaveImage(string fileName, int quality)
{
Bitmap myBitmap= new Bitmap(fileName);
System.Drawing.Imaging.EncoderParameters myEncoderParameters =
new System.Drawing.Imaging.EncoderParameters(1);
System.Drawing.Imaging.EncoderParameter myEncoderParameter =
new System.Drawing.Imaging.EncoderParameter(
System.Drawing.Imaging.Encoder.Quality, quality);
myEncoderParameters.Param[0] = myEncoderParameter;
System.Drawing.Imaging.ImageCodecInfo myImageCodecInfo;
myImageCodecInfo = GetEncoderInfo("image/jpeg");
string ext = myImageCodecInfo.FilenameExtension.Split(';')[0];
ext = System.IO.Path.GetExtension(ext).ToLower();
string saveName= System.IO.Path.ChangeExtension(fileName, ext);
//保存
myBitmap.Save(saveName, myImageCodecInfo, myEncoderParameters);
}
//获取MimeType
private static System.Drawing.Imaging.ImageCodecInfo
GetEncoderInfo(string mineType)
{
System.Drawing.Imaging.ImageCodecInfo [] myEncoders=
System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
foreach (System.Drawing.Imaging.ImageCodecInfo myEncoder in myEncoders)
if (myEncoder.MimeType == mineType)
return myEncoder;
return null;
}
[VB.NET]
Public Shared Sub SaveImage()Sub SaveImage(ByVal fileName As String, _
ByVal quality As Integer)
Dim myBitmap As Bitmap = New Bitmap(fileName)
Dim myEncoderParameters As _
New System.Drawing.Imaging.EncoderParameters(1)
Dim myEncoderParameter As _
New System.Drawing.Imaging.EncoderParameter( _
System.Drawing.Imaging.Encoder.Quality,quality)
myEncoderParameters.Param(0) = myEncoderParameter
Dim myImageCodecInfo As System.Drawing.Imaging.ImageCodecInfo
myImageCodecInfo = GetEncoderInfo("image/jpeg")
Dim ext As String = myImageCodecInfo.FilenameExtension.Split(";"c)(0)
ext = System.IO.Path.GetExtension(ext).ToLower()
Dim saveName As String = _
System.IO.Path.ChangeExtension(fileName, ext)
'保存
myBitmap.Save(saveName, myImageCodecInfo, myEncoderParameters)
End Sub
'获取MimeType
Private Shared Function GetEncoderInfo()Function GetEncoderInfo(ByVal mineType As String) _
As System.Drawing.Imaging.ImageCodecInfo
Dim myEncoders() As System.Drawing.Imaging.ImageCodecInfo = _
System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()
Dim myEncoder As System.Drawing.Imaging.ImageCodecInfo
For Each myEncoder In myEncoders
If myEncoder.MimeType = mineType Then
Return myEncoder
End If
Next
Return Nothing
End Function