{"record":{"id":"9485c82939259496","repo":"dotnet/wpf","slug":"throw-new-argumentnullexception-nameof-bitmap","errorCode":null,"errorMessage":"throw new ArgumentNullException(nameof(bitmap));","messagePattern":"throw new ArgumentNullException\\(nameof\\(bitmap\\)\\);","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/InterOp/Imaging.cs","lineNumber":54,"sourceCode":"\n        /// <summary>\n        /// Construct an Bitmap from a HBITMAP.\n        /// </summary>\n        /// <param name=\"bitmap\"></param>\n        /// <param name=\"palette\"></param>\n        /// <param name=\"sourceRect\"></param>\n        /// <param name=\"sizeOptions\"></param>\n        /// <param name=\"alphaOptions\"></param>\n        internal static unsafe BitmapSource CriticalCreateBitmapSourceFromHBitmap(\n            IntPtr bitmap,\n            IntPtr palette,\n            Int32Rect sourceRect,\n            BitmapSizeOptions sizeOptions,\n            WICBitmapAlphaChannelOption alphaOptions)\n        {\n            if (bitmap == IntPtr.Zero)\n            {\n                throw new ArgumentNullException(nameof(bitmap));\n            }\n\n            return new InteropBitmap(bitmap, palette, sourceRect, sizeOptions, alphaOptions); // use the critical version\n        }\n        \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)","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/InterOp/Imaging.cs#L36-L72","documentation":"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.","triggerScenarios":"Calling Imaging.CreateBitmapSourceFromHBitmap(IntPtr.Zero, ...) — often when CreateCompatibleBitmap/LoadImage/BmpFromScreen failed and the zero handle was passed through unchecked.","commonSituations":"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.","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"],"exampleFix":"// before\nvar bmp = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());\n// after\nif (hBitmap == IntPtr.Zero) throw new InvalidOperationException(\"bitmap creation failed\");\nvar bmp = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());","handlingStrategy":"validation","validationCode":"if (hBitmap == IntPtr.Zero) throw new InvalidOperationException(\"GDI bitmap creation failed\");","typeGuard":"bool IsValidHandle(IntPtr h) => h != IntPtr.Zero;","tryCatchPattern":"try { Imaging.CreateBitmapSourceFromHBitmap(hBitmap, ...); } catch (ArgumentNullException) { /* retry capture or use placeholder */ }","preventionTips":["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"],"tags":["wpf","imaging","null-argument","gdi"],"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"}