Unfortunately, .NET does not provide an easy way to get your hands on an ImageCodecInfo for a specific image format.
Instead, you can use something like the following:
C#:
public static ImageCodecInfo FindEncoder(ImageFormat format)
{
if (format == null)
throw new ArgumentNullException("format");
foreach (ImageCodecInfo codec in ImageCodecInfo.GetImageEncoders())
{
if (codec.FormatID.Equals(format.Guid))
{
return codec;
}
}
return null;
}
Visual Basic:
Public Shared Function FindEncoder(ByVal format As ImageFormat) As ImageCodecInfo
If (format Is Nothing) Then Throw New ArgumentNullException("format")
For Each codec As ImageCodecInfo In ImageCodecInfo.GetImageEncoders()
If codec.FormatID.Equals(format.Guid) Then
Return codec
End If
Next
Return Nothing
End Function
To use this method and retrieve the correct encoder, simply pass an ImageFormat for the format you want to save the image as, and then pass the encoder to the Image.Save overload.
C#:
ImageCodecInfo encoder = FindEncoder(ImageFormat.Jpeg);
Visual Basic:
Dim encoder as ImageCodecInfo = FindEncoder(ImageFormat.Jpeg);