Editar

Partilhar via


EncoderParameter Class

Definition

Used to pass a value, or an array of values, to an image encoder.

public ref class EncoderParameter sealed : IDisposable
public sealed class EncoderParameter : IDisposable
type EncoderParameter = class
    interface IDisposable
Public NotInheritable Class EncoderParameter
Implements IDisposable
Inheritance
EncoderParameter
Implements

Examples

The following code example demonstrates how to create a EncoderParameter using the EncoderParameter constructor. To run this example, paste the code into a Windows Form and call the VaryQualityLevel method from the form's constructor. This example requires an image file named TestPhoto.jpg located at c:\.

private void VaryQualityLevel()
{
    // Get a bitmap.
    Bitmap bmp1 = new Bitmap(@"c:\TestPhoto.jpg");
    ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);

    // Create an Encoder object based on the GUID
    // for the Quality parameter category.
    System.Drawing.Imaging.Encoder myEncoder =
        System.Drawing.Imaging.Encoder.Quality;

    // Create an EncoderParameters object.
    // An EncoderParameters object has an array of EncoderParameter
    // objects. In this case, there is only one
    // EncoderParameter object in the array.
    EncoderParameters myEncoderParameters = new EncoderParameters(1);

    EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 50L);
    myEncoderParameters.Param[0] = myEncoderParameter;
    bmp1.Save(@"c:\TestPhotoQualityFifty.jpg", jpgEncoder, myEncoderParameters);

    myEncoderParameter = new EncoderParameter(myEncoder, 100L);
    myEncoderParameters.Param[0] = myEncoderParameter;
    bmp1.Save(@"c:\TestPhotoQualityHundred.jpg", jpgEncoder, myEncoderParameters);

    // Save the bitmap as a JPG file with zero quality level compression.
    myEncoderParameter = new EncoderParameter(myEncoder, 0L);
    myEncoderParameters.Param[0] = myEncoderParameter;
    bmp1.Save(@"c:\TestPhotoQualityZero.jpg", jpgEncoder, myEncoderParameters);
}
Private Sub VaryQualityLevel()
    ' Get a bitmap.
    Dim bmp1 As New Bitmap("c:\TestPhoto.jpg")
    Dim jpgEncoder As ImageCodecInfo = GetEncoder(ImageFormat.Jpeg)

    ' Create an Encoder object based on the GUID
    ' for the Quality parameter category.
    Dim myEncoder As System.Drawing.Imaging.Encoder = System.Drawing.Imaging.Encoder.Quality

    ' Create an EncoderParameters object.
    ' An EncoderParameters object has an array of EncoderParameter
    ' objects. In this case, there is only one
    ' EncoderParameter object in the array.
    Dim myEncoderParameters As New EncoderParameters(1)

    Dim myEncoderParameter As New EncoderParameter(myEncoder, 50&)
    myEncoderParameters.Param(0) = myEncoderParameter
    bmp1.Save("c:\TestPhotoQualityFifty.jpg", jpgEncoder, myEncoderParameters)

    myEncoderParameter = New EncoderParameter(myEncoder, 100&)
    myEncoderParameters.Param(0) = myEncoderParameter
    bmp1.Save("c:\TestPhotoQualityHundred.jpg", jpgEncoder, myEncoderParameters)

    ' Save the bitmap as a JPG file with zero quality level compression.
    myEncoderParameter = New EncoderParameter(myEncoder, 0&)
    myEncoderParameters.Param(0) = myEncoderParameter
    bmp1.Save("c:\TestPhotoQualityZero.jpg", jpgEncoder, myEncoderParameters)

End Sub
private ImageCodecInfo GetEncoder(ImageFormat format)
{
    ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();

    foreach (ImageCodecInfo codec in codecs)
    {
        if (codec.FormatID == format.Guid)
        {
            return codec;
        }
    }
    
    return null;
}
Private Function GetEncoder(ByVal format As ImageFormat) As ImageCodecInfo

    Dim codecs As ImageCodecInfo() = ImageCodecInfo.GetImageEncoders()

    Dim codec As ImageCodecInfo
    For Each codec In codecs
        If codec.FormatID = format.Guid Then
            Return codec
        End If
    Next codec
    Return Nothing

End Function

Remarks

You can also use an EncoderParameter object to receive a list of possible values supported by a particular parameter of a particular image encoder.

Note

In .NET 6 and later versions, the System.Drawing.Common package, which includes this type, is only supported on Windows operating systems. Use of this type in cross-platform apps causes compile-time warnings and run-time exceptions. For more information, see System.Drawing.Common only supported on Windows.

Constructors

EncoderParameter(Encoder, Byte, Boolean)

Initializes a new instance of the EncoderParameter class with the specified Encoder object and one 8-bit value. Sets the ValueType property to ValueTypeUndefined or ValueTypeByte, and sets the NumberOfValues property to 1.

EncoderParameter(Encoder, Byte)

Initializes a new instance of the EncoderParameter class with the specified Encoder object and one unsigned 8-bit integer. Sets the ValueType property to ValueTypeByte, and sets the NumberOfValues property to 1.

EncoderParameter(Encoder, Byte[], Boolean)

