diff options
author | Sergey Chebotar <s.chebotar@gmail.com> | 2023-06-05 07:01:44 +0300 |
---|---|---|
committer | Sergey Chebotar <s.chebotar@gmail.com> | 2023-06-05 07:01:44 +0300 |
commit | 14312cb5c6f3f21742750e501adf0bb48522b1d7 (patch) | |
tree | 61ec7317bf48cf71b9f122dbac62d8af566f4d6a /Services | |
parent | e5416aff886957886ab2a6a7e27320bed49a48bd (diff) |
Add thumbnail file creation
Diffstat (limited to 'Services')
-rw-r--r-- | Services/IImageResizer.cs | 8 | ||||
-rw-r--r-- | Services/ImageResizer.cs | 43 |
2 files changed, 51 insertions, 0 deletions
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 |