-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathImageProvider.cs
More file actions
96 lines (81 loc) · 3.19 KB
/
ImageProvider.cs
File metadata and controls
96 lines (81 loc) · 3.19 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
#nullable enable
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;
using ServiceStack.Text;
namespace ServiceStack;
public abstract class ImageProvider
{
public static ImageProvider Instance { get; set; } = new ImageDrawingProvider();
public abstract Stream Resize(Stream stream, int newWidth, int newHeight);
public virtual Stream Resize(Stream origStream, string? savePhotoSize = null)
{
var parts = savePhotoSize?.Split('x');
if (parts is { Length: > 1 } &&
int.TryParse(parts[0], out var width) && int.TryParse(parts[1], out var height))
return Resize(origStream, width, height);
return origStream;
}
}
public class ImageDrawingProvider : ImageProvider
{
public override Stream Resize(Stream origStream, int newWidth, int newHeight)
{
using var origImage = Image.FromStream(origStream);
return origImage.ResizeToPng(newWidth, newHeight);
}
}
public static class ImageExtensions
{
public static MemoryStream ResizeToPng(this Image img, int newWidth, int newHeight)
{
if (newWidth != img.Width || newHeight != img.Height)
{
var ratioX = (double)newWidth / img.Width;
var ratioY = (double)newHeight / img.Height;
var ratio = Math.Max(ratioX, ratioY);
var width = (int)(img.Width * ratio);
var height = (int)(img.Height * ratio);
using var newImage = new Bitmap(width, height);
Graphics.FromImage(newImage).DrawImage(img, 0, 0, width, height);
if (newImage.Width != newWidth || newImage.Height != newHeight)
{
var startX = (Math.Max(newImage.Width, newWidth) - Math.Min(newImage.Width, newWidth)) / 2;
var startY = (Math.Max(newImage.Height, newHeight) - Math.Min(newImage.Height, newHeight)) / 2;
return CropToPng(img, newWidth, newHeight, startX, startY);
}
var ms = MemoryStreamFactory.GetStream();
newImage.Save(ms, ImageFormat.Png);
ms.Position = 0;
return ms;
}
else
{
var ms = MemoryStreamFactory.GetStream();
img.Save(ms, ImageFormat.Png);
ms.Position = 0;
return ms;
}
}
public static MemoryStream CropToPng(this Image img, int newWidth, int newHeight, int startX = 0, int startY = 0)
{
if (img.Height < newHeight)
newHeight = img.Height;
if (img.Width < newWidth)
newWidth = img.Width;
using var bmp = new Bitmap(newWidth, newHeight, PixelFormat.Format24bppRgb);
bmp.SetResolution(72, 72);
using var g = Graphics.FromImage(bmp);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.DrawImage(img, new Rectangle(0, 0, newWidth, newHeight), startX, startY, newWidth, newHeight,
GraphicsUnit.Pixel);
var ms = MemoryStreamFactory.GetStream();
bmp.Save(ms, ImageFormat.Png);
ms.Position = 0;
return ms;
}
}