Initializes a new instance of the EncoderParameter class with the specified Encoder object and an array of bytes. Sets the ValueType property to ValueTypeUndefined or ValueTypeByte, and sets the NumberOfValues property to the number of elements in the array.

EncoderParameter(Encoder, Byte[])

Initializes a new instance of the EncoderParameter class with the specified Encoder object and an array of unsigned 8-bit integers. Sets the ValueType property to ValueTypeByte, and sets the NumberOfValues property to the number of elements in the array.

EncoderParameter(Encoder, Int16)

Initializes a new instance of the EncoderParameter class with the specified Encoder object and one, 16-bit integer. Sets the ValueType property to ValueTypeShort, and sets the NumberOfValues property to 1.

EncoderParameter(Encoder, Int16[])

Initializes a new instance of the EncoderParameter class with the specified Encoder object and an array of 16-bit integers. Sets the ValueType property to ValueTypeShort, and sets the NumberOfValues property to the number of elements in the array.

EncoderParameter(Encoder, Int32, EncoderParameterValueType, IntPtr)

Initializes a new instance of the EncoderParameter class with the specified Encoder object, number of values, data type of the values, and a pointer to the values stored in the EncoderParameter object.

EncoderParameter(Encoder, Int32, Int32, Int32, Int32)

Initializes a new instance of the EncoderParameter class with the specified Encoder object and four, 32-bit integers. The four integers represent a range of fractions. The first two integers represent the smallest fraction in the range, and the remaining two integers represent the largest fraction in the range. Sets the ValueType property to ValueTypeRationalRange, and sets the NumberOfValues property to 1.

EncoderParameter(Encoder, Int32, Int32, Int32)
Obsolete.
Obsolete.
Obsolete.

Initializes a new instance of the EncoderParameter class with the specified Encoder object and three integers that specify the number of values, the data type of the values, and a pointer to the values stored in the EncoderParameter object.

EncoderParameter(Encoder, Int32, Int32)

Initializes a new instance of the EncoderParameter class with the specified Encoder object and a pair of 32-bit integers. The pair of integers represents a fraction, the first integer being the numerator, and the second integer being the denominator. Sets the ValueType property to ValueTypeRational, and sets the NumberOfValues property to 1.

EncoderParameter(Encoder, Int32[], Int32[], Int32[], Int32[])

Initializes a new instance of the EncoderParameter class with the specified Encoder object and four arrays of 32-bit integers. The four arrays represent an array rational ranges. A rational range is the set of all fractions from a minimum fractional value through a maximum fractional value. Sets the ValueType property to ValueTypeRationalRange, and sets the NumberOfValues property to the number of elements in the numerator1 array, which must be the same as the number of elements in the other three arrays.

EncoderParameter(Encoder, Int32[], Int32[])

Initializes a new instance of the EncoderParameter class with the specified Encoder object and two arrays of 32-bit integers. The two arrays represent an array of fractions. Sets the ValueType property to ValueTypeRational, and sets the NumberOfValues property to the number of elements in the numerator array, which must be the same as the number of elements in the denominator array.

EncoderParameter(Encoder, Int64, Int64)

Initializes a new instance of the EncoderParameter class with the specified Encoder object and a pair of 64-bit integers. The pair of integers represents a range of integers, the first integer being the smallest number in the range, and the second integer being the largest number in the range. Sets the ValueType property to ValueTypeLongRange, and sets the NumberOfValues property to 1.

EncoderParameter(Encoder, Int64)

Initializes a new instance of the EncoderParameter class with the specified Encoder object and one 64-bit integer. Sets the ValueType property to ValueTypeLong (32 bits), and sets the NumberOfValues property to 1.

EncoderParameter(Encoder, Int64[], Int64[])

Initializes a new instance of the EncoderParameter class with the specified Encoder object and two arrays of 64-bit integers. The two arrays represent an array integer ranges. Sets the ValueType property to ValueTypeLongRange, and sets the NumberOfValues property to the number of elements in the rangebegin array, which must be the same as the number of elements in the rangeend array.

EncoderParameter(Encoder, Int64[])

Initializes a new instance of the EncoderParameter class with the specified Encoder object and an array of 64-bit integers. Sets the ValueType property to ValueTypeLong (32-bit), and sets the NumberOfValues property to the number of elements in the array.

EncoderParameter(Encoder, String)

Initializes a new instance of the EncoderParameter class with the specified Encoder object and a character string. The string is converted to a null-terminated ASCII string before it is stored in the EncoderParameter object. Sets the ValueType property to ValueTypeAscii, and sets the NumberOfValues property to the length of the ASCII string including the NULL terminator.

Properties

Encoder

Gets or sets the Encoder object associated with this EncoderParameter object. The Encoder object encapsulates the globally unique identifier (GUID) that specifies the category (for example Quality, ColorDepth, or Compression) of the parameter stored in this EncoderParameter object.

NumberOfValues

Gets the number of elements in the array of values stored in this EncoderParameter object.

Type

Gets the data type of the values stored in this EncoderParameter object.

ValueType

Gets the data type of the values stored in this EncoderParameter object.

Methods

Dispose()

Releases all resources used by this EncoderParameter object.

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
Finalize()

Allows an EncoderParameter object to attempt to free resources and perform other cleanup operations before the EncoderParameter object is reclaimed by garbage collection.

GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to

See also