/
ImageFormat.cs
203 lines (181 loc) · 6.12 KB
/
ImageFormat.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// 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.Runtime.Versioning;
namespace System.Drawing.Imaging;
/// <summary>
/// Specifies the format of the image.
/// </summary>
[TypeConverter(typeof(ImageFormatConverter))]
public sealed class ImageFormat
{
// Format IDs
// private static ImageFormat undefined = new ImageFormat(new Guid("{b96b3ca9-0728-11d3-9d7b-0000f81ef32e}"));
private static readonly ImageFormat s_memoryBMP = new(new Guid("{b96b3caa-0728-11d3-9d7b-0000f81ef32e}"));
private static readonly ImageFormat s_bmp = new(new Guid("{b96b3cab-0728-11d3-9d7b-0000f81ef32e}"));
private static readonly ImageFormat s_emf = new(new Guid("{b96b3cac-0728-11d3-9d7b-0000f81ef32e}"));
private static readonly ImageFormat s_wmf = new(new Guid("{b96b3cad-0728-11d3-9d7b-0000f81ef32e}"));
private static readonly ImageFormat s_jpeg = new(new Guid("{b96b3cae-0728-11d3-9d7b-0000f81ef32e}"));
private static readonly ImageFormat s_png = new(new Guid("{b96b3caf-0728-11d3-9d7b-0000f81ef32e}"));
private static readonly ImageFormat s_gif = new(new Guid("{b96b3cb0-0728-11d3-9d7b-0000f81ef32e}"));
private static readonly ImageFormat s_tiff = new(new Guid("{b96b3cb1-0728-11d3-9d7b-0000f81ef32e}"));
private static readonly ImageFormat s_exif = new(new Guid("{b96b3cb2-0728-11d3-9d7b-0000f81ef32e}"));
private static readonly ImageFormat s_icon = new(new Guid("{b96b3cb5-0728-11d3-9d7b-0000f81ef32e}"));
private static readonly ImageFormat s_heif = new(new Guid("{b96b3cb6-0728-11d3-9d7b-0000f81ef32e}"));
private static readonly ImageFormat s_webp = new(new Guid("{b96b3cb7-0728-11d3-9d7b-0000f81ef32e}"));
private Guid _guid;
/// <summary>
/// Initializes a new instance of the <see cref='ImageFormat'/> class with the specified GUID.
/// </summary>
public ImageFormat(Guid guid)
{
_guid = guid;
}
/// <summary>
/// Specifies a global unique identifier (GUID) that represents this <see cref='ImageFormat'/>.
/// </summary>
public Guid Guid
{
get { return _guid; }
}
/// <summary>
/// Specifies a memory bitmap image format.
/// </summary>
public static ImageFormat MemoryBmp
{
get { return s_memoryBMP; }
}
/// <summary>
/// Specifies the bitmap image format.
/// </summary>
public static ImageFormat Bmp
{
get { return s_bmp; }
}
/// <summary>
/// Specifies the enhanced Windows metafile image format.
/// </summary>
public static ImageFormat Emf
{
get { return s_emf; }
}
/// <summary>
/// Specifies the Windows metafile image format.
/// </summary>
public static ImageFormat Wmf
{
get { return s_wmf; }
}
/// <summary>
/// Specifies the GIF image format.
/// </summary>
public static ImageFormat Gif
{
get { return s_gif; }
}
/// <summary>
/// Specifies the JPEG image format.
/// </summary>
public static ImageFormat Jpeg
{
get { return s_jpeg; }
}
/// <summary>
/// Specifies the W3C PNG image format.
/// </summary>
public static ImageFormat Png
{
get { return s_png; }
}
/// <summary>
/// Specifies the Tag Image File Format (TIFF) image format.
/// </summary>
public static ImageFormat Tiff
{
get { return s_tiff; }
}
/// <summary>
/// Specifies the Exchangeable Image Format (EXIF).
/// </summary>
public static ImageFormat Exif
{
get { return s_exif; }
}
/// <summary>
/// Specifies the Windows icon image format.
/// </summary>
public static ImageFormat Icon
{
get { return s_icon; }
}
/// <summary>
/// Specifies the High Efficiency Image Format (HEIF).
/// </summary>
/// <remarks>
/// This format is supported since Windows 10 1809.
/// </remarks>
[SupportedOSPlatform("windows10.0.17763.0")]
public static ImageFormat Heif
{
get { return s_heif; }
}
/// <summary>
/// Specifies the WebP image format.
/// </summary>
/// <remarks>
/// This format is supported since Windows 10 1809.
/// </remarks>
[SupportedOSPlatform("windows10.0.17763.0")]
public static ImageFormat Webp
{
get { return s_webp; }
}
/// <summary>
/// Returns a value indicating whether the specified object is an <see cref='ImageFormat'/> equivalent to this
/// <see cref='ImageFormat'/>.
/// </summary>
public override bool Equals([NotNullWhen(true)] object? o)
{
ImageFormat? format = o as ImageFormat;
if (format == null)
return false;
return _guid == format._guid;
}
/// <summary>
/// Returns a hash code.
/// </summary>
public override int GetHashCode()
{
return _guid.GetHashCode();
}
// Find any random encoder which supports this format
internal ImageCodecInfo? FindEncoder()
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
foreach (ImageCodecInfo codec in codecs)
{
if (codec.FormatID.Equals(_guid))
return codec;
}
return null;
}
/// <summary>
/// Converts this <see cref='System.Drawing.Imaging.ImageFormat'/> to a human-readable string.
/// </summary>
public override string ToString()
{
if (this.Guid == s_memoryBMP.Guid) return "MemoryBMP";
if (this.Guid == s_bmp.Guid) return "Bmp";
if (this.Guid == s_emf.Guid) return "Emf";
if (this.Guid == s_wmf.Guid) return "Wmf";
if (this.Guid == s_gif.Guid) return "Gif";
if (this.Guid == s_jpeg.Guid) return "Jpeg";
if (this.Guid == s_png.Guid) return "Png";
if (this.Guid == s_tiff.Guid) return "Tiff";
if (this.Guid == s_exif.Guid) return "Exif";
if (this.Guid == s_icon.Guid) return "Icon";
if (this.Guid == s_heif.Guid) return "Heif";
if (this.Guid == s_webp.Guid) return "Webp";
return $"[ImageFormat: {_guid}]";
}
}