-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathImageResize.cs
239 lines (213 loc) · 10.3 KB
/
ImageResize.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
using LazZiya.ImageResize.ColorFormats;
using LazZiya.ImageResize.ResizeMethods;
using LazZiya.ImageResize.Tools;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
namespace LazZiya.ImageResize
{
/// <summary>
/// Resize images
/// </summary>
public static class ImageResize
{
/// <summary>
/// Auto scale image by width or height till longest border (width/height) is equal to new width/height.
/// Final image aspect ratio is equal to original image aspect ratio.
/// If the aspect ratio of new w/h != aspect ratio of original image then
/// one border will be in different size than the given value in order to keep original aspect ratio
/// </summary>
/// <param name="img"></param>
/// <param name="newWidth"></param>
/// <param name="newHeight"></param>
/// <returns></returns>
public static Image Scale(this Image img, int newWidth, int newHeight)
{
var resize = new Scale(img.Size, new Size(newWidth, newHeight));
return Resize(img, resize.SourceRect, resize.TargetRect);
}
/// <summary>
/// Auto scale image by width or height till longest border (width/height) is equal to new width/height.
/// Final image aspect ratio is equal to original image aspect ratio.
/// If the aspect ratio of new w/h != aspect ratio of original image then
/// one border will be in different size than the given value in order to keep original aspect ratio
/// </summary>
/// <param name="img"></param>
/// <param name="newWidth"></param>
/// <param name="newHeight"></param>
/// <param name="ops">Graphic options <see cref="GraphicOptions"/></param>
/// <returns></returns>
public static Image Scale(this Image img, int newWidth, int newHeight, GraphicOptions ops)
{
var resize = new Scale(img.Size, new Size(newWidth, newHeight));
return Resize(img, resize.SourceRect, resize.TargetRect, ops);
}
/// <summary>
/// Scale image by width and keep same aspect ratio of target image same as the original image.
/// Height will be adjusted automatically
/// </summary>
/// <param name="img"></param>
/// <param name="newWidth"></param>
/// <returns></returns>
public static Image ScaleByWidth(this Image img, int newWidth)
{
var resize = new Scale(img.Size, new Size(newWidth, 0));
return Resize(img, resize.SourceRect, resize.TargetRect);
}
/// <summary>
/// Scale image by width and keep same aspect ratio of target image same as the original image.
/// Height will be adjusted automatically
/// </summary>
/// <param name="img"></param>
/// <param name="newWidth"></param>
/// <param name="ops">Graphic options <see cref="GraphicOptions"/></param>
/// <returns></returns>
public static Image ScaleByWidth(this Image img, int newWidth, GraphicOptions ops)
{
var resize = new Scale(img.Size, new Size(newWidth, 0));
return Resize(img, resize.SourceRect, resize.TargetRect, ops);
}
/// <summary>
/// Scale image by height and keep same aspect ratio of target image same as the original image.
/// Width will be adjusted automatically
/// </summary>
/// <param name="img"></param>
/// <param name="newHeight"></param>
/// <returns></returns>
public static Image ScaleByHeight(this Image img, int newHeight)
{
var resize = new Scale(img.Size, new Size(0, newHeight));
return Resize(img, resize.SourceRect, resize.TargetRect);
}
/// <summary>
/// Scale image by height and keep same aspect ratio of target image same as the original image.
/// Width will be adjusted automatically
/// </summary>
/// <param name="img"></param>
/// <param name="newHeight"></param>
/// <param name="ops">Graphic options <see cref="GraphicOptions"/></param>
/// <returns></returns>
public static Image ScaleByHeight(this Image img, int newHeight, GraphicOptions ops)
{
var resize = new Scale(img.Size, new Size(0, newHeight));
return Resize(img, resize.SourceRect, resize.TargetRect, ops);
}
/// <summary>
/// Scale target image till shortest border are equal to target value,
/// then crop the additonal pixels from the longest border.
/// Final image aspect ratio is equal to the given new width/height
/// </summary>
/// <param name="img"></param>
/// <param name="newWidth"></param>
/// <param name="newHeight"></param>
/// <param name="spot"></param>
/// <returns></returns>
public static Image ScaleAndCrop(this Image img, int newWidth, int newHeight, TargetSpot spot = TargetSpot.Center)
{
var resize = new ScaleAndCrop(img.Size, new Size(newWidth, newHeight), spot);
return Resize(img, resize.SourceRect, resize.TargetRect);
}
/// <summary>
/// Scale target image till shortest border are equal to target value,
/// then crop the additonal pixels from the longest border.
/// Final image aspect ratio is equal to the given new width/height
/// </summary>
/// <param name="img"></param>
/// <param name="newWidth"></param>
/// <param name="newHeight"></param>
/// <param name="spot"></param>
/// <param name="ops">Graphic options <see cref="GraphicOptions"/></param>
/// <returns></returns>
public static Image ScaleAndCrop(this Image img, int newWidth, int newHeight, GraphicOptions ops, TargetSpot spot = TargetSpot.Center)
{
var resize = new ScaleAndCrop(img.Size, new Size(newWidth, newHeight), spot);
return Resize(img, resize.SourceRect, resize.TargetRect, ops);
}
/// <summary>
/// Directly crop original image without scaling it.
/// Final image aspect ratio is equal to given new width/height
/// </summary>
/// <param name="img"></param>
/// <param name="newWidth"></param>
/// <param name="newHeight"></param>
/// <param name="spot">target spot to crop and save</param>
/// <returns></returns>
public static Image Crop(this Image img, int newWidth, int newHeight, TargetSpot spot = TargetSpot.Center)
{
var resize = new Crop(img.Size, new Size(newWidth, newHeight), spot);
return Resize(img, resize.SourceRect, resize.TargetRect);
}
/// <summary>
/// Directly crop original image without scaling it.
/// Final image aspect ratio is equal to given new width/height
/// </summary>
/// <param name="img"></param>
/// <param name="newWidth"></param>
/// <param name="newHeight"></param>
/// <param name="spot">target spot to crop and save</param>
/// <param name="ops">Graphic options <see cref="GraphicOptions"/></param>
/// <returns></returns>
public static Image Crop(this Image img, int newWidth, int newHeight, GraphicOptions ops, TargetSpot spot = TargetSpot.Center)
{
var resize = new Crop(img.Size, new Size(newWidth, newHeight), spot);
return Resize(img, resize.SourceRect, resize.TargetRect, ops);
}
/// <summary>
/// Specify custom resize options
/// </summary>
/// <param name="img">the image to resize</param>
/// <param name="source">The coordinates to read as source from the image,
/// can be the whole image or part of it</param>
/// <param name="target">The coordinates of the target image size</param>
/// <returns></returns>
public static Image Resize(this Image img, Rectangle source, Rectangle target)
{
return img.Resize(source, target, new GraphicOptions());
}
/// <summary>
/// Specify custom resize options
/// </summary>
/// <param name="img">the image to resize</param>
/// <param name="source">The coordinates to read as source from the image,
/// can be the whole image or part of it</param>
/// <param name="target">The coordinates of the target image size</param>
/// <param name="ops">Graphic options <see cref="GraphicOptions"/></param>
/// <returns></returns>
public static Image Resize(this Image img, Rectangle source, Rectangle target, GraphicOptions ops)
{
// check for CMYK pixel format to use Format32bppArgb
// or use the image pixel format
var pixF = ImageColorFormats.GetColorFormat((Bitmap)img) == ImageColorFormat.Cmyk
? PixelFormat.Format32bppArgb
: img.PixelFormat;
using (Bitmap outputImage = new Bitmap(target.Width, target.Height, pixF))
{
outputImage.SetResolution(img.HorizontalResolution, img.VerticalResolution);
using (var graphics = Graphics.FromImage(outputImage))
{
graphics.SmoothingMode = ops.SmoothingMode;
graphics.InterpolationMode = ops.InterpolationMode;
graphics.PixelOffsetMode = ops.PixelOffsetMode;
graphics.CompositingQuality = ops.CompositingQuality;
graphics.CompositingMode = ops.CompositingMode;
graphics.PageUnit = ops.PageUnit;
graphics.DrawImage(
img,
target,
source,
ops.PageUnit);
}
// If the image has alpha channel (png) return image memory stream
if (img.PixelFormat == PixelFormat.Format32bppArgb)
{
using (var ms = new MemoryStream())
{
outputImage.Save(ms, img.RawFormat);
return Image.FromStream(ms);
}
}
return Image.FromHbitmap(outputImage.GetHbitmap());
}
}
}
}