Use GDI + to draw high-quality images and fonts (2)

Source: Internet
Author: User

I still remember an article from the past.ArticleI wrote "using GDI + to draw high-quality graphs and fonts" and used smoothingmode. highquality, compositingquality. highquality, textrenderinghint. cleartypegridfit and other attributes improve the quality of the generated image .. however, when I generated an image in the unit "Web dashboard" last week, I encountered a small problem, that is, when I generated a JPEG image, the default quality was blurred, no matter how you adjust the attribute, it cannot achieve satisfactory results .. check msdn. find a solution and find a new Taoyuan resort ..

The location of the problem is filtered. It is determined that the problem occurs when the image is saved, bitmap. Save ();, according to the generalProgramThe process requires a simple bitmap. save (string path, imageformat. however, the default value cannot meet our needs. the default JPEG storage quality should be around 60. at this time, we need to modify the default value .. the modification method is to set and define the imagecodecinfo class and encoderparameters class. The specificCodeAs follows: The imagecodecinfo class specifies the format to be saved, and encoderparameters is the set array of the encoderparameter class. system is used in the encoderparameter class. drawing. imaging. the encoder class is used to specify the mode and parameters to be presented, such as slice quality parameters, scan method parameters, color table parameters, compression parameters, and color depth. at this point, you should understand the steps and methods for modifying the image quality. mainly for system. drawing. imaging. encoder settings, and then use system. drawing. imaging. encoder constructs the encoderparameter class and passes the encoderparameter to the encoderparameters array to get an image that fits your needs! The starting code is as follows:
First introduce:
Using system;
Using system. drawing;
Using system. Drawing. imaging;

1
2 Bitmap mybitmap; // Create bitmap
3 Imagecodecinfo myimagecodecinfo;
4 Encoder myencoder; // This is a key class,
5 Encoderparameter myencoderparameter;
6 Encoderparameters myencoderparameters;
7 Mybitmap =   New Bitmap (server. mappath ( " A.bmp " ));
8 // Note that the myimagecodecinfo name here can be changed to a more general one.
9 Imagecodecinfo myimagecodecinfo = Imagecodecinfo. getimageencoders ()[ 0 ];
10 Myencoder = Encoder. quality;
11 Myencoderparameters =   New Encoderparameters ( 1 );
12 // Set the image quality level to 95l.
13 Myencoderparameter =   New Encoderparameter (myencoder, 95l );
14 Myencoderparameters. Param [ 0 ] = Myencoderparameter; // Assign the constructed encoderparameter class to the encoderparameters array.
15 Mybitmap. Save (server. mappath ( " OK .jpg " ), Myimagecodecinfo, myencoderparameters ); // Save image
16 Myencoderparameter. Dispose ();
17 Myencoderparameters. Dispose ();
18 Mybitmap. Dispose ();

For this imagecodecinfo myimagecodecinfo=Imagecodecinfo. getimageencoders ()[0]; Settings can also be written in this way. More common:

1 Private   Static Imagecodecinfo getencoderinfo (string mimetype)
2 {
3 Int J;
4 Imagecodecinfo [] encoders;
5 Encoders = Imagecodecinfo. getimageencoders ();
6 For (J =   0 ; J < Encoders. length; ++ J)
7 {
8If(Encoders [J]. mimetype=Mimetype)
9ReturnEncoders [J];
10}
11 Return   Null ;
12 }
13
14 // Do this when calling ..
15 Myimagecodecinfo = Getencoderinfo ( " Image/JPEG " );
16
17 // * ************ If you want to know other parameters in imagecodecinfo, you can perform the following loop, the result is ******************
18
19 Int J;
20 Imagecodecinfo [] encoders;
21 Encoders = Imagecodecinfo. getimageencoders ();
22 For (J =   0 ; J < Encoders. length; ++ J)
23 {
24Response. Write (encoders [J]. mimetype+ "<Br>");
25}
26

Through the above settings, the generated image works very well, and the most important thing is that you can increase the dimension of the encoderparameters array to achieve a lot of results. For example, to highlight the image as a whole, special processing such as lightening ..

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.