HandyOrg/HandyControl · error · ArgumentException

GdiplusInvalidSize

Error message

GdiplusInvalidSize

What it means

GifImage.GetHbitmap converts the native GDI+ bitmap to an HBITMAP. When GDI+ returns status 2 (InvalidParameter/InvalidSize) and the image dimensions are >= short.MaxValue, the method throws ArgumentException('GdiplusInvalidSize') because GDI+ HBITMAPs cannot represent such large bitmaps.

Solutions

  1. Downscale the image to below 32767x32767 before calling GetHbitmap
  2. Catch ArgumentException and handle oversized images with a different rendering path
  3. Pre-check Width/Height of the GifImage and refuse interop conversion for oversized ones

Example fix

// before
var hBitmap = gif.GetHbitmap(Colors.Black); // throws for huge images
// after
if (gif.Width < short.MaxValue && gif.Height < short.MaxValue)
{
    var hBitmap = gif.GetHbitmap(Colors.Black);
}
Defensive patterns

Strategy: validation

Validate before calling

if (gif.Width < short.MaxValue && gif.Height < short.MaxValue) { var hb = gif.GetHbitmap(bg); }

Type guard

static bool CanGetHbitmap(GifImage g) => g.Width < short.MaxValue && g.Height < short.MaxValue;

Try / catch

try { return gif.GetHbitmap(background); }
catch (ArgumentException) { return IntPtr.Zero; }

Prevention

When it happens

Trigger: Calling GetHbitmap on a GifImage whose Width or Height is >= 32767 pixels.

Common situations: Rendering extremely large/tall GIFs (panoramas, long scrolling screenshots exported as GIF); converting huge bitmaps for Win32 interop / clipboard / drag-drop.

Related errors


AI-assisted analysis of HandyOrg/HandyControl@2c0875ebd6 (2026-09-14). Data as JSON: /api/errors/94d7ec4e498d524a. Report an issue: GitHub.

Appendix: source

Thrown at src/Shared/HandyControl_Shared/Controls/Image/GifImage.cs:473

    }

    [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
    [EditorBrowsable(EditorBrowsableState.Advanced)]
    [ResourceExposure(ResourceScope.Machine)]
    [ResourceConsumption(ResourceScope.Machine)]
    internal IntPtr GetHbitmap() => GetHbitmap(Colors.LightGray);

    [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
    [EditorBrowsable(EditorBrowsableState.Advanced)]
    [ResourceExposure(ResourceScope.Machine)]
    [ResourceConsumption(ResourceScope.Machine)]
    internal IntPtr GetHbitmap(Color background)
    {
        var status = InteropMethods.Gdip.GdipCreateHBITMAPFromBitmap(new HandleRef(this, NativeImage), out var hBitmap,
            ColorHelper.ToWin32(background));
        if (status == 2 && (Width >= short.MaxValue || Height >= short.MaxValue))
        {
            throw new ArgumentException("GdiplusInvalidSize");
        }

        if (status != InteropMethods.Gdip.Ok && NativeImage != IntPtr.Zero)
        {
            throw InteropMethods.Gdip.StatusException(status);
        }

        return hBitmap;
    }

    private void GetBitmapSource()
    {
        var handle = IntPtr.Zero;

        try
        {
            handle = GetHbitmap();
            Source = Imaging.CreateBitmapSourceFromHBitmap(handle, IntPtr.Zero, Int32Rect.Empty,

View on GitHub (pinned to 2c0875ebd6)