ASP.NET生成縮略圖失真非常厲害,如果圖像原文件為JPG格式的,可以通過以下程序優化!如果是其它格式的圖片可以在上傳時候保存為JPG格式的,詳情情參見第二段程序。
第一段:提高ASP.NET生成縮略圖質量(針對JPG)
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
---------------------------------------------------
第二段:根據上傳圖片重新生成新圖片
C#:
public string UploadImage(System.Web.HttpPostedFile postFile)
{
try
{
if(postFile.ContentLength==0)
{
throw(new Exception("NotExistedPostFile"));
}
else
if(postFile.ContentLength>Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["MaxImageSize"]))
{
throw(new Exception("TooLargePostFile"));
}
String strExt=System.IO.Path.GetExtension(postFile.FileName);
if(System.Configuration.ConfigurationSettings.AppSettings["ImageExtension"].IndexOf(";"+strExt+";")>-1)
{
throw(new Exception("ErrorImageExtension"));
}
String strFileName=@"/uploads/images/"+DateTime.Now.ToString("yyyyMMddHHmmssffff")+".jpg";
String strAbsoluteFileName=System.Web.HttpContext.Current.Server.MapPath(strFileName);
String Copyright=System.Configuration.ConfigurationSettings.AppSettings["CopyRight"];
System.Drawing.Image imgPhoto=System.Drawing.Image.FromStream(postFile.InputStream,true);
//取高和寬
int phWidth = imgPhoto.Width;
int phHeight =imgPhoto.Height;
//建新圖,指定格式為每像素 24 位;紅色、綠色和藍色分量各使用 8 位。
Bitmap bmPhoto = new Bitmap(phWidth, phHeight,PixelFormat.Format24bppRgb);
//設置分辨率
bmPhoto.SetResolution(imgPhoto.HorizontalResolution,imgPhoto.VerticalResolution);
//準備Graphics
Graphics grPhoto = Graphics.FromImage(bmPhoto);
try
{
//指定消除鋸齒的呈現。
grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
//拷貝原圖到做圖區
grPhoto.DrawImage(
imgPhoto,
new Rectangle(0, 0, phWidth, phHeight),
0,
0,
phWidth,
phHeight,
GraphicsUnit.Pixel);
//用指定Sizes繪制圖片時,測量用指定的字串寬度
//取可能的最大寬度
int[] sizes = new int[]{64,48,32,16,8,6,4};
Font crFont = null;
SizeF crSize = new SizeF();
for (int i=0 ;i<7; i++)
{
crFont = new Font("Verdana", sizes[i],
FontStyle.Bold);
crSize = grPhoto.MeasureString(Copyright,
crFont);
if((ushort)crSize.Width < (ushort)phWidth)
break;
}
//指定做圖點
int yPixlesFromBottom = (int)(phHeight *.05);
float yPosFromBottom = ((phHeight -
yPixlesFromBottom)-(crSize.Height/2));
float xCenterOfImg = (phWidth/2);
StringFormat StrFormat = new StringFormat();
StrFormat.Alignment = StringAlignment.Center;
//繪制copyright
SolidBrush semiTransBrush2 =
new SolidBrush(Color.FromArgb(100, 0, 0,0));
grPhoto.DrawString(Copyright,
crFont,
semiTransBrush2,
new PointF(xCenterOfImg+1,yPosFromBottom+1),
StrFormat);
SolidBrush semiTransBrush = new SolidBrush(
Color.FromArgb(100, 255, 255, 255));
grPhoto.DrawString(Copyright,
crFont,
semiTransBrush,
new PointF(xCenterOfImg,yPosFromBottom),
StrFormat);
bmPhoto.Save(strAbsoluteFileName,ImageFormat.Jpeg);
}
finally
{
grPhoto.Dispose();
bmPhoto.Dispose();
imgPhoto.Dispose();
}
return strFileName;
}
catch(Exception excep)
{
throw(excep);
}
}
VB.NET:
Public Function UploadImage(ByVal postFile As HttpPostedFile, ByVal savePath As String) As String
Dim fileName As String
Try
If (postFile.ContentLength = 0) Then
Throw New Exception("NotExistedPostFile")
End If
If (postFile.ContentLength > Convert.ToInt32(ConfigurationSettings.AppSettings.Item("MaxImageSize"))) Then
Throw New Exception("TooLargePostFile")
End If
Dim strExt As String = Path.GetExtension(postFile.FileName)
If (ConfigurationSettings.AppSettings.Item("ImageExtension").IndexOf((";" & strExt & ";")) > -1) Then
Throw New Exception("ErrorImageExtension")
End If
Dim strFileName As String = ("/uploads/images/" & DateTime.Now.ToString("yyyyMMddHHmmssffff") & ".jpg")
Dim strAbsoluteFileName As String = HttpContext.Current.Server.MapPath(strFileName)
Dim Copyright As String = ConfigurationSettings.AppSettings.Item("CopyRight")
Dim imgPhoto As System.Drawing.Image = System.Drawing.Image.FromStream(postFile.InputStream, True)
Dim phWidth As Integer = imgPhoto.Width
Dim phHeight As Integer = imgPhoto.Height
Dim bmPhoto As New Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb)
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution)
Dim grPhoto As Graphics = Graphics.FromImage(bmPhoto)
Try
Dim crSize As SizeF
grPhoto.SmoothingMode = SmoothingMode.AntiAlias
grPhoto.DrawImage(imgPhoto, New Rectangle(0, 0, phWidth, phHeight), 0, 0, phWidth, phHeight, GraphicsUnit.Pixel)
Dim sizes As Integer() = New Integer() {16, 14, 12, 10, 8, 6, 4}
Dim crFont As Font = Nothing
crSize = New SizeF
Dim i As Integer
For i = 0 To 6
crFont = New Font("Verdana", CType(sizes(i), Single), FontStyle.Bold)
crSize = grPhoto.MeasureString(Copyright, crFont)
If (CType(crSize.Width, Integer) < CType(phWidth, Integer)) Then
Exit For
End If
Next i
Dim yPixlesFromBottom As Integer = CType((phHeight * 0.05), Integer)
Dim yPosFromBottom As Single = ((phHeight - yPixlesFromBottom) - (crSize.Height / 2.0!))
Dim xCenterOfImg As Single = (phWidth / 2)
Dim StrFormat As New StringFormat
StrFormat.Alignment = StringAlignment.Center
Dim semiTranssemiTransBrush As New SolidBrush(Color.FromArgb(100, 0, 0, 0))
grPhoto.DrawString(Copyright, crFont, semiTranssemiTransBrush, New PointF((xCenterOfImg + 1.0!), (yPosFromBottom + 1.0!)), StrFormat)
Dim semiTransBrush As New SolidBrush(Color.FromArgb(100, 255, 255, 255))
grPhoto.DrawString(Copyright, crFont, semiTransBrush, New PointF(xCenterOfImg, yPosFromBottom), StrFormat)
'bmPhoto.Save(savePath, ImageFormat.Jpeg)
'處理圖像質量
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))
bmPhoto.Save(savePath, ici, ep)
Finally
grPhoto.Dispose()
bmPhoto.Dispose()
imgPhoto.Dispose()
End Try
fileName = strFileName
Catch exception1 As Exception
Throw exception1
End Try
Return fileName
End Function