diff --git a/Toolkit.Avalonia/IImageResizer.cs b/Toolkit.Avalonia/IImageResizer.cs index ad1bc8b..5b8e601 100644 --- a/Toolkit.Avalonia/IImageResizer.cs +++ b/Toolkit.Avalonia/IImageResizer.cs @@ -5,7 +5,7 @@ namespace Toolkit.Avalonia; public interface IImageResizer { public Bitmap Resize(Stream stream, - int targetWidth, - int targetHeight, + double width, + double height, bool maintainAspectRatio); } \ No newline at end of file diff --git a/Toolkit.Avalonia/ImageReader.cs b/Toolkit.Avalonia/ImageReader.cs index d2b233a..a0c7a0f 100644 --- a/Toolkit.Avalonia/ImageReader.cs +++ b/Toolkit.Avalonia/ImageReader.cs @@ -7,11 +7,23 @@ public class ImageReader(IImageResizer imageResizer) : IImageReader { public IImageDescriptor Get(Stream stream, - int width, - int height, + double width, + double height, bool maintainAspectRatio) { - Bitmap resizedImage = imageResizer.Resize(stream, width, height, maintainAspectRatio); + Bitmap resizedImage = imageResizer.Resize(stream, + width, + height, + maintainAspectRatio); + return new ImageDescriptor(resizedImage, width, height); } + + public IImageDescriptor Get(Stream stream) + { + Bitmap image = new(stream); + return new ImageDescriptor(image, + image.Size.Width, + image.Size.Height); + } } diff --git a/Toolkit.Avalonia/ImageResizer.cs b/Toolkit.Avalonia/ImageResizer.cs index a73c297..8c79597 100644 --- a/Toolkit.Avalonia/ImageResizer.cs +++ b/Toolkit.Avalonia/ImageResizer.cs @@ -7,29 +7,29 @@ public class ImageResizer : IImageResizer { public Bitmap Resize(Stream stream, - int targetWidth, - int targetHeight, + double width, + double height, bool maintainAspectRatio) { Bitmap bitmap = new(stream); - if (bitmap.Size.Width != targetWidth || bitmap.Size.Height != targetHeight) + if (bitmap.Size.Width != width || bitmap.Size.Height != height) { stream.Seek(0, SeekOrigin.Begin); using SKBitmap sKBitmap = SKBitmap.Decode(stream); - if (targetHeight == 0 && maintainAspectRatio) + if (height == 0 && maintainAspectRatio) { - targetHeight = (int)((float)targetWidth / sKBitmap.Width * sKBitmap.Height); + height = (int)((float)width / sKBitmap.Width * sKBitmap.Height); } - if (targetWidth == 0 && maintainAspectRatio) + if (width == 0 && maintainAspectRatio) { - targetWidth = (int)((float)targetHeight / sKBitmap.Height * sKBitmap.Width); + width = (int)((float)height / sKBitmap.Height * sKBitmap.Width); } - float widthRatio = (float)targetWidth / sKBitmap.Width; - float heightRatio = (float)targetHeight / sKBitmap.Height; + float widthRatio = (float)width / sKBitmap.Width; + float heightRatio = (float)height / sKBitmap.Height; float scale = maintainAspectRatio ? Math.Max(widthRatio, heightRatio) : Math.Min(widthRatio, heightRatio); int newWidth = (int)(sKBitmap.Width * scale); @@ -50,14 +50,14 @@ public class ImageResizer : SKBitmap cropped; if (maintainAspectRatio) { - int cropX = (newWidth - targetWidth) / 2; - int cropY = (newHeight - targetHeight) / 2; + int cropX = (int)(newWidth - width) / 2; + int cropY = (int)(newHeight - height) / 2; - cropped = new SKBitmap(targetWidth, targetHeight); + cropped = new SKBitmap((int)width, (int)height); using SKCanvas croppedCanvas = new(cropped); - SKRect cropRect = new(cropX, cropY, cropX + targetWidth, cropY + targetHeight); + SKRect cropRect = new(cropX, cropY, (int)(cropX + width), cropY + (int)height); croppedCanvas.Clear(SKColors.Transparent); - croppedCanvas.DrawBitmap(resized, cropRect, new SKRect(0, 0, targetWidth, targetHeight)); + croppedCanvas.DrawBitmap(resized, cropRect, new SKRect(0, 0, (int)width, (int)height)); } else { diff --git a/Toolkit.Foundation/IImageDescriptor.cs b/Toolkit.Foundation/IImageDescriptor.cs index 169fd84..1dc5ae1 100644 --- a/Toolkit.Foundation/IImageDescriptor.cs +++ b/Toolkit.Foundation/IImageDescriptor.cs @@ -4,7 +4,7 @@ public interface IImageDescriptor { object Image { get; } - int Width { get; } + double Width { get; } - int Height { get; } + double Height { get; } } diff --git a/Toolkit.Foundation/IImageReader.cs b/Toolkit.Foundation/IImageReader.cs index 465b169..27bde33 100644 --- a/Toolkit.Foundation/IImageReader.cs +++ b/Toolkit.Foundation/IImageReader.cs @@ -3,7 +3,9 @@ public interface IImageReader { IImageDescriptor Get(Stream stream, - int width, - int height, + double width, + double height, bool maintainAspectRatio = false); + + IImageDescriptor Get(Stream stream); } \ No newline at end of file diff --git a/Toolkit.Foundation/ImageDescriptor.cs b/Toolkit.Foundation/ImageDescriptor.cs index ff8a7ed..f60f915 100644 --- a/Toolkit.Foundation/ImageDescriptor.cs +++ b/Toolkit.Foundation/ImageDescriptor.cs @@ -1,4 +1,4 @@ namespace Toolkit.Foundation; -public record ImageDescriptor(object Image, int Width, int Height) : +public record ImageDescriptor(object Image, double Width, double Height) : IImageDescriptor; \ No newline at end of file