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

  1. Check hIcon != IntPtr.Zero before calling CreateBitmapSourceFromHIcon and fall back to a default icon
  2. Verify the icon extraction API succeeded (file exists, icon index valid)
  3. 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

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


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)