{"record":{"id":"28cac2f573ea548c","repo":"dotnet/wpf","slug":"throw-new-argumentnullexception-nameof-icon","errorCode":null,"errorMessage":"throw new ArgumentNullException(nameof(icon));","messagePattern":"throw new ArgumentNullException\\(nameof\\(icon\\)\\);","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/InterOp/Imaging.cs","lineNumber":77,"sourceCode":"        \n        /// <summary>\n        /// Construct an Bitmap from a HICON.\n        /// </summary>\n        /// <param name=\"icon\"></param>\n        /// <param name=\"sourceRect\"></param>\n        /// <param name=\"sizeOptions\"></param>\n        /// <remarks>\n        ///     Callers must have UnmanagedCode permission to call this API.\n        /// </remarks>\n        public static unsafe BitmapSource CreateBitmapSourceFromHIcon(\n            IntPtr icon,\n            Int32Rect sourceRect,\n            BitmapSizeOptions sizeOptions)\n        {\n\n            if (icon == IntPtr.Zero)\n            {\n                throw new ArgumentNullException(nameof(icon));\n            }\n\n            return new InteropBitmap(icon, sourceRect, sizeOptions);\n        }\n\n        /// <summary>\n        /// Construct an Bitmap from a section handle.\n        /// </summary>\n        /// <param name=\"section\"></param>\n        /// <param name=\"pixelWidth\"></param>\n        /// <param name=\"pixelHeight\"></param>\n        /// <param name=\"format\"></param>\n        /// <param name=\"stride\"></param>\n        /// <param name=\"offset\"></param>\n        /// <remarks>\n        ///     Callers must have UnmanagedCode permission to call this API.\n        /// </remarks>\n        public static unsafe BitmapSource CreateBitmapSourceFromMemorySection(","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/InterOp/Imaging.cs#L59-L95","documentation":"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.","triggerScenarios":"Calling Imaging.CreateBitmapSourceFromHIcon(IntPtr.Zero, ...) — typically when ExtractIcon/LoadIcon/SHGetFileInfo returned NULL (missing resource, wrong path) and the handle was used unchecked.","commonSituations":"Extracting file icons where the file lacks an icon or the path is wrong; LoadIcon with an icon name not found in resources.","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"],"exampleFix":"// before\nvar src = Imaging.CreateBitmapSourceFromHIcon(hIcon, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());\n// after\nif (hIcon == IntPtr.Zero) hIcon = LoadIcon(defaultIcon);\nvar src = Imaging.CreateBitmapSourceFromHIcon(hIcon, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());","handlingStrategy":"validation","validationCode":"if (hIcon == IntPtr.Zero) throw new InvalidOperationException(\"icon extraction failed\");","typeGuard":"bool IsValidHandle(IntPtr h) => h != IntPtr.Zero;","tryCatchPattern":"try { Imaging.CreateBitmapSourceFromHIcon(hIcon, ...); } catch (ArgumentNullException) { /* fall back to default icon */ }","preventionTips":["Check ExtractIcon/LoadIcon results before use","Call DestroyIcon on the HICON after creating the BitmapSource","Provide a default icon fallback for files without icons"],"tags":["wpf","imaging","null-argument","icon"],"backgroundTag":"null-argument","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}