导航

yaosansi's Blog

当你背向太阳的时候,你只会看到自己的阴影!能力是有限的,努力无限的!

« Warning the User when Caps Lock is On (当大写锁定键打开时警告用户)Master-Detail with the GridView, DetailsView and ModalPopup Controls »

Creating great thumbnails in ASP.NET(在ASP.NET中生成高质量缩略图)

  • 本站大部分内容从网上收集,收集目的仅供研究、学习。涉及版权或不希望收录您的文章请您及时与我联系。
  • 本站IM群,请自行选择。请各位朋友按照自己喜好加入。加入群后请及时发言,防止被清理。谢谢您的合作!!!
  • QQ群:Y①WEB开发(ASP.NET)号码:7351660 QQ群:Y②WEB开发(ASP+.NET)号码:11864905
  • QQ群:Y③WEB开发(DIV+CSS)号码:16610506 QQ群:Y④WEB开发(JS+AJAX)号码:16143998
  • QQ群:Y⑤WEB开发(新手)号码:12777715 MSN群:yaosansi[at]126.com

原文:http://www.thebrainparasite.com/post/Creating-great-thumbnails-in-ASPNET.aspx

The built in function for creating thumbnails in ASP.NET is extremely convenient and very simple to implement.

 

 original (31.7k)

The trouble is that it produces relatively poor quality results and excessively large file sizes. The thumbnails tend to look very muddy when using this route, but many times it's good enough for whatever your needs may be.

An Alternative

The alternative that I use regularly involves redrawing the image using the System.Drawing.Graphics library. It is very simple to implement but produces superior results if for no other reason than its file size. The following is the standard function I use for creating thumbnails.

 

aG8klKbxhE-h43x2_tLGsw (10.5k)

This function is handy because it's parameters include a flag for maintaining the aspect ratio of the image along with the thumbnail size you would like. The thumbnail magic happens in this portion of the code:

 
  1. using (Graphics g = Graphics.FromImage(ret))   
  2. {   
  3.     g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;   
  4.     g.FillRectangle(Brushes.White, 0, 0, wi, hi);   
  5.     g.DrawImage(source, 0, 0, wi, hi);   
  6. }  

This method is slightly slower but the results are hard to ignore as illustrated by the comparison below:

(31.7k)original aG8klKbxhE-h43x2_tLGsw (10.5k)

Pretty nice improvement in both file size and quality I would say, but....

We can do even better

Now we can add into the mix some JPEG compression and really optimize the results. I won't pretend to fully understand how the JPEG compression code below works, but it sure does the trick.

 
  1. //Configure JPEG Compression Engine   
  2. System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters();   
  3. long[] quality = new long[1];   
  4. quality[0] = 75;   
  5. System.Drawing.Imaging.EncoderParameter encoderParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);   
  6. encoderParams.Param[0] = encoderParam;   
  7.   
  8. System.Drawing.Imaging.ImageCodecInfo[] arrayICI = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();   
  9. System.Drawing.Imaging.ImageCodecInfo jpegICI = null;   
  10. for (int x = 0; x < arrayICI.Length; x++)   
  11. {   
  12.     if (arrayICI[x].FormatDescription.Equals("JPEG"))   
  13.     {   
  14.         jpegICI = arrayICI[x];   
  15.         break;   
  16.     }   
  17. }  

