C # Save bitmap or image as a clear GIF

Source: Internet
Author: User

In C #, bitmap can be saved as GIF by default, but the GIF stored in this method will be seriously distorted.

Under normal circumstancesCode:

1 System. Drawing. Bitmap B =   New System. Drawing. Bitmap ("C: \ original_image.gif");
2 System. Drawing. Image thmbnail = B. getthumbnailimage ( 100 , 75 , Null , New Intptr ());

3 Thmbnail. Save ("C: \ thumnail.gif", system. Drawing. imaging. imageformat. GIF ); 

 

Today, I am going to write a software program for batch processing images, including various processing methods and effects. However, when I saved the images as GIF, I encountered a problem and checked it online for a long time.

There is no way to improve the quality of GIF images. Many people are asking this question, but few people have answered the question correctly.

I finally found a solution to reduce the size of the saved GIF, but the effect basically conforms to the regular

The method in this example is "REE".Algorithm. The "REE" algorithm allows us to insert our own algorithms to quantize our images. 

A good "Color quantization" Algorithm You should consider filling in a transitional color that is similar to the color of these two pixels between two pixel particles to provide more visible color space.

Morgan Skinner provides good "REE" algorithm code. You can download it for reference.

It is convenient to use octreequantizer:

1 System. Drawing. Bitmap B =   New System. Drawing. Bitmap ("C: \ original_image.gif");
2 System. Drawing. Image thmbnail = B. getthumbnailimage ( 100 , 75 , Null , New Intptr ());
3 Octreequantizer Quantizer =   New Octreequantizer ( 255 , 8 );
4 Using (Bitmap quantized = Quantizer. quantize (thmbnail ))
5 {
6 Quantized. Save ("C: \ thumnail.gif", system. Drawing. imaging. imageformat. GIF );
7 }
8 Octreequantizer grayquantizer =   New Grayscalequantizer ();
9 Using (Bitmap quantized = Grayquantizer. quantize (thmbnail ))
10 {
11 Quantized. Save ("C: \ thumnail.gif", system. Drawing. imaging. imageformat. GIF );

12 } 

You can click here to download the class file (project file). According to my trial, only two class files (octreequantizer. CS, quantizer. CS). Change the namespace of these two class files

The name of your project is enough. You also need to compile the project in unsafe compilation mode. Right-click the project name and select "allow Insecure code" in the Generation tab.

 

 

Related Article

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.