AvaloniaUI/Avalonia · error · ArgumentException
Size should be >= (1,1)
Error message
Size should be >= (1,1)
What it means
WriteableBitmap.CreatePlatformImpl throws ArgumentException(nameof(size)) when size.Width <= 0 || size.Height <= 0. A bitmap must have at least one pixel in each dimension. This guard runs in every constructor path and DecodeToHeight, so a decoded stream that yields zero dimensions also surfaces here.
Source
Thrown at src/Avalonia.Base/Media/Imaging/WriteableBitmap.cs:127
/// <summary>
/// Loads a Bitmap from a stream and decodes at the desired height. Aspect ratio is maintained.
/// This is more efficient than loading and then resizing.
/// </summary>
/// <param name="stream">The stream to read the bitmap from. This can be any supported image format.</param>
/// <param name="height">The desired height of the resulting bitmap.</param>
/// <param name="interpolationMode">The <see cref="BitmapInterpolationMode"/> to use should any scaling be required.</param>
/// <returns>An instance of the <see cref="WriteableBitmap"/> class.</returns>
public new static WriteableBitmap DecodeToHeight(Stream stream, int height, BitmapInterpolationMode interpolationMode = BitmapInterpolationMode.HighQuality)
{
var ri = GetFactory();
return new WriteableBitmap(ri.LoadWriteableBitmapToHeight(stream, height, interpolationMode));
}
private static (IBitmapImpl, BitmapMemory?) CreatePlatformImpl(PixelSize size, in Vector dpi, PixelFormat? format, AlphaFormat? alphaFormat)
{
if (size.Width <= 0 || size.Height <= 0)
throw new ArgumentException("Size should be >= (1,1)", nameof(size));
var ri = GetFactory();
PixelFormat finalFormat = format ?? ri.DefaultPixelFormat;
AlphaFormat finalAlphaFormat = alphaFormat ?? ri.DefaultAlphaFormat;
if (ri.IsSupportedBitmapPixelFormat(finalFormat))
return (ri.CreateWriteableBitmap(size, dpi, finalFormat, finalAlphaFormat), null);
if (!PixelFormatReader.SupportsFormat(finalFormat))
throw new NotSupportedException($"Pixel format {finalFormat} is not supported");
finalAlphaFormat = finalFormat.HasAlpha ? finalAlphaFormat : Platform.AlphaFormat.Opaque;
var impl = ri.CreateWriteableBitmap(size, dpi, PixelFormat.Rgba8888, finalAlphaFormat);
return (impl, new BitmapMemory(finalFormat, finalAlphaFormat, size));
}
View on GitHub (pinned to 11c5427268)
Solutions
- Ensure width and height are >= 1 before constructing.
- Validate the stream decodes to a non-zero size (decode to a Bitmap first to inspect PixelSize).
- Guard callers that derive size from external input against zero/negative values.
Example fix
// before
var wb = new WriteableBitmap(new PixelSize(0, 0), dpi, PixelFormat.Bgra8888);
// after
if (size.Width < 1 || size.Height < 1)
throw new ArgumentException("Bitmap size must be at least 1x1.", nameof(size));
var wb = new WriteableBitmap(new PixelSize(Math.Max(1,size.Width), Math.Max(1,size.Height)), dpi, PixelFormat.Bgra8888); Defensive patterns
Strategy: validation
Validate before calling
if (size.Width < 1 || size.Height < 1)
throw new ArgumentException("Size must be >= (1,1).", nameof(size));
var wb = new WriteableBitmap(size, dpi, PixelFormat.Bgra8888); Prevention
- Validate dimensions derived from external input before constructing.
- When decoding, verify the stream yields a non-zero PixelSize first.
- Avoid passing UI Size (double) where PixelSize (int) is required.
When it happens
Trigger: Constructing a WriteableBitmap with PixelSize(0,0) or negative dimensions; calling DecodeToHeight on a corrupt/empty stream that decodes to a zero-size image.
Common situations: Passing a UI Size or a default PixelSize; decoding a truncated or unsupported image file; computing dimensions from DPI/scale and rounding to zero.
Related errors
- stride
- {nameof(KeySpline)} must have X coordinates >= 0.0 and <= 1.
- Invalid KeySpline X1 value. Must be >= 0.0 and <= 1.0.
- Invalid KeySpline X2 value. Must be >= 0.0 and <= 1.0.
- Invalid application identifier
AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13).
Data as JSON: /api/errors/611bb641f0444c04.
Report an issue: GitHub.