{"record":{"id":"31dcd8917f1f8139","repo":"HandyOrg/HandyControl","slug":"statusexception-status-screenshotwindow","errorCode":null,"errorMessage":"StatusException(status)","messagePattern":"StatusException\\(status\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Shared/HandyControl_Shared/Controls/Screenshot/ScreenshotWindow.cs","lineNumber":569,"sourceCode":"    private BitmapSource GetDesktopSnapshot()\n    {\n        _desktopWindowHandle = InteropMethods.GetDesktopWindow();\n        var hdcSrc = InteropMethods.GetWindowDC(_desktopWindowHandle);\n        var hdcDest = InteropMethods.CreateCompatibleDC(hdcSrc);\n\n        InteropMethods.GetWindowRect(_desktopWindowHandle, out _desktopWindowRect);\n        var desktopWindowWidth = _desktopWindowRect.Right - _desktopWindowRect.Left;\n        var desktopWindowHeight = _desktopWindowRect.Bottom - _desktopWindowRect.Top;\n\n        var hbitmap = InteropMethods.CreateCompatibleBitmap(hdcSrc, desktopWindowWidth, desktopWindowHeight);\n        var hOld = InteropMethods.SelectObject(hdcDest, hbitmap);\n        InteropMethods.BitBlt(hdcDest, 0, 0, desktopWindowWidth, desktopWindowHeight, hdcSrc, 0, 0, InteropValues.SRCCOPY);\n        InteropMethods.SelectObject(hdcDest, hOld);\n        InteropMethods.DeleteDC(hdcDest);\n        InteropMethods.ReleaseDC(_desktopWindowHandle, hdcSrc);\n\n        var status = InteropMethods.Gdip.GdipCreateBitmapFromHBITMAP(new HandleRef(null, hbitmap), new HandleRef(null, IntPtr.Zero), out var bitmap);\n        if (status != InteropMethods.Gdip.Ok) throw InteropMethods.Gdip.StatusException(status);\n\n        using var ms = new MemoryStream();\n        status = InteropMethods.Gdip.GdipGetImageEncodersSize(out var numEncoders, out var size);\n        if (status != InteropMethods.Gdip.Ok) throw InteropMethods.Gdip.StatusException(status);\n\n        var memory = Marshal.AllocHGlobal(size);\n        try\n        {\n            status = InteropMethods.Gdip.GdipGetImageEncoders(numEncoders, size, memory);\n            if (status != InteropMethods.Gdip.Ok) throw InteropMethods.Gdip.StatusException(status);\n\n            var codecInfo = ImageCodecInfo.ConvertFromMemory(memory, numEncoders).FirstOrDefault(item => item.FormatID.Equals(BmpGuid));\n            if (codecInfo == null) throw new Exception(\"ImageCodecInfo is null\");\n\n            var encoderParamsMemory = IntPtr.Zero;\n\n            try\n            {","sourceCodeStart":551,"sourceCodeEnd":587,"githubUrl":"https://github.com/HandyOrg/HandyControl/blob/2c0875ebd67326e0c67282967e3e809c69282fee/src/Shared/HandyControl_Shared/Controls/Screenshot/ScreenshotWindow.cs#L551-L587","documentation":"GDI+ returned a non-Ok status from GdipCreateBitmapFromHBITMAP while wrapping the screen-capture HBITMAP into a native Bitmap inside ScreenshotWindow. The library immediately surfaces it as a StatusException. In a screenshot flow this usually means the HBITMAP or its palette handle was invalid at conversion time.","triggerScenarios":"Taking a screenshot where the freshly created HBITMAP could not be converted — e.g. the CompatibleBitmap creation failed earlier (bad HDC, zero desktop width/height, secure desktop/UAC elevation differences) — or a null hbitmap reached GdipCreateBitmapFromHBITMAP.","commonSituations":"Capturing on secure desktops / elevated UAC prompts where BitBlt yields an empty bitmap; RDP sessions with disconnected monitors (zero-size desktop); GDI handle exhaustion after many captures.","solutions":["Verify desktopWindowWidth/Height are > 0 and all GDI handles (CreateCompatibleDC/Bitmap, SelectObject) succeeded before this call","Check GetDC/GetDesktopWindow results and retry capture if BitBlt failed","Release/Dispose each capture's HBITMAP and DCs to prevent GDI handle leaks over repeated screenshots","Catch the StatusException around the capture and show a user-facing 'capture failed' fallback"],"exampleFix":"// before\nvar status = InteropMethods.Gdip.GdipCreateBitmapFromHBITMAP(new HandleRef(null, hbitmap), new HandleRef(null, IntPtr.Zero), out var bitmap);\nif (status != InteropMethods.Gdip.Ok) throw InteropMethods.Gdip.StatusException(status);\n// after\nif (desktopWindowWidth <= 0 || desktopWindowHeight <= 0)\n    throw new InvalidOperationException(\"Desktop has no drawable area; screenshot aborted.\");\nvar status = InteropMethods.Gdip.GdipCreateBitmapFromHBITMAP(new HandleRef(null, hbitmap), new HandleRef(null, IntPtr.Zero), out var bitmap);\nif (status != InteropMethods.Gdip.Ok || bitmap == IntPtr.Zero)\n    throw InteropMethods.Gdip.StatusException(status);","handlingStrategy":"try-catch","validationCode":"if (desktopWindowWidth <= 0 || desktopWindowHeight <= 0 || hbitmap == IntPtr.Zero)\n    throw new InvalidOperationException(\"Invalid capture state; aborting bitmap conversion.\");","typeGuard":"static bool IsCaptureValid(int w, int h, IntPtr hbmp) => w > 0 && h > 0 && hbmp != IntPtr.Zero;","tryCatchPattern":"try { status = GdipCreateBitmapFromHBITMAP(...); if (status != Ok) throw StatusException(status); }\ncatch (ExternalException) { /* retry capture or notify user */ }","preventionTips":["Check every GDI handle creation result before BitBlt","Handle secure-desktop / elevated-UAC captures gracefully","Delete HBITMAPs and release DCs after each capture to avoid handle exhaustion"],"tags":["gdiplus","interop","screenshot","gdi"],"backgroundTag":"gdiplus-error","analyzedSha":"2c0875ebd67326e0c67282967e3e809c69282fee","analyzedAt":"2026-09-14T14:45:29.754Z","contentChangedAt":"2026-09-14T14:45:29.754Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}