dotnet/wpf · error · ArgumentNullException
throw new ArgumentNullException(nameof(bitmap));
Error message
throw new ArgumentNullException(nameof(bitmap));
What it means
Imaging.CriticalCreateBitmapSourceFromHBitmap (reached via the public CreateBitmapSourceFromHBitmap) requires a non-zero HBITMAP. Passing IntPtr.Zero throws ArgumentNullException for the 'bitmap' parameter because a null bitmap cannot back an InteropBitmap.
Solutions
- Check hBitmap != IntPtr.Zero before calling CreateBitmapSourceFromHBitmap and handle the failure (e.g. retry capture)
- Verify the GDI call that produced the HBITMAP (GetLastError on failure)
- Remember to DeleteObject the HBITMAP after creating the source to avoid leaks
Example fix
// before
var bmp = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
// after
if (hBitmap == IntPtr.Zero) throw new InvalidOperationException("bitmap creation failed");
var bmp = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); Defensive patterns
Strategy: validation
Validate before calling
if (hBitmap == IntPtr.Zero) throw new InvalidOperationException("GDI bitmap creation failed"); Type guard
bool IsValidHandle(IntPtr h) => h != IntPtr.Zero;
Try / catch
try { Imaging.CreateBitmapSourceFromHBitmap(hBitmap, ...); } catch (ArgumentNullException) { /* retry capture or use placeholder */ } Prevention
- Always check GDI handles for IntPtr.Zero before use
- Free HBITMAPs with DeleteObject after creating the BitmapSource
- Wrap screen-capture in code that verifies GetDC/CreateCompatibleBitmap results
When it happens
Trigger: Calling Imaging.CreateBitmapSourceFromHBitmap(IntPtr.Zero, ...) — often when CreateCompatibleBitmap/LoadImage/BmpFromScreen failed and the zero handle was passed through unchecked.
Common situations: Screen-capture code where GetDC/CreateCompatibleBitmap failed under secure desktop or GDI resource exhaustion; unmarshal of a bitmap handle that came back NULL from native code.
Related errors
- throw new ArgumentNullException(nameof(icon));
- annotation component
- ArgumentNullException
- ArgumentNullException(argName)
- ArgumentNullException("characterBufferReference.CharacterBuf…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/9485c82939259496.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/InterOp/Imaging.cs:54
/// <summary>
/// Construct an Bitmap from a HBITMAP.
/// </summary>
/// <param name="bitmap"></param>
/// <param name="palette"></param>
/// <param name="sourceRect"></param>
/// <param name="sizeOptions"></param>
/// <param name="alphaOptions"></param>
internal static unsafe BitmapSource CriticalCreateBitmapSourceFromHBitmap(
IntPtr bitmap,
IntPtr palette,
Int32Rect sourceRect,
BitmapSizeOptions sizeOptions,
WICBitmapAlphaChannelOption alphaOptions)
{
if (bitmap == IntPtr.Zero)
{
throw new ArgumentNullException(nameof(bitmap));
}
return new InteropBitmap(bitmap, palette, sourceRect, sizeOptions, alphaOptions); // use the critical version
}
/// <summary>
/// Construct an Bitmap from a HICON.
/// </summary>
/// <param name="icon"></param>
/// <param name="sourceRect"></param>
/// <param name="sizeOptions"></param>
/// <remarks>
/// Callers must have UnmanagedCode permission to call this API.
/// </remarks>
public static unsafe BitmapSource CreateBitmapSourceFromHIcon(
IntPtr icon,
Int32Rect sourceRect,
BitmapSizeOptions sizeOptions)View on GitHub (pinned to 81131a70a4)