-
Notifications
You must be signed in to change notification settings - Fork 169
/
Copy pathImageExtensions.cs
52 lines (51 loc) · 1.7 KB
/
ImageExtensions.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
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
namespace Nexus.Client.Util
{
/// <summary>
/// Extension methods for <see cref="Image"/>.
/// </summary>
/// <remarks>
/// This allows instantiation of an image from a <see cref="byte"/> array,
/// and returns the file extension commonly associated with the image's
/// format.
/// </remarks>
public static class ImageExtensions
{
/// <summary>
/// Returns the file extension commonly associated with the image's
/// format.
/// </summary>
/// <param name="p_imgImage">The image whose format is to be examined.</param>
/// <returns>The file extension commonly associated with the image's
/// format.</returns>
/// <exception cref="ImageFormatException">Thrown if the <see cref="Image"/>'s
/// <see cref="ImageFormat"/> is not recognized.</exception>
public static string GetExtension(this Image p_imgImage)
{
ImageFormat ifmFormat = p_imgImage.RawFormat;
if (ifmFormat.Equals(ImageFormat.Bmp))
return ".bmp";
if (ifmFormat.Equals(ImageFormat.Emf))
return ".emf";
if (ifmFormat.Equals(ImageFormat.Exif))
return ".jpg";
if (ifmFormat.Equals(ImageFormat.Gif))
return ".gif";
if (ifmFormat.Equals(ImageFormat.Icon))
return ".ico";
if (ifmFormat.Equals(ImageFormat.Jpeg))
return ".jpg";
if (ifmFormat.Equals(ImageFormat.MemoryBmp))
return ".bmp";
if (ifmFormat.Equals(ImageFormat.Png))
return ".png";
if (ifmFormat.Equals(ImageFormat.Tiff))
return ".tif";
if (ifmFormat.Equals(ImageFormat.Wmf))
return ".wmf";
throw new ImageFormatException("Unrecognized image format: " + ifmFormat.ToString(), ifmFormat);
}
}
}