-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageHelper.cs
181 lines (155 loc) · 6.22 KB
/
ImageHelper.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
using Microsoft.AspNetCore.Http;
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Collections.Generic;
namespace littlebreadloaf
{
public class ImageHelper
{
private readonly string _sourceID;
public enum ImageResizeMode
{
Square = 0,
Banner = 1,
AspectRatio = 2
}
#region "Public Functions - Image Upload"
public ImageHelper(string sourceID)
{
_sourceID = sourceID;
}
public void BuildDirectory(int size)
{
string sourceDirectory = SourceDirectory();
if (!Directory.Exists(sourceDirectory))
{
Directory.CreateDirectory(sourceDirectory);
}
}
public bool HasDirectory()
{
return Directory.Exists(SourceDirectory());
}
public string SourceDirectory()
{
return Path.Combine(Environment.CurrentDirectory,
"wwwroot",
"images",
_sourceID);
}
public string GetNewFileName(int size, string SourceImageID)
{
return Path.ChangeExtension(Path.Combine(SourceDirectory(), String.Concat(SourceImageID,"_",size)),".png");
}
public string GetDisplayFileName(string SourceImageID)
{
return string.Concat("/images/", _sourceID, "/", SourceImageID,"_{1}.png");
}
public void DeleteAll()
{
var di = new DirectoryInfo(SourceDirectory());
if(di.Exists)
{
foreach (var file in di.GetFiles())
{
if (file.Exists)
{
file.Delete();
}
}
di.Delete();
}
}
public void DeleteImage(string sourceImageID)
{
var di = new DirectoryInfo(SourceDirectory());
foreach(var file in di.GetFiles().Where(f=>f.Name.StartsWith(sourceImageID)))
{
if(file.Exists)
{
file.Delete();
}
}
}
public void AddImages(ImageResizeMode mode,
string sourceImageID,
Size[] sizes,
IFormFile upload)
{
foreach (var size in sizes)
{
SaveImage(mode, sourceImageID, size, upload);
}
}
#endregion
#region "Private Functions"
private void SaveImage(ImageResizeMode mode,
string souceImageID,
Size size,
IFormFile upload)
{
Rectangle destRect;
Rectangle srcRect;
int width;
int height;
using (var stmImage = new MemoryStream())
{
upload.CopyTo(stmImage);
using (var imgRaw = Image.FromStream(stmImage))
{
switch (mode)
{
case ImageResizeMode.Banner: //sizes are known
destRect = new Rectangle(0, 0, size.Width, size.Height);
srcRect = new Rectangle(0, 0, imgRaw.Width, imgRaw.Height);
width = size.Width;
height = size.Width;
break;
case ImageResizeMode.AspectRatio:
decimal scale = Math.Min(decimal.Divide(size.Width, imgRaw.Width), decimal.Divide(size.Height, imgRaw.Height));
width = (int)(imgRaw.Width * scale);
height = (int)(imgRaw.Height * scale);
destRect = new Rectangle(0, 0, width, height);
srcRect = new Rectangle(0, 0, imgRaw.Width, imgRaw.Height);
size = new Size(width, height);
break;
case ImageResizeMode.Square:
int posY = 0, posX = 0;
if (imgRaw.Height > imgRaw.Width)
posY = (imgRaw.Height - imgRaw.Width) / 2;
else
posX = (imgRaw.Width - imgRaw.Height) / 2;
width = size.Width;
height = size.Height;
destRect = new Rectangle(0, 0, size.Width, size.Height);
srcRect = new Rectangle(posX, posY, imgRaw.Width - posX * 2, imgRaw.Height - posY * 2);
break;
default:
throw new ArgumentException("Invalid image resize mode.");
}
using (var imgNew = new Bitmap(imgRaw, size))
{
using (var graphics = Graphics.FromImage(imgNew))
{
//graphics.FillRectangle(new SolidBrush(Color.White), 0, 0, width, height);
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphics.DrawImage(imgRaw, destRect, srcRect, GraphicsUnit.Pixel);
}
var imgHelper = new ImageHelper(_sourceID);
imgHelper.BuildDirectory(size.Width);
string fileName = imgHelper.GetNewFileName(size.Width, souceImageID);
imgNew.Save(fileName, ImageFormat.Jpeg);
}
}
}
}
#endregion
}
}