// This article has www.blue1000.com translation, original address http://codebetter.com/blogs/brendan.tompkins/archive/2004/01/26/6103.aspx
// Respect the fruits of others' work. For details, please indicate the source.
GDI + is often used when writing a program. It can save a dark 32 BPP image as a GIF file, and the process is relatively simple. You can adjust the size of the GIF image before using the createthumnailimage method to save it.
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 plot and save GIF files, but soon you will find the problem: the quality of the generated thumnail.gif file is much lower than our expectation.
Slice:
As shown in, low-quality granular images also require "Color quantization" (palettization ). This problem occurs because the default 256 color is used by GDI +, and the actual color of the image is not taken into account.
Then, we tried to create our own "Palette", but the result was worse :). A good "Color quantization" algorithm should consider filling two pixel particles with a transitional color similar to the two pixels to provide more visible color space.
This is the "REE" algorithm. The "REE" algorithm allows us to insert our own algorithms to quantize our images.
Here are two Microsoft articles that may help us: KB 319061 and optimizing Color quantization for ASP. NET images (by Microsoft Morgan Skinner ). Morgan Skinner provides good "REE" algorithm code. You can download it for reference.
It is convenient to use octreequantizer:
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 film is as follows (is it more beautiful ?) :
Click here to download the project code and modify namespace to use it in your own project.