How to: Determine the Parameters Supported by an Encoder
You can adjust image parameters, such as quality and compression level, but you must know which parameters are supported by a given image encoder. The Image class provides the GetEncoderParameterList method so that you can determine which image parameters are supported for a particular encoder. You specify the encoder with a GUID. The GetEncoderParameterList method returns an array of EncoderParameter objects.
Example
The following example code outputs the supported parameters for the JPEG encoder. Use the list of parameter categories and associated GUIDs in the Encoder class overview to determine the category for each parameter.
private void GetSupportedParameters(PaintEventArgs e)
{
Bitmap bitmap1 = new Bitmap(1, 1);
ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);
EncoderParameters paramList = bitmap1.GetEncoderParameterList(jpgEncoder.Clsid);
EncoderParameter[] encParams = paramList.Param;
StringBuilder paramInfo = new StringBuilder();
for (int i = 0; i < encParams.Length; i++)
{
paramInfo.Append("Param " + i + " holds " + encParams[i].NumberOfValues +
" items of type " +
encParams[i].ValueType + "\r\n" + "Guid category: " + encParams[i].Encoder.Guid + "\r\n");
}
e.Graphics.DrawString(paramInfo.ToString(), this.Font, Brushes.Red, 10.0F, 10.0F);
}
private ImageCodecInfo GetEncoder(ImageFormat format)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
foreach (ImageCodecInfo codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
return null;
}
Private Sub GetSupportedParameters(ByVal e As PaintEventArgs)
Dim bitmap1 As New Bitmap(1, 1)
Dim jpgEncoder As ImageCodecInfo = GetEncoder(ImageFormat.Jpeg)
Dim paramList As EncoderParameters = _
bitmap1.GetEncoderParameterList(jpgEncoder.Clsid)
Dim encParams As EncoderParameter() = paramList.Param
Dim paramInfo As New StringBuilder()
Dim i As Integer
For i = 0 To encParams.Length - 1
paramInfo.Append("Param " & i & " holds " & _
encParams(i).NumberOfValues & " items of type " & _
encParams(i).Type.ToString() & vbCr & vbLf & "Guid category: " & _
encParams(i).Encoder.Guid.ToString() & vbCr & vbLf)
Next i
e.Graphics.DrawString(paramInfo.ToString(), _
Me.Font, Brushes.Red, 10.0F, 10.0F)
End Sub
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
Compiling the Code
This example requires:
A Windows Forms application.
A PaintEventArgs, which is a parameter of PaintEventHandler.
See also
.NET Desktop feedback