HandyOrg/HandyControl · error · ArgumentException

NativeHandle0

Error message

NativeHandle0

What it means

GifImage.SetNativeImage stores the native GDI+ image handle and throws ArgumentException('NativeHandle0') when the handle is IntPtr.Zero, because a zero handle means GDI+ failed to produce a valid image. Called from CreateSourceFromFile and CreateSourceFromStream.

Solutions

  1. Validate the image file/stream (e.g. try loading with a standard Image decoder) before constructing GifImage
  2. Catch ArgumentException and fall back to a static placeholder image
  3. Re-export or re-download the GIF if the file is corrupt

Example fix

// before
var gif = new GifImage { Source = File.OpenRead(path) }; // corrupt file -> Zero handle -> throws
// after
try { var gif = new GifImage { Source = File.OpenRead(path) }; }
catch (ArgumentException) { ShowPlaceholderImage(); }
Defensive patterns

Strategy: try-catch

Validate before calling

try { using var probe = System.Drawing.Image.FromStream(stream); } catch { return false; }

Type guard

static bool IsValidGif(Stream s) => s != null && s.Length > 6;

Try / catch

try { return GifImage.CreateSourceFromStream(stream); }
catch (ArgumentException) { ShowPlaceholder(); return null; }

Prevention

When it happens

Trigger: Creating a GifImage from a file/stream where the preceding GdipCreateBitmapFromStream/GdipCreateBitmapFromFile returned a zero handle — typically a corrupt, missing, or non-image file.

Common situations: Corrupt or truncated GIF files; files that exist but are not valid images; GDI+ native failures swallowed upstream before the handle check.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

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

        {
        }
        // possible exceptions for setting/getting the position inside dataStream
        catch (NotSupportedException)
        {
        }
        catch (ObjectDisposedException)
        {
        }
        // possible exception when reading stuff into dataStream
        catch (ArgumentException)
        {
        }
    }

    internal void SetNativeImage(IntPtr handle)
    {
        if (handle == IntPtr.Zero)
            throw new ArgumentException("NativeHandle0");

        NativeImage = handle;
    }

    internal Guid RawGuid
    {
        get
        {
            var guid = new Guid();

            var status = InteropMethods.Gdip.GdipGetImageRawFormat(new HandleRef(this, NativeImage), ref guid);

            if (status != InteropMethods.Gdip.Ok)
                throw InteropMethods.Gdip.StatusException(status);

            return guid;
        }
    }

View on GitHub (pinned to 2c0875ebd6)