Fixed a bug where we if the bitmap needs resizing and cropping, we need to seek the stream back to the begining
This commit is contained in:
@@ -6,52 +6,44 @@ namespace Toolkit.Avalonia;
|
|||||||
public class ImageResizer :
|
public class ImageResizer :
|
||||||
IImageResizer
|
IImageResizer
|
||||||
{
|
{
|
||||||
public Bitmap Resize(Stream stream,
|
public Bitmap Resize(Stream stream, int targetWidth, int targetHeight, bool maintainAspectRatio)
|
||||||
int targetWidth,
|
|
||||||
int targetHeight,
|
|
||||||
bool maintainAspectRatio)
|
|
||||||
{
|
{
|
||||||
Bitmap bitmap = new(stream);
|
Bitmap bitmap = new(stream);
|
||||||
|
|
||||||
if (bitmap.Size.Width != targetWidth || bitmap.Size.Height != targetHeight)
|
if (bitmap.Size.Width != targetWidth || bitmap.Size.Height != targetHeight)
|
||||||
{
|
{
|
||||||
using SKBitmap sKBitmap = SKBitmap.Decode(stream);
|
stream.Seek(0, SeekOrigin.Begin);
|
||||||
SKBitmap? cropped = null;
|
|
||||||
bitmap.Dispose();
|
|
||||||
|
|
||||||
|
using SKBitmap sKBitmap = SKBitmap.Decode(stream);
|
||||||
float widthRatio = (float)targetWidth / sKBitmap.Width;
|
float widthRatio = (float)targetWidth / sKBitmap.Width;
|
||||||
float heightRatio = (float)targetHeight / sKBitmap.Height;
|
float heightRatio = (float)targetHeight / sKBitmap.Height;
|
||||||
float scale = maintainAspectRatio ? Math.Max(widthRatio, heightRatio) : Math.Min(widthRatio, heightRatio);
|
float scale = maintainAspectRatio ? Math.Min(widthRatio, heightRatio) : Math.Max(widthRatio, heightRatio);
|
||||||
|
|
||||||
int newWidth = (int)(sKBitmap.Width * scale);
|
int newWidth = (int)(sKBitmap.Width * scale);
|
||||||
int newHeight = (int)(sKBitmap.Height * scale);
|
int newHeight = (int)(sKBitmap.Height * scale);
|
||||||
|
|
||||||
using SKBitmap resized = new(newWidth, newHeight);
|
using SKBitmap resized = sKBitmap.Resize(new SKImageInfo(newWidth, newHeight), SKFilterQuality.High);
|
||||||
using SKCanvas canvas = new(resized);
|
|
||||||
|
|
||||||
canvas.Clear(SKColors.Transparent);
|
|
||||||
canvas.DrawBitmap(sKBitmap, new SKRect(0, 0, newWidth, newHeight));
|
|
||||||
|
|
||||||
|
SKBitmap cropped = resized;
|
||||||
if (maintainAspectRatio)
|
if (maintainAspectRatio)
|
||||||
{
|
{
|
||||||
int cropX = (newWidth - targetWidth) / 2;
|
int cropX = (resized.Width - targetWidth) / 2;
|
||||||
int cropY = (newHeight - targetHeight) / 2;
|
int cropY = (resized.Height - targetHeight) / 2;
|
||||||
|
|
||||||
cropped = new SKBitmap(targetWidth, targetHeight);
|
cropped = new SKBitmap(targetWidth, targetHeight);
|
||||||
using SKCanvas croppedCanvas = new(cropped);
|
using (SKCanvas croppedCanvas = new(cropped))
|
||||||
SKRect cropRect = new(cropX, cropY, cropX + targetWidth, cropY + targetHeight);
|
{
|
||||||
croppedCanvas.Clear(SKColors.Transparent);
|
SKRect srcRect = new(cropX, cropY, cropX + targetWidth, cropY + targetHeight);
|
||||||
croppedCanvas.DrawBitmap(resized, cropRect, new SKRect(0, 0, targetWidth, targetHeight));
|
SKRect destRect = new(0, 0, targetWidth, targetHeight);
|
||||||
}
|
croppedCanvas.DrawBitmap(resized, srcRect, destRect);
|
||||||
else
|
}
|
||||||
{
|
|
||||||
cropped = resized;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
using SKImage image = SKImage.FromBitmap(cropped);
|
using SKImage image = SKImage.FromBitmap(cropped);
|
||||||
using MemoryStream outputStream = new();
|
using MemoryStream outputStream = new MemoryStream();
|
||||||
image.Encode(SKEncodedImageFormat.Png, 100).SaveTo(outputStream);
|
image.Encode(SKEncodedImageFormat.Png, 100).SaveTo(outputStream);
|
||||||
outputStream.Seek(0, SeekOrigin.Begin);
|
outputStream.Seek(0, SeekOrigin.Begin);
|
||||||
|
bitmap.Dispose();
|
||||||
|
|
||||||
return new Bitmap(outputStream);
|
return new Bitmap(outputStream);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user