44 lines
1.4 KiB
C#
44 lines
1.4 KiB
C#
|
|
using SkiaSharp;
|
|
|
|
namespace NeDvachAPI.DBControllers
|
|
{
|
|
public class ImageController
|
|
{
|
|
public static bool CheckExtention(string extentionToCheck)
|
|
{
|
|
var supportedTypes = new[] { ".jpg", ".png", ".jpeg" };
|
|
if (supportedTypes.Contains(extentionToCheck.ToLower()))
|
|
{
|
|
return true;
|
|
}
|
|
else return false;
|
|
}
|
|
|
|
public static bool ResizeImage(string picPath, string smallPath, int width, int height)
|
|
{
|
|
|
|
var bitmap = SKBitmap.Decode(picPath);
|
|
|
|
double ratio = Math.Max((double)bitmap.Width / (double)width , (double)bitmap.Height / (double)height);
|
|
int targetWidth = (int)(bitmap.Width/ ratio);
|
|
int targetHeight = (int)(bitmap.Height / ratio);
|
|
|
|
Console.WriteLine($"Ресайзим картинку до {targetWidth} ширины и {targetHeight} высоты.");
|
|
|
|
var dstInfo = new SKImageInfo(targetWidth , targetHeight);
|
|
var image = SKImage.FromBitmap(bitmap.Resize(dstInfo, SKFilterQuality.Medium));
|
|
var data = image.Encode(SKEncodedImageFormat.Jpeg, 90);
|
|
|
|
using (var stream = new FileStream(smallPath, FileMode.Create, FileAccess.Write))
|
|
data.SaveTo(stream);
|
|
|
|
data.Dispose();
|
|
image.Dispose();
|
|
bitmap.Dispose();
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
|