提高ASP.NET生成缩略图质量

2010-08-28 10:52:10来源:西部e网作者:

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

关键词:ASP.NET

赞助商链接: