Original article: http://www.thebrainparasite.com/post/Creating-great-thumbnails-in-ASPNET.aspx
Using the built-in functions of ASP. NET to create thumbnails is very convenient and easy to implement.
Int width = 190;
Int height = 190;
Bitmap source = new Bitmap ("c: \ someimage.gif ");
System. Drawing. Image thumb = source. GetThumbnailImage (width, height, null, IntPtr. Zero );
(31.7 k)
The trouble is that it produces relatively poor thumbnails and the generated files are too large. Thumbnails generated by this method tend to look very dirty, which may be good enough for what you need in many cases.
One choice
One of my common methods is to use the System. Drawing. Graphics Library to re-draw images. This is very simple, but it has very good results. The following is an example function for generating thumbnails.
Public static Bitmap CreateThumbnail (Bitmap source, int thumbWi, int thumbHi, bool maintainAspect)
{
// Return the source image if it's smaller than the designated thumbnail
If (source. Width <thumbWi & source. Height <thumbHi) return source;
System. Drawing. Bitmap ret = null;
Try
{
Int wi, hi;
Wi = thumbWi;
Hi = thumbHi;
If (maintainAspect)
{
// Maintain the aspect ratio despite the thumbnail size parameters
If (source. Width> source. Height)
{
Wi = thumbWi;
Hi = (int) (source. Height * (decimal) thumbWi/source. Width ));
}
Else
{
Hi = thumbHi;
Wi = (int) (source. Width * (decimal) thumbHi/source. Height ));
}
}
// Original code that creates lousy thumbnails
// System. Drawing. Image ret = source. GetThumbnailImage (wi, hi, null, IntPtr. Zero );
Ret = new Bitmap (wi, hi );
Using (Graphics g = Graphics. FromImage (ret ))
{
G. InterpolationMode = System. Drawing. Drawing2D. InterpolationMode. HighQualityBicubic;
G. FillRectangle (Brushes. White, 0, 0, wi, hi );
G. DrawImage (source, 0, 0, wi, hi );
}
}
Catch
{
Ret = null;
}
Return ret;
}
(10.5 k)
This function is very convenient, because one of its parameters is used to identify whether the dimension maintains the aspect ratio of the image when a thumbnail is generated. This thumbnail magic occurs in the following code:
Using (Graphics g = Graphics. FromImage (ret ))
{
G. InterpolationMode = System. Drawing. Drawing2D. InterpolationMode. HighQualityBicubic;
G. FillRectangle (Brushes. White, 0, 0, wi, hi );
G. DrawImage (source, 0, 0, wi, hi );
}
This method is a little slow but difficult to discard. It can be seen from the comparison of the following illustration:
(31.7 k) (10.5 k)
I would like to say that this is an excellent implementation of both file and image quality, ......
We can do better.
Now, we can mix JPEG compression into the real optimization results. I don't want to pretend that I fully understand how the JPEG compression code works, but it does turn around.
// Configure JPEG Compression Engine
System. Drawing. Imaging. EncoderParameters encoderParams = new System. Drawing. Imaging. EncoderParameters ();
Long [] quality = new long [1];
Quality [0] = 75;
System. Drawing. Imaging. EncoderParameter encoderParam = new System. Drawing. Imaging. EncoderParameter (System. Drawing. Imaging. Encoder. Quality, quality );
EncoderParams. Param [0] = encoderParam;
System. Drawing. Imaging. ImageCodecInfo [] arrayICI = System. Drawing. Imaging. ImageCodecInfo. GetImageEncoders ();
System. Drawing. Imaging. ImageCodecInfo paiici = null;
For (int x = 0; x <arrayICI. Length; x ++)
{
If (arrayICI [x]. FormatDescription. Equals ("JPEG "))
{
JpegICI = arrayICI [x];
Break;
}
}
This Code sets the encoderParameters parameter required to save the compressed thumbnail, and the value of quality [0] sets the compression level, I have successfully set a value as low as 40 for some applications. When the quality requirement is high, I found that setting it to 75 is very good. Using this code, you can use the JPEG compression algorithm before generating a thumbnail. When saving the image, use encoderParamaters as the parameter. For example:
System. Drawing. Image myThumbnail = CreateThumbnail (myBitmap, Width, Height, false );
// Configure JPEG Compression Engine
System. Drawing. Imaging. EncoderParameters encoderParams = new System. Drawing. Imaging. EncoderParameters ();
Long [] quality = new long [1];
Quality [0] = 75;
System. Drawing. Imaging. EncoderParameter encoderParam = new System. Drawing. Imaging. EncoderParameter (System. Drawing. Imaging. Encoder. Quality, quality );
EncoderParams. Param [0] = encoderParam;
System. Drawing. Imaging. ImageCodecInfo [] arrayICI = System. Drawing. Imaging. ImageCodecInfo. GetImageEncoders ();
System. Drawing. Imaging. ImageCodecInfo paiici = null;
For (int x = 0; x <arrayICI. Length; x ++)
{
If (arrayICI [x]. FormatDescription. Equals ("JPEG "))
{
JpegICI = arrayICI [x];
Break;
}
}
MyThumbnail. Save (Path. Combine (SavePathThumb, fileName), jpegICI, encoderParams );
MyThumbnail. Dispose ();
(2.39 k)
The size of 2.39 kb still looks great
Conclusion and Comparison
This is the comparison between the maximum size and minimum size of the last three thumbnails.
Maximum = 31.7 k
Uncompressed repainting = 10.5 k (67% smaller)
Compressed repainting = 2.39 k (92% smaller)
Difficult to discard. the source code of the thumbnail generation function and JPEG compression is below:
ThumbnailGenerator. cs (1.97 kb)
JPEGCompressionConfig. cs (969.00 bytes)