Use GDI + in. NET to improve the saving quality of GIF pictures __.net

Source: Internet
Author: User

This article has www.blue1000.com translation, the original address http://codebetter.com/blogs/brendan.tompkins/archive/2004/01/26/6103.aspx
Respect others labor achievements, reprint please indicate the source.

When writing a program often use GDI +, he can save a dark-bpp image as a GIF file, the process is relatively simple. And you can resize it before you save the GIF image using the Createthumnailimage method.
Common code:
System.Drawing.Bitmap B = new System.Drawing.Bitmap ("C://original_image.gif");
System.Drawing.Image Thmbnail = b.getthumbnailimage (100,75,null,new IntPtr ());
Thmbnail. Save ("C://thumnail.gif", System.Drawing.Imaging.ImageFormat.Gif);

The above code can complete the drawing and saving of GIF files, but soon you will find the problem: The resulting thumnail.gif files are well below our expectations.
Effect Picture:


A "color quantization" process (palettization) is also required for the low quality granular image shown above. This occurs because GDI + uses 256 colors by default, without taking into account the actual color of the image itself.

After that, we tried to create our own "palette", but the result was even worse:. A good "color quantization" algorithm should consider populating two pixel particles with a gradient color similar to those of the two pixels, providing more visual color space.
This is the "Octree" algorithm. The "Octree" algorithm allows us to insert our own algorithms to quantized our images.

Here are two articles from Microsoft that might help: KB 319061 and optimizing Color quantization for asp.net Images (Microsoft Morgan Skinner). Morgan Skinner provides a good "octree" algorithm code that you can download for reference use.

The use of Octreequantizer is very convenient:

System.Drawing.Bitmap B = new System.Drawing.Bitmap ("C://original_image.gif");
System.Drawing.Image Thmbnail = b.getthumbnailimage (100,75,null,new IntPtr ());
Octreequantizer Quantizer = new Octreequantizer (255, 8);
using (Bitmap quantized = Quantizer. Quantize (Thmbnail))
{
quantized. Save ("C://thumnail.gif", System.Drawing.Imaging.ImageFormat.Gif);
}

Octreequantizer Grayquantizer = new Grayscalequantizer ();
using (Bitmap quantized = Grayquantizer. Quantize (Thmbnail))
{
quantized. Save ("C://thumnail.gif", System.Drawing.Imaging.ImageFormat.Gif);
}

The effect picture is as follows (not much prettier.) ):

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.