ASP.NET生成缩略图失真非常厉害,如果图像原文件为JPG格式的,可以通过以下程序优化!!!如果是其它格式的图片可以在上传时候保存为JPG格式的,详情情参见 http://blog.csdn.net/rheleven/archive/2005/03/21/325540.aspx
C#版本:
private void MakeSLT(string oldImagePath,string newImagePath)
{
//oldImagePath -原图地址 newImagePath 生成缩略图地址
int width = 150;//缩略图的宽度
int height = 112;// 缩略图的高度
int level = 100; //缩略图的质量 1-100的范围
System.Drawing.Image oldimage = System.Drawing.Image.FromFile(oldImagePath);
System.Drawing.Image thumbnailImage = oldimage.GetThumbnailImage(width, height,new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
Bitmap bm=new Bitmap(thumbnailImage);
//处理JPG质量的函数
ImageCodecInfo[] codecs=ImageCodecInfo.GetImageEncoders();
ImageCodecInfo ici=null;
foreach(ImageCodecInfo codec in codecs)
{
if(codec.MimeType=="image/jpeg")
ici=codec;
}
EncoderParameters ep=new EncoderParameters();
ep.Param[0]=new EncoderParameter(Encoder.Quality,(long)level);
bm.Save(newImagePath,ici,ep);
}
VB.NET版本:
Sub makeSLT(ByVal oldImagePath As String,ByVal newImagePath As String)
Dim oimg As System.Drawing.Image = System.Drawing.Image.FromFile(oldImagePath)
Dim nimg As System.Drawing.Image = oimg.GetThumbnailImage(wids, heis, Nothing, IntPtr.Zero)
Response.Clear()
Dim outs As Bitmap = New Bitmap(nimg)
处理图像质量
Dim codecs As ImageCodecInfo() = ImageCodecInfo.GetImageEncoders()
Dim ici As ImageCodecInfo = System.DBNull
For Each codec As ImageCodecInfo In codecs
If codec.MimeType = "image/jpeg" Then
ici = codec
End If
Next
Dim ep As EncoderParameters = New EncoderParameters
ep.Param(0) = New EncoderParameter(System.Drawing.Imaging.Encoder.Quality, CLng(100))
outs.Save(newImagePath, ici, ep)
End Sub