-
Notifications
You must be signed in to change notification settings - Fork 169
/
Copy pathExtendedImage.cs
99 lines (88 loc) · 2.61 KB
/
ExtendedImage.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
using System;
using System.Drawing;
using static System.Net.Mime.MediaTypeNames;
using System.IO;
namespace Nexus.Client.Util
{
/// <summary>
/// This class extends the information and functionality if an <see cref="Image"/>.
/// </summary>
/// <remarks>
/// Ideally this class would extend <see cref="Image"/>, but all of <see cref="Image"/>'s
/// constructors are internal, making extension pointless.
/// </remarks>
public class ExtendedImage
{
private byte[] m_bteImage = null;
#region Properties
/// <summary>
/// Gets the byte array containing the image data.
/// </summary>
/// <value>The byte array containing the image data.</value>
public byte[] Data
{
get
{
return m_bteImage;
}
private set
{
m_bteImage = value;
if (value == null)
{
Image = null;
return;
}
ImageConverter cnvConverter = new ImageConverter();
try
{
Image = (System.Drawing.Image)cnvConverter.ConvertFrom(m_bteImage);
}
catch
{
using (WebP webp = new WebP())
Image = webp.Decode(m_bteImage);
}
}
}
/// <summary>
/// Gets the underlying <see cref="Image"/> of the object.
/// </summary>
/// <value>The underlying <see cref="Image"/> of the object.</value>
public System.Drawing.Image Image { get; private set; }
#endregion
#region Constructors
/// <summary>
/// Creates an image from the given byte array.
/// </summary>
/// <param name="p_bteImage">A byte array representing an image.</param>
public ExtendedImage(byte[] p_bteImage)
{
Data = p_bteImage;
}
#endregion
/// <summary>
/// An implicit operator that converts this <see cref="ExtendedImage"/>
/// to an <see cref="Image"/>.
/// </summary>
/// <param name="p_eimImage">The <see cref="ExtendedImage"/> to convert.</param>
/// <returns>The underlying <see cref="Image"/> of the object.</returns>
public static implicit operator System.Drawing.Image(ExtendedImage p_eimImage)
{
return (p_eimImage == null) ? null : p_eimImage.Image;
}
/// <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 string GetExtension()
{
return Image.GetExtension();
}
}
}