Microsoft.Maui.Graphics
Microsoft.Maui.Graphics copied to clipboard
IImage.Resize (SkiaImage) produces unusable object
I'm trying to convert some image manipulation code from System.Drawing
to Microsoft.Maui.Graphics
. The code is being used in an ASP.NET 6 application running in Visual Studio 2022 (17.2.2) on Windows 11. So far, not going too well. I've hit multiple roadblocks that I'll try to write up but here is the first one in the chain. Currently using version 6.0.300 of both the Microsoft.Maui.Graphics and Microsoft.Maui.Graphics.Skia packages.
The IImage.Resize
method is producing an unusable object. Here is the code that is being used to extract the image (from a byte array) and then resize it.
public static IImage ExtractImage(byte[] ib)
{
using (MemoryStream imgStream = new(ib))
{
//var image = PlatformImage.FromStream(imgStream);
var image = SkiaImage.FromStream(imgStream);
return image;
}
}
public static IImage ResizeImage(IImage imgToResize, int outWidth, int outHeight)
{
var sourceWidth = imgToResize.Width;
var sourceHeight = imgToResize.Height;
var (destWidth, destHeight) = ResizeImageDimensions(sourceWidth, sourceHeight, outWidth, outHeight);
var newImage = imgToResize.Resize(destWidth, destHeight, ResizeMode.Stretch, true);
return newImage;
}
First off, I tried to use PlatformImage
to load the image into an IImage
object. The resulting object had the raw data but the dimensions were garbage. After digging around a bit it looked like SkiaImage
was was best alternative. SkiaImage
did load the bytes into an object that recognized the dimensions when inspecting in break mode.
The problem I care about here is when I try to resize the image. Feeding the result from the ExtractImage
method into ResizeImage
. If I put a breakpoint on the return
statement and inspect the imgToResize
parameter it looks fine. Still shows the expected dimensions. As soon as I try to inspect the newImage
variable, an exception is thrown when the debugger tries to get the value for the Height
property.
The ResizeImageDimensions
method calculates the new width and height to fit inside a bounding box without distorting the image. In this case, the image size is being reduced.