From 14312cb5c6f3f21742750e501adf0bb48522b1d7 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Mon, 5 Jun 2023 07:01:44 +0300 Subject: Add thumbnail file creation --- Services/IImageResizer.cs | 8 ++++++++ Services/ImageResizer.cs | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 Services/IImageResizer.cs create mode 100644 Services/ImageResizer.cs (limited to 'Services') diff --git a/Services/IImageResizer.cs b/Services/IImageResizer.cs new file mode 100644 index 0000000..de02893 --- /dev/null +++ b/Services/IImageResizer.cs @@ -0,0 +1,8 @@ +namespace MyDarling.Services +{ + public interface IImageResizer + { + public void CreateThumbnail(string filePath); + public void DownsizeImage(string filePath); + } +} \ No newline at end of file diff --git a/Services/ImageResizer.cs b/Services/ImageResizer.cs new file mode 100644 index 0000000..53c1eec --- /dev/null +++ b/Services/ImageResizer.cs @@ -0,0 +1,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(); + } + } +} \ No newline at end of file -- cgit v1.2.3