Encoder Parameters and Image Formats in GDI+


This article has been excerpted from book "Graphics Programming with GDI+".

The Save method of the Image class can be used to save images in different formats. This is what our code is used to save an images as a TIFF file:

            curImage.Save(fileName, ImageFormat.Tiff);

In fact, the Save method does much more than just save an image in different formats. An overloaded Save method can take an argument of type EncoderParameters, which represents an encoder. An encoder is responsible for converting a file from one format to another and a decoder reverses it. The encoder is responsible for saving an image to a format defined by codec parameters.

Two forms of the Save method with EncoderParameters are

        public void Save(Stream, ImageCodecInfo, EncoderParameters);

        public void Save(String, ImageCodecInfo, EncoderParameters);

Another method is SaveAdd. This method adds information to an Image object. EncoderParameters determines how the new information is incorporated into the existing image.

The SaveAdd method has two overloaded forms. The first form adds a frame to the file or stream specified in a previous call to the Save method. This method can be used to save selected frames from a multiple-frame image to another multiple-frame image.

        public void SaveAdd(EncoderParameters);

The second form, which takes two parameters (Image and EncoderParameters) adds a frame to the file or stream specified in a previous call to the Save method.

        public void SaveAdd(Image, EncoderParameters);

The Encoder, EncoderCollection, and Image Relationship

Unfortunately, most because of inadequate documentation and samples in MSDN, it is a little difficult to understand how encoder and decoder parameters relate to images. To help clear this up, look at Figure 8.12, which shows how the different elements relate to each other.

As you can see, the Save method of the Image class consumes EncoderParameters, which is a collection of type EncoderParameter. An EncoderParameter object represents an encoder. We use the Encoder property to attach an Encoder object to the EncoderParameter object. 

The Encoder and EncoderParameter Classes

An Encoder object encapsulates a globally unique identifies (GUID) that identifies the category of an image encoder parameter represented by EncoderParamater. This Encoder object is attached to an EncoderParameter object through its Encoder property.
An Encoder object is created by use of the Encoder class constructor, which takes one parameter of type Guid.

The Encoder class provides one property, Guid, and a set of static fields, which represent the encoder properties. The Guid property of the Encoder class returns a GUID attached to an encoder. Table 8.10 describes the fields.

Figure 8.12.gif

FIGURE 8.12: The relationship among Encoder, EncoderCollection , and Image

EncoderParameters represents an array of values that is used to pass values to an image encoder. The EncoderParameter constructor takes an argument of Encoder object type. Table 8.11 describes the properties of the EncoderParameter class.

The EncoderParameters class represents an array of EncoderParamter objects. You will have to create an EncoderParameter object because the Save and SaveAdd methods take a parameter of type EncoderParameters.

Suppose that you want to save a JPEG file to a TIFF file with 24-bit compression. In Listing 8.13 we first create an EncoderParameters object. Then we create an array of ImageCodecInfo objects, which provide members to retrieve information about installed image codecs, including the codec name, MIME type, format, version, and signature. The properties of the ImageCodecInfo class are listed in Table 8.12. All of these properties have both get and set types.

TABLE 8.10: Encoder fields

Field

Description

ChrominanceTable

Specifies chrominance table as the parameter category.

ColorDepth

Specifies color depth as the parameter category.

Compression

Specifies compression as the parameter category

LuminanceTable

Specifies luminance table as the parameter category.

Quality

Specifies quality as the parameter category.

RenderMethod

Specifies rendering method as the parameter category.

SaveFlag

Specifies save flag as the parameter category.

ScanMethod

Specifies scan method as the parameter category.

Transformation

Specifies transformation as the parameter category.

Version

Specifies version as the parameter category.

TABLE 8.11: EncoderParameter properties

Property

Description

Encoder

Represents an encoder associated with this encoder parameter. Both get and set types.

NumberOfValues

Returns the number of elements in the array of values stored in an encoder parameter.

Type

Returns the type of an encoder parameter.

ValueType

Returns the data type of the values stored in an encoder parameter.

GDI+ provides several built-in image encoders and decoders. The ImageCodecInfo class provides two static methods: GetImageEncoders and GetImageDecoders, which return the built-in GDI+ image encoders and decoders in an array of ImageCodecInfo objects.

Conclusion

Hope the article would have helped you in understanding Encoder Parameters and Image Formats in GDI+. Read other articles on GDI+ on the website.

bookGDI.jpg
This book teaches .NET developers how to work with GDI+ as they develop applications that include graphics, or that interact with monitors or printers. It begins by explaining the difference between GDI and GDI+, and covering the basic concepts of graphics programming in Windows.

erver'>

Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.