/
ImageFormatConverter.cs
144 lines (129 loc) · 5.93 KB
/
ImageFormatConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Drawing.Imaging;
using System.Globalization;
using System.Reflection;
namespace System.Drawing;
[SuppressMessage("Interoperability", "CA1416:Validate platform compatibility", Justification = "Heif and Webp are referenced here for " +
"design-time support, the user is responsible to ensure that they are used on a supported version of Windows.")]
public class ImageFormatConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
{
return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
}
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, object value)
{
// we must be able to convert from short names and long names
string? strFormat = value as string;
if (strFormat is null)
{
// case #1, this is not a string
return base.ConvertFrom(context, culture, value);
}
if ((strFormat[0] == '[') && (strFormat.Length >= 50) && Guid.TryParse(strFormat.AsSpan(14, 36), out Guid guid))
{
// case #2, this is probably a long format (guid)
return new ImageFormat(guid);
}
// case #3, this is probably a short format
if (strFormat.Equals("Bmp", StringComparison.OrdinalIgnoreCase))
return ImageFormat.Bmp;
else if (strFormat.Equals("Emf", StringComparison.OrdinalIgnoreCase))
return ImageFormat.Emf;
else if (strFormat.Equals("Exif", StringComparison.OrdinalIgnoreCase))
return ImageFormat.Exif;
else if (strFormat.Equals("Gif", StringComparison.OrdinalIgnoreCase))
return ImageFormat.Gif;
else if (strFormat.Equals("Icon", StringComparison.OrdinalIgnoreCase))
return ImageFormat.Icon;
else if (strFormat.Equals("Jpeg", StringComparison.OrdinalIgnoreCase))
return ImageFormat.Jpeg;
else if (strFormat.Equals("MemoryBmp", StringComparison.OrdinalIgnoreCase))
return ImageFormat.MemoryBmp;
else if (strFormat.Equals("Png", StringComparison.OrdinalIgnoreCase))
return ImageFormat.Png;
else if (strFormat.Equals("Tiff", StringComparison.OrdinalIgnoreCase))
return ImageFormat.Tiff;
else if (strFormat.Equals("Wmf", StringComparison.OrdinalIgnoreCase))
return ImageFormat.Wmf;
else if (strFormat.Equals("Heif", StringComparison.OrdinalIgnoreCase))
return ImageFormat.Heif;
else if (strFormat.Equals("Webp", StringComparison.OrdinalIgnoreCase))
return ImageFormat.Webp;
throw new FormatException(SR.Format(SR.ConvertInvalidPrimitive, strFormat, nameof(ImageFormat)));
}
public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType)
{
if (value is ImageFormat imgFormat)
{
if (destinationType == typeof(string))
{
return imgFormat.ToString();
}
if (destinationType == typeof(InstanceDescriptor))
{
string? strFormat = null;
if (imgFormat.Guid.Equals(ImageFormat.Bmp.Guid))
strFormat = "Bmp";
else if (imgFormat.Guid.Equals(ImageFormat.Emf.Guid))
strFormat = "Emf";
else if (imgFormat.Guid.Equals(ImageFormat.Exif.Guid))
strFormat = "Exif";
else if (imgFormat.Guid.Equals(ImageFormat.Gif.Guid))
strFormat = "Gif";
else if (imgFormat.Guid.Equals(ImageFormat.Icon.Guid))
strFormat = "Icon";
else if (imgFormat.Guid.Equals(ImageFormat.Jpeg.Guid))
strFormat = "Jpeg";
else if (imgFormat.Guid.Equals(ImageFormat.MemoryBmp.Guid))
strFormat = "MemoryBmp";
else if (imgFormat.Guid.Equals(ImageFormat.Png.Guid))
strFormat = "Png";
else if (imgFormat.Guid.Equals(ImageFormat.Tiff.Guid))
strFormat = "Tiff";
else if (imgFormat.Guid.Equals(ImageFormat.Wmf.Guid))
strFormat = "Wmf";
if (strFormat is not null)
{
return new InstanceDescriptor(typeof(ImageFormat).GetProperty(strFormat), null);
}
else
{
ConstructorInfo? ctor = typeof(ImageFormat).GetConstructor(new Type[] { typeof(Guid) });
return new InstanceDescriptor(ctor, new object[] { imgFormat.Guid });
}
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext? context)
{
return new StandardValuesCollection(new ImageFormat[]
{
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) => true;
}