43 lines
1.1 KiB
C#
43 lines
1.1 KiB
C#
|
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();
|
||
|
}
|
||
|
}
|
||
|
}
|