ImageFormatConverter
ImageFormatConverter is a class that can be used to convert ImageFormat objects from one data type to another. Access this class through the TypeDescriptor object.
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Diagnostics.CodeAnalysis;
using System.Drawing.Imaging;
using System.Globalization;
using System.Runtime.CompilerServices;
namespace System.Drawing
{
[NullableContext(2)]
[Nullable(0)]
public class ImageFormatConverter : TypeConverter
{
[NullableContext(1)]
public override bool CanConvertFrom([Nullable(2)] ITypeDescriptorContext context, Type sourceType)
{
if (!(sourceType == typeof(string)))
return base.CanConvertFrom(context, sourceType);
return true;
}
public override bool CanConvertTo(ITypeDescriptorContext context, [NotNullWhen(true)] Type destinationType)
{
if (destinationType == typeof(string) || destinationType == typeof(InstanceDescriptor))
return true;
return base.CanConvertTo(context, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, [Nullable(1)] object value)
{
string text = value as string;
if (text == null)
return base.ConvertFrom(context, culture, value);
if (text[0] == '[' && text.Length >= 50 && Guid.TryParse(text.AsSpan(14, 36), out Guid result))
return new ImageFormat(result);
if (text.Equals("Bmp", StringComparison.OrdinalIgnoreCase))
return ImageFormat.Bmp;
if (text.Equals("Emf", StringComparison.OrdinalIgnoreCase))
return ImageFormat.Emf;
if (text.Equals("Exif", StringComparison.OrdinalIgnoreCase))
return ImageFormat.Exif;
if (text.Equals("Gif", StringComparison.OrdinalIgnoreCase))
return ImageFormat.Gif;
if (text.Equals("Icon", StringComparison.OrdinalIgnoreCase))
return ImageFormat.Icon;
if (text.Equals("Jpeg", StringComparison.OrdinalIgnoreCase))
return ImageFormat.Jpeg;
if (text.Equals("MemoryBmp", StringComparison.OrdinalIgnoreCase))
return ImageFormat.MemoryBmp;
if (text.Equals("Png", StringComparison.OrdinalIgnoreCase))
return ImageFormat.Png;
if (text.Equals("Tiff", StringComparison.OrdinalIgnoreCase))
return ImageFormat.Tiff;
if (text.Equals("Wmf", StringComparison.OrdinalIgnoreCase))
return ImageFormat.Wmf;
if (text.Equals("Heif", StringComparison.OrdinalIgnoreCase))
return ImageFormat.Heif;
if (text.Equals("Webp", StringComparison.OrdinalIgnoreCase))
return ImageFormat.Webp;
throw new FormatException(System.SR.Format(System.SR.ConvertInvalidPrimitive, text, "ImageFormat"));
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, [Nullable(1)] Type destinationType)
{
ImageFormat imageFormat = value as ImageFormat;
if (imageFormat != null) {
if (destinationType == typeof(string))
return imageFormat.ToString();
if (destinationType == typeof(InstanceDescriptor)) {
string text = null;
Guid guid = imageFormat.Guid;
if (guid.Equals(ImageFormat.Bmp.Guid))
text = "Bmp";
else {
guid = imageFormat.Guid;
if (guid.Equals(ImageFormat.Emf.Guid))
text = "Emf";
else {
guid = imageFormat.Guid;
if (guid.Equals(ImageFormat.Exif.Guid))
text = "Exif";
else {
guid = imageFormat.Guid;
if (guid.Equals(ImageFormat.Gif.Guid))
text = "Gif";
else {
guid = imageFormat.Guid;
if (guid.Equals(ImageFormat.Icon.Guid))
text = "Icon";
else {
guid = imageFormat.Guid;
if (guid.Equals(ImageFormat.Jpeg.Guid))
text = "Jpeg";
else {
guid = imageFormat.Guid;
if (guid.Equals(ImageFormat.MemoryBmp.Guid))
text = "MemoryBmp";
else {
guid = imageFormat.Guid;
if (guid.Equals(ImageFormat.Png.Guid))
text = "Png";
else {
guid = imageFormat.Guid;
if (guid.Equals(ImageFormat.Tiff.Guid))
text = "Tiff";
else {
guid = imageFormat.Guid;
if (guid.Equals(ImageFormat.Wmf.Guid))
text = "Wmf";
}
}
}
}
}
}
}
}
}
if (text != null)
return new InstanceDescriptor(typeof(ImageFormat).GetProperty(text), null);
return new InstanceDescriptor(typeof(ImageFormat).GetConstructor(new Type[1] {
typeof(Guid)
}), new object[1] {
imageFormat.Guid
});
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
[NullableContext(1)]
public override StandardValuesCollection GetStandardValues([Nullable(2)] ITypeDescriptorContext context)
{
return new StandardValuesCollection(new ImageFormat[12] {
ImageFormat.MemoryBmp,
ImageFormat.Bmp,
ImageFormat.Emf,
ImageFormat.Wmf,
ImageFormat.Gif,
ImageFormat.Jpeg,
ImageFormat.Png,
ImageFormat.Tiff,
ImageFormat.Exif,
ImageFormat.Icon,
ImageFormat.Heif,
ImageFormat.Webp
});
}
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
}
}