This code will set up the encoderParameters needed for saving the new compressed thumbnail. The quality[0] value is where you set the compression level. I've had success going as low as a value of 40 for some applications, but when quality is a major requirement I find 75 to do very well. To use this engine you would execute the JPEG Compression code before you save your thumbnail, then use its encoderParamaters as a parameter when saving. For example:

 
  1. System.Drawing.Image myThumbnail = CreateThumbnail(myBitmap,Width,Height,false);                   
  2.   
  3. //Configure JPEG Compression Engine   
  4.                System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters();   
  5.                long[] quality = new long[1];   
  6.                quality[0] = 75;   
  7.                System.Drawing.Imaging.EncoderParameter encoderParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);   
  8.                encoderParams.Param[0] = encoderParam;   
  9.   
  10.                System.Drawing.Imaging.ImageCodecInfo[] arrayICI = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();   
  11.                System.Drawing.Imaging.ImageCodecInfo jpegICI = null;   
  12.                for (int x = 0; x < arrayICI.Length; x++)   
  13.                {   
  14.                    if (arrayICI[x].FormatDescription.Equals("JPEG"))   
  15.                    {   
  16.                        jpegICI = arrayICI[x];   
  17.                        break;   
  18.                    }   
  19.                }   
  20.   
  21. myThumbnail.Save(Path.Combine(SavePathThumb, fileName), jpegICI, encoderParams);   
  22.                myThumbnail.Dispose();  

compressed (2.39k)

Which still looks pretty darn good for 2.39k.

Conclusion and final comparison

Here is the final comparison between the 3 thumbnails going from largest file size to smallest:

original aG8klKbxhE-h43x2_tLGsw compressed

Largest = 31.7k

Uncompressed redraw = 10.5k (67% smaller)

Compressed redraw = 2.39k (92% smaller)

It's hard to ignore those results. The source code for the thumbnail function and the JPEG compression engine are below.

ThumbnailGenerator.cs (1.97 kb)

JPEGCompressionConfig.cs (969.00 bytes)

  1. public static Bitmap CreateThumbnail(Bitmap source, int thumbWi, int thumbHi, bool maintainAspect)   
  2.         {   
  3.             // return the source image if it's smaller than the designated thumbnail   
  4.             if (source.Width < thumbWi && source.Height < thumbHi) return source;   
  5.   
  6.             System.Drawing.Bitmap ret = null;   
  7.             try  
  8.             {   
  9.                 int wi, hi;   
  10.   
  11.                 wi = thumbWi;   
  12.                 hi = thumbHi;   
  13.   
  14.                 if (maintainAspect)   
  15.                 {   
  16.                     // maintain the aspect ratio despite the thumbnail size parameters   
  17.                     if (source.Width > source.Height)   
  18.                     {   
  19.                         wi = thumbWi;   
  20.                         hi = (int)(source.Height * ((decimal)thumbWi / source.Width));   
  21.                     }   
  22.                     else  
  23.                     {   
  24.                         hi = thumbHi;   
  25.                         wi = (int)(source.Width * ((decimal)thumbHi / source.Height));   
  26.                     }   
  27.                 }   
  28.   
  29.                 // original code that creates lousy thumbnails   
  30.                 // System.Drawing.Image ret = source.GetThumbnailImage(wi,hi,null,IntPtr.Zero);   
  31.                 ret = new Bitmap(wi, hi);   
  32.                 using (Graphics g = Graphics.FromImage(ret))   
  33.                 {   
  34.                     g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;   
  35.                     g.FillRectangle(Brushes.White, 0, 0, wi, hi);   
  36.                     g.DrawImage(source, 0, 0, wi, hi);   
  37.                 }   
  38.             }   
  39.             catch  
  40.             {   
  41.                 ret = null;   
  42.             }   
  43.   
  44.             return ret;   
  45.         }  

  1. int width = 190;   
  2. int height = 190;   
  3. Bitmap source = new Bitmap("c:\someimage.gif");   
  4. System.Drawing.Image thumb = source.GetThumbnailImage(width,height,null,IntPtr.Zero);  

原创文章如转载,请注明:转载自http://www.yaosansi.com
订阅本站,阅读更多文章

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

Powered By Z-Blog .Theme from Google黑板报 By Washun

Copyright 2005-2008 yaosansi'site All Rights Reserved.

感谢Denny·G 为本站提供FTP空间
辽ICP备05021434号

Search

  •  

赞助商广告

控制面板

最新评论及回复

最近发表