Update ImageReader to reaad images without resizinf

This commit is contained in:
TheXamlGuy
2024-10-10 15:47:01 +01:00
parent 1e402960f3
commit b1e239ab04
6 changed files with 38 additions and 24 deletions
+2 -2
View File
@@ -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);
}
+15 -3
View File
@@ -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);
}
}
+14 -14
View File
@@ -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
{
+2 -2
View File
@@ -4,7 +4,7 @@ public interface IImageDescriptor
{
object Image { get; }
int Width { get; }
double Width { get; }
int Height { get; }
double Height { get; }
}
+4 -2
View File
@@ -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);
}
+1 -1
View File
@@ -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;