dotnet/wpf · error · ArgumentNullException
throw new ArgumentNullException(nameof(icon));
Error message
throw new ArgumentNullException(nameof(icon));
What it means
Imaging.CreateBitmapSourceFromHBitmap's icon counterpart, CreateBitmapSourceFromHIcon, requires a non-zero HICON. Passing IntPtr.Zero throws ArgumentNullException for the 'icon' parameter since InteropBitmap needs a valid icon handle.
Solutions
- Check hIcon != IntPtr.Zero before calling CreateBitmapSourceFromHIcon and fall back to a default icon
- Verify the icon extraction API succeeded (file exists, icon index valid)
- DestroyIcon the handle after creating the BitmapSource to avoid handle leaks
Example fix
// before var src = Imaging.CreateBitmapSourceFromHIcon(hIcon, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); // after if (hIcon == IntPtr.Zero) hIcon = LoadIcon(defaultIcon); var src = Imaging.CreateBitmapSourceFromHIcon(hIcon, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
Defensive patterns
Strategy: validation
Validate before calling
if (hIcon == IntPtr.Zero) throw new InvalidOperationException("icon extraction failed"); Type guard
bool IsValidHandle(IntPtr h) => h != IntPtr.Zero;
Try / catch
try { Imaging.CreateBitmapSourceFromHIcon(hIcon, ...); } catch (ArgumentNullException) { /* fall back to default icon */ } Prevention
- Check ExtractIcon/LoadIcon results before use
- Call DestroyIcon on the HICON after creating the BitmapSource
- Provide a default icon fallback for files without icons
When it happens
Trigger: Calling Imaging.CreateBitmapSourceFromHIcon(IntPtr.Zero, ...) — typically when ExtractIcon/LoadIcon/SHGetFileInfo returned NULL (missing resource, wrong path) and the handle was used unchecked.
Common situations: Extracting file icons where the file lacks an icon or the path is wrong; LoadIcon with an icon name not found in resources.
Related errors
- throw new ArgumentNullException(nameof(bitmap));
- annotation component
- ArgumentNullException
- ArgumentNullException(argName)
- ArgumentNullException("characterBufferReference.CharacterBuf…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/28cac2f573ea548c.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/InterOp/Imaging.cs:77
/// <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)
{
if (icon == IntPtr.Zero)
{
throw new ArgumentNullException(nameof(icon));
}
return new InteropBitmap(icon, sourceRect, sizeOptions);
}
/// <summary>
/// Construct an Bitmap from a section handle.
/// </summary>
/// <param name="section"></param>
/// <param name="pixelWidth"></param>
/// <param name="pixelHeight"></param>
/// <param name="format"></param>
/// <param name="stride"></param>
/// <param name="offset"></param>
/// <remarks>
/// Callers must have UnmanagedCode permission to call this API.
/// </remarks>
public static unsafe BitmapSource CreateBitmapSourceFromMemorySection(View on GitHub (pinned to 81131a70a4)