Fixed perf issues

This commit is contained in:
TheXamlGuy
2024-07-02 23:38:52 +01:00
parent 757d938081
commit 3f9d6bf78f
10 changed files with 86 additions and 66 deletions
+44 -36
View File
@@ -6,48 +6,56 @@ namespace Toolkit.Avalonia;
public class ImageResizer :
IImageResizer
{
public Bitmap Resize(Stream stream,
int targetWidth,
int targetHeight,
public Bitmap Resize(Stream stream,
int targetWidth,
int targetHeight,
bool maintainAspectRatio)
{
using SKBitmap original = SKBitmap.Decode(stream);
Bitmap bitmap = new(stream);
float widthRatio = (float)targetWidth / original.Width;
float heightRatio = (float)targetHeight / original.Height;
float scale = maintainAspectRatio ? Math.Max(widthRatio, heightRatio) : Math.Min(widthRatio, heightRatio);
int newWidth = (int)(original.Width * scale);
int newHeight = (int)(original.Height * scale);
using SKBitmap resized = new(newWidth, newHeight);
using SKCanvas canvas = new(resized);
canvas.Clear(SKColors.Transparent);
canvas.DrawBitmap(original, new SKRect(0, 0, newWidth, newHeight));
SKBitmap cropped;
if (maintainAspectRatio)
if (bitmap.Size.Width != targetWidth || bitmap.Size.Height != targetHeight)
{
int cropX = (newWidth - targetWidth) / 2;
int cropY = (newHeight - targetHeight) / 2;
using SKBitmap sKBitmap = SKBitmap.Decode(stream);
SKBitmap? cropped = null;
bitmap.Dispose();
cropped = new SKBitmap(targetWidth, targetHeight);
using SKCanvas croppedCanvas = new(cropped);
SKRect cropRect = new(cropX, cropY, cropX + targetWidth, cropY + targetHeight);
croppedCanvas.Clear(SKColors.Transparent);
croppedCanvas.DrawBitmap(resized, cropRect, new SKRect(0, 0, targetWidth, targetHeight));
}
else
{
cropped = resized;
float widthRatio = (float)targetWidth / sKBitmap.Width;
float heightRatio = (float)targetHeight / sKBitmap.Height;
float scale = maintainAspectRatio ? Math.Max(widthRatio, heightRatio) : Math.Min(widthRatio, heightRatio);
int newWidth = (int)(sKBitmap.Width * scale);
int newHeight = (int)(sKBitmap.Height * scale);
using SKBitmap resized = new(newWidth, newHeight);
using SKCanvas canvas = new(resized);
canvas.Clear(SKColors.Transparent);
canvas.DrawBitmap(sKBitmap, new SKRect(0, 0, newWidth, newHeight));
if (maintainAspectRatio)
{
int cropX = (newWidth - targetWidth) / 2;
int cropY = (newHeight - targetHeight) / 2;
cropped = new SKBitmap(targetWidth, targetHeight);
using SKCanvas croppedCanvas = new(cropped);
SKRect cropRect = new(cropX, cropY, cropX + targetWidth, cropY + targetHeight);
croppedCanvas.Clear(SKColors.Transparent);
croppedCanvas.DrawBitmap(resized, cropRect, new SKRect(0, 0, targetWidth, targetHeight));
}
else
{
cropped = resized;
}
using SKImage image = SKImage.FromBitmap(cropped);
using MemoryStream outputStream = new();
image.Encode(SKEncodedImageFormat.Png, 100).SaveTo(outputStream);
outputStream.Seek(0, SeekOrigin.Begin);
return new Bitmap(outputStream);
}
using SKImage image = SKImage.FromBitmap(cropped);
using MemoryStream outputStream = new();
image.Encode(SKEncodedImageFormat.Png, 100).SaveTo(outputStream);
outputStream.Seek(0, SeekOrigin.Begin);
return new Bitmap(outputStream);
return bitmap;
}
}