[翻译]Asp.net生成高质量缩略图

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

使用ASP.NET内置的功能创建缩略图是非常方便和容易实现的。

[转]Asp.net生成高质量缩略图int width = 190;
[转]Asp.net生成高质量缩略图
int height = 190;
[转]Asp.net生成高质量缩略图Bitmap source 
= new Bitmap("c:\someimage.gif");
[转]Asp.net生成高质量缩略图System.Drawing.Image thumb 
= source.GetThumbnailImage(width,height,null,IntPtr.Zero);

[转]Asp.net生成高质量缩略图 (31.7k)

麻烦的是,它生产的缩略图质量相对较差且生成的文件过大。该方法生成的缩略图往往看起来非常污浊,或许很多情况下这对你所需要的来说已经足够好。

一个选择

我常用的一个方法是使用 System.Drawing.Graphics 库 重画图像,这实现起来非常简单,但是却有非常好的效果。下面是我用来生成缩略图的一个示例函数

[转]Asp.net生成高质量缩略图public static Bitmap CreateThumbnail(Bitmap source, int thumbWi, int thumbHi, bool maintainAspect)
        }

[转]Asp.net生成高质量缩略图 (10.5k)

这个函数是很方便的,因为的它的一个参数用来标识是否维在生成缩略图的时候维持图像的长宽比。这个缩略图魔法发生在下面的代码中

[转]Asp.net生成高质量缩略图                using (Graphics g = Graphics.FromImage(ret))
                }

这个方法有点慢但是却让人难以舍弃,从下面的插图的对比可见:

(31.7k)[转]Asp.net生成高质量缩略图 [转]Asp.net生成高质量缩略图 (10.5k)

我想说这是文件和图像质量都非常棒的实现,但是……

我们可以做的更好

现在我门可以混入 JPEG压缩来真正的优化结果, 我不想假装完全了解JPEG压缩的代码如何工作的,但它确实扭转了乾坤。

[转]Asp.net生成高质量缩略图                //Configure JPEG Compression Engine
[转]Asp.net生成高质量缩略图
                System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters();
[转]Asp.net生成高质量缩略图                
long[] quality = new long[1];
[转]Asp.net生成高质量缩略图                quality[
0= 75;
[转]Asp.net生成高质量缩略图                System.Drawing.Imaging.EncoderParameter encoderParam 
= new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
[转]Asp.net生成高质量缩略图                encoderParams.Param[
0= encoderParam;
[转]Asp.net生成高质量缩略图
[转]Asp.net生成高质量缩略图                System.Drawing.Imaging.ImageCodecInfo[] arrayICI 
= System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
[转]Asp.net生成高质量缩略图                System.Drawing.Imaging.ImageCodecInfo jpegICI 
= null;
[转]Asp.net生成高质量缩略图                
for (int x = 0; x < arrayICI.Length; x++)
                }

这段代码设置了保存经过压缩的缩略图所需的 encoderParameters 参数,quality[0] 的值设定压缩的级别,我曾经针对某些应用设置像40这么低的值也成功了,俺是当质量要求比较高的时候 ,我发现设置为75非常好。使用这段代码你能够在生成缩略图之前使用 JPEG 压缩算法,当保存的时候把 encoderParamaters 作为参数,例如:

[转]Asp.net生成高质量缩略图    System.Drawing.Image myThumbnail = CreateThumbnail(myBitmap,Width,Height,false);                
[转]Asp.net生成高质量缩略图
[转]Asp.net生成高质量缩略图    
//Configure JPEG Compression Engine
[转]Asp.net生成高质量缩略图
                System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters();
[转]Asp.net生成高质量缩略图                
long[] quality = new long[1];
[转]Asp.net生成高质量缩略图                quality[
0= 75;
[转]Asp.net生成高质量缩略图                System.Drawing.Imaging.EncoderParameter encoderParam 
= new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
[转]Asp.net生成高质量缩略图                encoderParams.Param[
0= encoderParam;
[转]Asp.net生成高质量缩略图
[转]Asp.net生成高质量缩略图                System.Drawing.Imaging.ImageCodecInfo[] arrayICI 
= System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
[转]Asp.net生成高质量缩略图                System.Drawing.Imaging.ImageCodecInfo jpegICI 
= null;
[转]Asp.net生成高质量缩略图                
for (int x = 0; x < arrayICI.Length; x++)
                myThumbnail.Dispose();

[转]Asp.net生成高质量缩略图 (2.39k)

2.39K 大小 看起来依然很棒

结论和最后比较

这是最后三张缩略图从文件大小最大到最小的效果对比

[转]Asp.net生成高质量缩略图 [转]Asp.net生成高质量缩略图 [转]Asp.net生成高质量缩略图

最大 = 31.7k

未压缩重绘 = 10.5k (67% smaller)

压缩重绘 = 2.39k (92% smaller)

难以舍弃的结果啊,缩略图生成函数和JPEG压缩的源代码 在下边 :

ThumbnailGenerator.cs (1.97 kb)

JPEGCompressionConfig.cs (969.00 bytes)

相关文章:

  • 2022-12-23
  • 2022-02-10
  • 2021-10-08
  • 2021-12-12
  • 2021-09-26
  • 2021-07-04
  • 2021-05-18
猜你喜欢
  • 2021-06-10
  • 2021-06-11
  • 2022-01-06
  • 2021-11-26
  • 2021-10-02
相关资源
相似解决方案