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: 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.