diff options
Diffstat (limited to 'Services/ImageResizer.cs')
-rw-r--r-- | Services/ImageResizer.cs | 43 |
1 files changed, 43 insertions, 0 deletions
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 |