summaryrefslogtreecommitdiff
path: root/Services/ImageResizer.cs
blob: 53c1eecca366ba7647236dfaeaa4f86825989631 (plain)
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
using SkiaSharp;

namespace MyDarling.Services
{
    public class ImageResizer : IImageResizer
	{
		const int size = 400;
		const int quality = 75;
		public void CreateThumbnail(string inputPath)
		{
			using var input = File.OpenRead(inputPath);
			using var inputStream = new SKManagedStream(input);
			using var original = SKBitmap.Decode(inputStream);

			int width, height;
			if (original.Width > original.Height)
			{
				width = size;
				height = original.Height * size / original.Width;
			}
			else
			{
				width = original.Width * size / original.Height;
				height = size;
			}

			using var resized = original.Resize(new SKImageInfo(width, height), SKFilterQuality.High);
			if (resized == null) return;

			using var image = SKImage.FromBitmap(resized);
			var outputPath = Path.GetDirectoryName(inputPath) + "\\" +
				Path.GetFileNameWithoutExtension(inputPath) + "_thumb.jpg";
			using var output = File.OpenWrite(outputPath);

			image.Encode(SKEncodedImageFormat.Jpeg, quality).SaveTo(output);
		}

		public void DownsizeImage(string filePath)
		{
			throw new NotImplementedException();
		}
	}
}