-
Notifications
You must be signed in to change notification settings - Fork 3
/
ImageResizeFunctions.cs
95 lines (83 loc) · 4.06 KB
/
ImageResizeFunctions.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
using System.IO;
using System.Numerics;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Processing.Drawing;
using SixLabors.ImageSharp.Processing.Transforms;
using SixLabors.Primitives;
using SixLabors.Shapes;
namespace AzureFuncSamples.ImageResize
{
public static class ImageResizeFunctions
{
public static byte[] GenerateThumbnailByPercentage(Stream imageStream, int resizeByPercentage)
{
using (var image = Image.Load(imageStream))
{
image.Mutate(x =>
x.Resize((image.Width * resizeByPercentage) / 100, (image.Height * resizeByPercentage) / 100));
using (var returnImageStream = new MemoryStream())
{
image.Save(returnImageStream, new JpegEncoder());
return returnImageStream.ToArray();
}
}
}
public static byte[] GenerateThumbnailByWidthHeight(Image<Rgba32> image, int w, int h)
{
image.Mutate(x => x.Resize(w, h));
using (var returnImageStream = new MemoryStream())
{
image.Save(returnImageStream, new JpegEncoder());
return returnImageStream.ToArray();
}
}
public static byte[] GenerateAvatar(Image<Rgba32> image, int w, int h, float radius)
{
var avtr = image.Clone(x => x.ConvertToAvatar(new Size(w, h), radius));
using (var returnImageStream = new MemoryStream())
{
avtr.Save(returnImageStream, new PngEncoder());
return returnImageStream.ToArray();
}
}
public static IImageProcessingContext<Rgba32> ConvertToAvatar(this IImageProcessingContext<Rgba32> processingContext, Size size, float cornerRadius)
{
return processingContext.Resize(new ResizeOptions
{
Size = size,
Mode = ResizeMode.Crop
}).Apply(i => ApplyRoundedCorners(i, cornerRadius));
}
public static void ApplyRoundedCorners(Image<Rgba32> img, float cornerRadius)
{
IPathCollection corners = BuildCorners(img.Width, img.Height, cornerRadius);
var graphicOptions = new GraphicsOptions(true)
{
BlenderMode = PixelBlenderMode.Src // enforces that any part of this shape that has color is punched out of the background
};
// mutating in here as we already have a cloned original
img.Mutate(x => x.Fill(graphicOptions, Rgba32.Transparent, corners));
}
public static IPathCollection BuildCorners(int imageWidth, int imageHeight, float cornerRadius)
{
// first create a square
var rect = new RectangularPolygon(-0.5f, -0.5f, cornerRadius, cornerRadius);
// then cut out of the square a circle so we are left with a corner
IPath cornerToptLeft = rect.Clip(new EllipsePolygon(cornerRadius - 0.5f, cornerRadius - 0.5f, cornerRadius));
// corner is now a corner shape positions top left
//lets make 3 more positioned correctly, we can do that by translating the orgional artound the center of the image
var center = new Vector2(imageWidth / 2F, imageHeight / 2F);
float rightPos = imageWidth - cornerToptLeft.Bounds.Width + 1;
float bottomPos = imageHeight - cornerToptLeft.Bounds.Height + 1;
// move it across the widthof the image - the width of the shape
IPath cornerTopRight = cornerToptLeft.RotateDegree(90).Translate(rightPos, 0);
IPath cornerBottomLeft = cornerToptLeft.RotateDegree(-90).Translate(0, bottomPos);
IPath cornerBottomRight = cornerToptLeft.RotateDegree(180).Translate(rightPos, bottomPos);
return new PathCollection(cornerToptLeft, cornerBottomLeft, cornerTopRight, cornerBottomRight);
}
}
}