技术开发 频道

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


【IT168技术文档】

  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); } }
0
相关文章