You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
With Avalonia 11.1.0-beta2 and 11.1.0-rc1 I get the following error when creating a new Bitmap:
Exception thrown: 'System.NullReferenceException' in Avalonia.Skia.dll: 'Object reference not set to an instance of an object.'
Stack trace:
> at Avalonia.Skia.ImmutableBitmap..ctor(PixelSize size, Vector dpi, Int32 stride, PixelFormat format, AlphaFormat alphaFormat, IntPtr data)
This worked without problems in 11.1.0-beta1 and earlier.
To Reproduce
I'm using the following callback function to create an image from a buffer:
public void AfterFrameDraw(nint buffer_data, int frame_num, int width, int height, int bufferWidth, int bufferHeight)
{
try
{
if (isDisposing || width == 0 || height == 0 || bufferWidth == 0 || bufferHeight == 0)
return;
if (width == bufferWidth && height == bufferHeight && (width != this.Bounds.Width || height != this.Bounds.Height) && this.Bounds.Width > 0 && this.Bounds.Height > 0)
{
width = (int) this.Bounds.Width;
height = (int) this.Bounds.Height;
}
// Mapviewer buffer is a 32-bit RGBA image
int bufferChannels = 4; // RGBA
// Get byte array from unmanaged data pointer
byte[] byteArray = new byte[bufferHeight * bufferWidth * bufferChannels];
Marshal.Copy(buffer_data, byteArray, 0, bufferHeight * bufferWidth * bufferChannels);
int bytesPerPixel = 4; // For a 32-bit RGBA image
byte[] data = new byte[height * width * bytesPerPixel];
// Calculate the effective width and height to be copied
int cropWidth = Math.Min(width, bufferWidth);
int cropHeight = Math.Min(height, bufferHeight);
Parallel.For(0, cropHeight, k =>
{
int srcOffset = ((cropHeight - k - 1) * bufferWidth) * bytesPerPixel;
int destOffset = k * cropWidth * bytesPerPixel;
Buffer.BlockCopy(
byteArray, srcOffset,
data, destOffset,
cropWidth * bytesPerPixel);
});
Avalonia.Media.Imaging.Bitmap? bmImage = null;
unsafe
{
fixed (byte* p = data)
{
IntPtr ptr = (IntPtr) p;
bmImage = new Avalonia.Media.Imaging.Bitmap(Avalonia.Platform.PixelFormat.Rgba8888, Avalonia.Platform.AlphaFormat.Premul,
ptr,
new Avalonia.PixelSize(cropWidth, cropHeight),
new Avalonia.Vector(96, 96),
bufferChannels);
}
}
MainThreadHelper.BeginInvokeOnMainThread(() =>
{
this.Source = bmImage;
});
}
catch (Exception ex)
{
// Handle the exception (logging, etc.)
}
}
The exception happens at
bmImage = new Avalonia.Media.Imaging.Bitmap(Avalonia.Platform.PixelFormat.Rgba8888, Avalonia.Platform.AlphaFormat.Premul,
ptr,
new Avalonia.PixelSize(cropWidth, cropHeight),
new Avalonia.Vector(96, 96),
bufferChannels);
Expected behavior
This should just work like it did in 11.1.0-beta1 and earlier.
Avalonia version
11.1.0-beta2, 11.1.0-rc1
OS
Linux
Additional context
No response
The text was updated successfully, but these errors were encountered:
The stride parameter you're providing here (the last one in Bitmap's constructor) is incorrect.
It should probably be cropWidth * bufferChannels.
Before #15520, this parameter was incorrectly ignored so it worked for the wrong reason :)
That being said, we shouldn't throw a NullReferenceException here. Instead, Avalonia should check whether the internal InstallPixels call failed, and throw an ArgumentException for invalid data instead.
Describe the bug
With Avalonia 11.1.0-beta2 and 11.1.0-rc1 I get the following error when creating a new Bitmap:
This worked without problems in 11.1.0-beta1 and earlier.
To Reproduce
I'm using the following callback function to create an image from a buffer:
The exception happens at
Expected behavior
This should just work like it did in 11.1.0-beta1 and earlier.
Avalonia version
11.1.0-beta2, 11.1.0-rc1
OS
Linux
Additional context
No response
The text was updated successfully, but these errors were encountered: