-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageHelper.cs
More file actions
126 lines (121 loc) · 3.64 KB
/
Copy pathImageHelper.cs
File metadata and controls
126 lines (121 loc) · 3.64 KB
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
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
namespace NetFrameworkHelper
{
/// <summary>
/// Class that contains common operations for handling images
/// </summary>
public static class ImageHelper
{
/// <summary>
/// Convert an image to a blob (byte array) for storage in a database or for further byte manipulation
/// </summary>
/// <param name="image">Image this operation will be carried out against</param>
/// <returns>byte[]</returns>
public static byte[] ToBlob(this Image image)
{
//check if the byte array is compressed before moving on
if (image == null)
{
throw new ArgumentNullException(nameof(image), "The image passed was null;");
}
return image.ToBlob(false);
}
/// <summary>
/// Convert an image to a blob (byte array) for storage in a database or for further byte manipulation.
/// Specify true to return a compressed byte array.
/// </summary>
/// <param name="image">Image this operation will be carried out against</param>
/// <param name="compress">Specify wether or not to compress the byte array using GZip. Default is false</param>
/// <returns></returns>
public static byte[] ToBlob(this Image image, bool compress = false)
{
//check if the byte array is compressed before moving on
if (image == null)
{
throw new ArgumentNullException(nameof(image), "The image passed was null;");
}
ImageConverter imageConverter = new ImageConverter();
byte[] byteArr = (byte[])imageConverter.ConvertTo(image, typeof(byte[]));
if (compress)
{
return byteArr.Compress();
}
return byteArr;
}
/// <summary>
/// Convert a blob (byte array) to Image. Automatically checks for compression.
/// </summary>
/// <param name="byteArray"></param>
/// <returns></returns>
public static Image ToImage(this byte[] byteArray)
{
if (byteArray == null)
{
throw new ArgumentNullException(nameof(byteArray), "Byte array was null.");
}
using (MemoryStream ms = new MemoryStream(byteArray))
{
return Image.FromStream(ms);
}
}
/// <summary>
/// Resize an image. Maintains aspect ratio
/// </summary>
/// <param name="image">Image to apply this to</param>
/// <param name="size"> new size parameters</param>
/// <returns></returns>
public static Image Resize(this Image image, Size size)
{
if (image == null)
{
throw new ArgumentNullException(nameof(image), "image passed as null.");
}
if (size == null)
{
throw new ArgumentNullException(nameof(size), "Size cannot be null");
}
int sourceWidth = image.Width;
int sourceHeight = image.Height;
float nPercentW = size.Width / (float)sourceWidth;
float nPercentH = size.Height / (float)sourceHeight;
float nPercent;
if (nPercentH < nPercentW)
{
nPercent = nPercentH;
}
else
{
nPercent = nPercentW;
}
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap b = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage((Image)b);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(image, 0, 0, destWidth, destHeight);
g.Dispose();
return (Image)b;
}
}
/// <summary>
/// ImageMetaData class. Used to hold basic image meta data for quick access.
/// </summary>
public class ImageMetaData
{
/// <summary>
/// File name of the image
/// </summary>
public string ImageName { get; set; }
/// <summary>
/// File extension of the image
/// </summary>
public string ImageExtension { get; set; }
/// <summary>
/// Mime type for the image
/// </summary>
public string MimeType { get; set; }
}
}