-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathImageResizeConditional.cs
203 lines (190 loc) · 10.4 KB
/
ImageResizeConditional.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
using System.Drawing;
namespace LazZiya.ImageResize
{
/// <summary>
/// Conditionally resize images depending on a condition token
/// </summary>
public static class ImageResizeConditional
{
/// <summary>
/// Do conditional resize if the the condition is true, otherwise return without resizing.
/// 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="condition">true to resize, false will return the img</param>
/// <param name="newWidth"></param>
/// <param name="newHeight"></param>
/// <returns></returns>
public static Image ScaleIf(this Image img, bool condition, int newWidth, int newHeight)
{
return condition ? ImageResize.Scale(img, newWidth, newHeight) : img;
}
/// <summary>
/// Do conditional resize if the the condition is true, otherwise return without resizing.
/// 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="condition">true to resize, false will return the img</param>
/// <param name="newWidth"></param>
/// <param name="newHeight"></param>
/// <param name="ops">Graphic options <see cref="GraphicOptions"/></param>
/// <returns></returns>
public static Image ScaleIf(this Image img, bool condition, int newWidth, int newHeight, GraphicOptions ops)
{
return condition ? ImageResize.Scale(img, newWidth, newHeight, ops) : img;
}
/// <summary>
/// Do conditional resize if the the condition is true, otherwise return without resizing.
/// 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="condition">true to resize, false will return the img</param>
/// <param name="newWidth"></param>
/// <returns></returns>
public static Image ScaleByWidthIf(this Image img, bool condition, int newWidth)
{
return condition ? ImageResize.ScaleByWidth(img, newWidth) : img;
}
/// <summary>
/// Do conditional resize if the the condition is true, otherwise return without resizing.
/// 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="condition">true to resize, false will return the img</param>
/// <param name="newWidth"></param>
/// <param name="ops">Graphic options <see cref="GraphicOptions"/></param>
/// <returns></returns>
public static Image ScaleByWidthIf(this Image img, bool condition, int newWidth, GraphicOptions ops)
{
return condition ? ImageResize.ScaleByWidth(img, newWidth, ops) : img;
}
/// <summary>
/// Do conditional resize if the the condition is true, otherwise return without resizing.
/// 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="condition">true to resize, false will return the img</param>
/// <param name="newHeight"></param>
/// <returns></returns>
public static Image ScaleByHeightIf(this Image img, bool condition, int newHeight)
{
return condition ? ImageResize.ScaleByHeight(img, newHeight) : img;
}
/// <summary>
/// Do conditional resize if the the condition is true, otherwise return without resizing.
/// 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="condition">true to resize, false will return the img</param>
/// <param name="newHeight"></param>
/// <param name="ops">Graphic options <see cref="GraphicOptions"/></param>
/// <returns></returns>
public static Image ScaleByHeightIf(this Image img, bool condition, int newHeight, GraphicOptions ops)
{
return condition ? ImageResize.ScaleByHeight(img, newHeight, ops) : img;
}
/// <summary>
/// Do conditional resize if the the condition is true, otherwise return without resizing.
/// 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="condition">true to resize, false will return the img</param>
/// <param name="newWidth"></param>
/// <param name="newHeight"></param>
/// <param name="spot"></param>
/// <returns></returns>
public static Image ScaleAndCropIf(this Image img, bool condition, int newWidth, int newHeight, TargetSpot spot = TargetSpot.Center)
{
return condition ? ImageResize.ScaleAndCrop(img, newWidth, newHeight, spot) : img;
}
/// <summary>
/// Do conditional resize if the the condition is true, otherwise return without resizing.
/// 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="condition">true to resize, false will return the 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 ScaleAndCropIf(this Image img, bool condition, int newWidth, int newHeight, GraphicOptions ops, TargetSpot spot = TargetSpot.Center)
{
return condition ? ImageResize.ScaleAndCrop(img, newWidth, newHeight, ops, spot) : img;
}
/// <summary>
/// Do conditional resize if the the condition is true, otherwise return without resizing.
/// 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="condition">true to resize, false will return the 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 CropIf(this Image img, bool condition, int newWidth, int newHeight, TargetSpot spot = TargetSpot.Center)
{
return condition ? ImageResize.Crop(img, newWidth, newHeight, spot) : img;
}
/// <summary>
/// Do conditional resize if the the condition is true, otherwise return without resizing.
/// 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="condition">true to resize, false will return the 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 CropIf(this Image img, bool condition, int newWidth, int newHeight, GraphicOptions ops, TargetSpot spot = TargetSpot.Center)
{
return condition ? ImageResize.Crop(img, newWidth, newHeight, ops, spot) : img;
}
/// <summary>
/// Do conditional resize if the the condition is true, otherwise return without resizing.
/// Specify custom resize options
/// </summary>
/// <param name="img">the image to resize</param>
/// <param name="condition">true to resize, false will return the img</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 ResizeIf(this Image img, bool condition, Rectangle source, Rectangle target)
{
return condition ? ImageResize.Resize(img, source, target) : img;
}
/// <summary>
/// Do conditional resize if the the condition is true, otherwise return without resizing.
/// Specify custom resize options
/// </summary>
/// <param name="img">the image to resize</param>
/// <param name="condition">true to resize, false will return the img</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 ResizeIf(this Image img, bool condition, Rectangle source, Rectangle target, GraphicOptions ops)
{
return condition ? ImageResize.Resize(img, source, target, ops) : img;
}
}
}