Gdiplus [55]: image (vii) image encoding Parameters

Source: Internet
Author: User
You can use igpimage. getencoderparameterlist to obtain the parameter list of the specified encoding format;

This list can be used to traverse the pointer of each parameter: pgpnativeencoderparameter (pointer of tgpnativeencoderparameter );

Tgpnativeencoderparameter is a struct:

Tgpnativeencoderparameter = record guid: tguid; {parameter identifier} numberofvalues: ulong; {number of elements in the parameter array} valuetype: tgpencoderparametervaluetype; {parameter type} value: pointer; {Parameter Data Pointer} end; // The tgpencoderparametervaluetype is an enumeration with the following enumerated values: bytes = 1 {byte array} encoderparametervaluetypeascii = 2 {pansichar} bytes = 3 {word} bytes = 4 {Cardinal} encoderparametervaluetyperational = 5 {Cardinal/Cardinal; the first number is molecules, the second number is denominator} bytes = 6 {a pair of cardinal, indicating a value range} encoderparametervaluetypeundefined = 7 {can contain any data type byte array} encoderparametervaluetyperationalrange = 8 {four integers: cardinal/Cardinal, Cardinal/Cardinal} encoderparametervaluetypepointer = 9 {pointer} // The Four integers in encoderparametervaluetyperationalrange obtain two values through fractional calculation: Minimum value... maximum value.
 

The parameters of each encoder must be different. The following code obtains the parameter information supported by the JPEG Encoder:

Uses gdiplus; Procedure tform1.button1click (Sender: tobject); var image: igpimage; parameters: igpencoderparameters; Param: pgpnativeencoderparameter; begin image: = tgpbitmap. create (1, 1); parameters: = image. getencoderparameterlist (tgpimageformat. JPEG. codecid); memo1.clear; for Param in parameters do with memo1.lines do begin add (format ('guid: % s', [guidtostring (Param. guid)]); add (format ('numberofvalues: % d', [Param. numberofvalues]); add (format ('valuetype: % d', [ord (Param. valuetype)]); add (format ('value: $ % P', [Param. value]); add (emptystr); end; (* result: guid: {region} numberofvalues: 5 valuetype: 4 value: $00ad8190 guid: {region} numberofvalues: 1 valuetype: 6 value: $00ad81a4 guid: {EDB33BCE-0266-4A77-B904-27216099E717} numberofvalues: 0 valuetype: 3 Value: $ 00ad81ac guid: {F2E455DC-09B3-4316-8260-676ADA32481C} numberofvalues: 0 valuetype: 3 Value: $ 00ad81ac *)
 

The Type obtained by the igpimage. getencoderparameterlist method is igpencoderparameters;

The igpimage. Save method has a default igpencoderparameters parameter, which can be used to pass in encoding parameters.

There are many types of encoding parameters. For example, encoderquality determines the image compression ratio.

The following example uses three different quality parameters (compression level) when saving JPG files ):


Uses gdiplus; Procedure extract (Sender: tobject); var prams: igpencoderparameters; Image: igpimage; graphics: igpgraphics; Quality: integer; begin chdir ('C: \ gdiplusimg \'); image: = tgpimage.create('grapebunch.bmp '); prams: = tgpencoderparameters. create; Quality: = 1; prams. add (encoderquality, quality); image.save('grapebunch_1.jpg ', tgpimageformat. JPEG, prams); prams. clear; Quality: = 50; prams. add (encoderquality, quality); image.save('grapebunch_50.jpg ', tgpimageformat. JPEG, prams); prams. clear; Quality: = 100; prams. add (encoderquality, quality); image.save('grapebunch_100.jpg ', tgpimageformat. JPEG, prams); // display graphics: = tgpgraphics. create (handle); Image: = tgpimage.create('grapebunch_1.jpg '); graphics. drawimage (image, 10, 10); graphics. translatetransform (image. width + 10, 0); Image: = tgpimage.create('grapebunch_50.jpg '); graphics. drawimage (image, 10, 10); graphics. translatetransform (image. width + 10, 0); Image: = tgpimage.create('grapebunch_100.jpg '); graphics. drawimage (image, 10, 10); end;
 

Members of igpencoderparameters:

Igpencoderparameters. getenumerator; igpencoderparameters. clear; igpencoderparameters. add (); igpencoderparameters. count; igpencoderparameters. param []; igpencoderparameters. nativeparams; // The add method has multiple reloads, which facilitates adding various types of data; parameter type constants: encodercompression {compression} colors {color depth} encoderscanmethod {scan method} encoderversion {version} encoderrendermethod {Rendering Method} encoderquality {quality} encodertransformation {conversion} colors {brightness table} colors {color table} encodersaveflag {save flag} codeciimagebytes {}{ below is supported by GDI + 1.1 :} encodercolorspace {} encoderimageitems {} encodersaveascmyk {}
 

What "parameter types" are supported by five encoders (BMP, JPEG, GIF, Tiff, and PNG?
What is the format of supported parameter types? Although the add method has prepared multiple reloads, which one should be used?

The following program lists the parameters of various encoders:

Uses gdiplus; const paramvaluetypearr: array [1 .. 9] of string = ('valuetypebyte', 'valuetypeascii ', 'valuetypeshort', 'valuetypelong ', 'valuetyperational', 'category', 'category', 'valuetyperationalrange ', 'valuetypepointer '); // custom function getguidname (G: tguid): string; var S: string; begin S: = emptystr; If isw.guid (G, encodercompression) then S: = 'encodercompression'; If isserguid (G, encodercolordepth) Then S: = 'encodercolordepth '; If isserguid (G, encoderscanmethod) Then S: = 'encoderscanmethod '; if isserguid (G, encoderversion) Then S: = 'encoderversion'; If isserguid (G, encoderrendermethod) Then S: = 'encoderrendermethod'; If isserguid (G, encoderquality) then s: = 'encoderquality'; If isserguid (G, encodertransformation) Then S: = 'encodertransformation '; If isserguid (G, identifier) Then S: = 'encoderluminancetable'; If isserguid (G, token) Then S: = 'authorization'; If isincluguid (G, encodersaveflag) Then S: = 'encodersaveflag'; If isincluguid (G, codeciimagebytes) Then S: = 'codeciimagebytes '; {$ if (gdipver >=$ 0110)} If isserguid (G, encodercolorspace) Then S: = 'encodercolorspace'; If isserguid (G, encoderimageitems) then s: = 'encoderimageitems '; If ishangguid (G, encodersaveascmyk) Then S: = 'hangzhou'; {$ ifend} result: = s; end; Procedure tform1.button1click (Sender: tobject ); vaR image: igpimage; parameters: igpencoderparameters; Param: pgpnativeencoderparameter; Encoder: igpimagecodecinfo; begin image: = tgpbitmap. create (1, 1); memo1.clear; for Encoder in tgpimagecodecinfo. getimageencoders do with memo1.lines do begin parameters: = image. getencoderparameterlist (encoder. CLSID); add ('--------------------'); add (format ('encoder type: % s', [encoder. formatdescription]); If parameters. count = 0 then begin add ('no parameter'); add (emptystr); continue; end; add (format ('number of parameters: % d', [parameters. count]); for Param in parameters do begin add (format ('parameter type: % s', [getguidname (Param. guid)]); add (format ('parameter Value Type: % s', [paramvaluetypearr [ord (Param. valuetype)]); add (format ('number of parameter values: % d', [Param. numberofvalues]); add (format ('parameter value pointer: $ % P', [Param. value]); add (emptystr); end; (* display result: -------------------- encoder type: BMP No parameter -------------------- type: JPEG parameter count: 4 parameter type: encodertransformation parameter value type: valuetypelong parameter value count: 5 parameter value pointer: $00ad8190 parameter type: encoderquality parameter value type: valuetypelongrange parameter value count: 1 parameter value pointer: $00ad81a4 parameter type: encoderluminancetable parameter value type: valuetypeshort parameter value count: 0 parameter value pointer: $ 00ad81ac parameter type: encoderchrominancetable parameter value type: valuetypeshort parameter value count: 0 parameter value pointer: $ 00ad81ac ---------------------- encoder type: GIF No parameter ------------------ encodercompression type: tiff parameter count: 3 parameter type: encodercompression parameter value type: valuetypelong parameter value count: 5 parameter value pointer: $00ad8190 parameter type: encodercolordepth parameter value type: valuetypelong parameter value count: 5 parameter value pointer: $00ad81a4 parameter type: encodersaveflag parameter value type: valuetypelong parameter value count: 1 parameter value pointer: $00ad81b8 ---------------------- encoder type: PNG No parameter *)
 

From the example above, we can see that:

There are no encoding parameters for BMP, GIF, and PNG encoders (whether GIF is supported in GDI + 1.1 is not yet tested ).

JPEG support:
Encodertransformation (conversion)
Encoderquality)
Encoderluminancetable (brightness table)
Encoderchrominancetable (color table)

Tiff support:
Encodercompression (compression)
Encodercolordepth (color depth)
Encodersaveflag (save flag)

We can learn that:

The optional value of the parameter type encodertransformation of JPEG encoding is:
Tgpencodervalue (13): encodervaluetransformrotate90
Tgpencodervalue (14): encodervaluetransformrotate180
Tgpencodervalue (15): encodervaluetransformrotate270
Tgpencodervalue (16): encodervaluetransformfliphorizontal
Tgpencodervalue (17): encodervaluetransformflipvertical

The optional value of the parameter type encoderquality of JPEG encoding is: 0 .. 100.

The optional value of the parameter type encodercompression of TIFF encoding is:
Tgpencodervalue (2): encodervaluecompressionlzw
Tgpencodervalue (3): encodervaluecompressionccitt3
Tgpencodervalue (4): encodervaluecompressionccitt4
Tgpencodervalue (5): encodervaluecompressionrle
Tgpencodervalue (6): encodervaluecompressionnone

The optional values of the parameter type encodercolordepth of TIFF encoding are:, and 32.

These values can be obtained from the following program:

uses GdiPlus;var  Image: IGPImage;  Parameters: IGPEncoderParameters;  p: PCardinal;procedure TForm1.FormCreate(Sender: TObject);begin  Image := TGPBitmap.Create(1, 1);end;{$PointerMath On}procedure TForm1.Button1Click(Sender: TObject);begin  Parameters := Image.GetEncoderParameterList(TGPImageFormat.Jpeg.CodecId);  p := Parameters[0].Value; { EncoderTransformation }  ShowMessageFmt('%d,%d,%d,%d,%d', [p[0],p[1],p[2],p[3],p[4]]); { 13,14,15,16,17 }end;procedure TForm1.Button2Click(Sender: TObject);begin  Parameters := Image.GetEncoderParameterList(TGPImageFormat.Jpeg.CodecId);  p := Parameters[1].Value; { EncoderQuality }  ShowMessageFmt('%d..%d', [p[0],p[1]]); { 0..100 }end;procedure TForm1.Button3Click(Sender: TObject);begin  Parameters := Image.GetEncoderParameterList(TGPImageFormat.Tiff.CodecId);  p := Parameters[0].Value; { EncoderCompression }  ShowMessageFmt('%d,%d,%d,%d,%d', [p[0],p[1],p[2],p[3],p[4]]); { 2,3,5,4,6 }end;procedure TForm1.Button4Click(Sender: TObject);begin  Parameters := Image.GetEncoderParameterList(TGPImageFormat.Tiff.CodecId);  p := Parameters[1].Value; { EncoderColorDepth }  ShowMessageFmt('%d,%d,%d,%d,%d', [p[0],p[1],p[2],p[3],p[4]]); { 1,4,8,24,32 }end;
 

The following example uses the encodertransformation encoding parameter of JPEG to save the rotated image:


Uses gdiplus; Procedure injection (Sender: tobject); var graphics: igpgraphics; prams: igpencoderparameters; Image: igpimage; begin chdir ('C: \ gdiplusimg \ '); image: = tgpimage.create('grapes.jpg '); prams: = tgpencoderparameters. create; prams. add (encodertransformation, encodervaluetransformrotate90); image.save('grapes_rotate90.jpg ', tgpimageformat. JPEG, prams); graphics: = tgpgraphics. create (H Andle); graphics. drawimage (image, 10, 10, image. Width, image. Height); end; // Why only JPEG provides this encoding parameter? Because JPG files will reduce the quality during Automatic saving, this conversion will not.
 

In addition:

1. Many types of encoded parameter values are cardinal, which is based on official materials. However, the Add function requires the integer type.

2. You can see from the information obtained above that the GDI + 1.0 cannot be written into the GIF animation; can the GDI + 1.1 still be tested.

3. You can set the encodersaveflag type parameter of the tiff encodersaveflag to save the multi-page TIFF file.

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.