{"record":{"id":"b9f3bea3d54e4fbf","repo":"SubtitleEdit/subtitleedit","slug":"failed-to-set-clipboard-data","errorCode":null,"errorMessage":"Failed to set clipboard data","messagePattern":"Failed to set clipboard data","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/ui/Logic/ClipboardHelper.cs","lineNumber":275,"sourceCode":"                // Open clipboard and set data\n                if (!OpenClipboard(IntPtr.Zero))\n                {\n                    GlobalFree(hGlobal);\n                    throw new InvalidOperationException(\"Failed to open clipboard\");\n                }\n\n                try\n                {\n                    if (!EmptyClipboard())\n                    {\n                        GlobalFree(hGlobal);\n                        throw new InvalidOperationException(\"Failed to empty clipboard\");\n                    }\n\n                    if (SetClipboardData(CF_DIB, hGlobal) == IntPtr.Zero)\n                    {\n                        GlobalFree(hGlobal);\n                        throw new InvalidOperationException(\"Failed to set clipboard data\");\n                    }\n\n                    // Clipboard now owns the memory\n                    hGlobal = IntPtr.Zero;\n                }\n                finally\n                {\n                    CloseClipboard();\n                }\n            }\n            catch (Exception ex)\n            {\n                Debug.WriteLine($\"Failed to copy image to clipboard on Windows: {ex.Message}\");\n                throw;\n            }\n            finally\n            {\n                if (hGlobal != IntPtr.Zero)","sourceCodeStart":257,"sourceCodeEnd":293,"githubUrl":"https://github.com/SubtitleEdit/subtitleedit/blob/17a9f0748781032255db3526b7215d2fb891e3af/src/ui/Logic/ClipboardHelper.cs#L257-L293","documentation":"Thrown in CopyImageToClipboardWindows when SetClipboardData(CF_DIB, hGlobal) returns IntPtr.Zero after EmptyClipboard succeeded. The data was not accepted, most often because the clipboard was re-locked by another app between EmptyClipboard and SetClipboardData, or the CF_DIB payload (header/pixels) was malformed. On success ownership transfers to the clipboard; on failure the code frees hGlobal.","triggerScenarios":"Another process grabbed the clipboard between EmptyClipboard and SetClipboardData; the BITMAPINFOHEADER/pixel layout is invalid (wrong biSize, mismatched biSizeImage/stride, bad biCompression); the global block is too large or not properly locked-writable.","commonSituations":"Clipboard contention with RDP/clipboard managers; a malformed DIB header (e.g. wrong header size, non-BI_RGB compression); alpha-channel/stride mismatch; rare memory pressure.","solutions":["Validate the BITMAPINFOHEADER fields (biSize == Marshal.SizeOf<BITMAPINFOHEADER>(), biCompression == BI_RGB, biSizeImage == imageSize, biPlanes == 1, biBitCount == 32).","Confirm stride == width*4 and the bottom-up row order is correct before SetClipboardData.","Retry the open/empty/set sequence; check Marshal.GetLastWin32Error().","Disable clipboard managers/redirectors that may be stealing the clipboard between calls."],"exampleFix":"// before\nif (SetClipboardData(CF_DIB, hGlobal) == IntPtr.Zero) { GlobalFree(hGlobal); throw new InvalidOperationException(\"Failed to set clipboard data\"); }\n\n// after - assert header integrity and surface the Win32 error\nif (header.biSize != (uint)headerSize || header.biCompression != BI_RGB || header.biSizeImage != (uint)imageSize)\n    throw new InvalidOperationException(\"Malformed DIB header before SetClipboardData.\");\nif (SetClipboardData(CF_DIB, hGlobal) == IntPtr.Zero)\n{\n    var winErr = Marshal.GetLastWin32Error();\n    GlobalFree(hGlobal);\n    throw new InvalidOperationException($\"Failed to set clipboard data (Win32 error {winErr}).\");\n}","handlingStrategy":"validation","validationCode":"if (header.biSize != (uint)headerSize || header.biCompression != BI_RGB || header.biSizeImage != (uint)imageSize)\n    throw new InvalidOperationException(\"Malformed DIB header before SetClipboardData.\");\nif (stride != width * 4) throw new InvalidOperationException(\"Stride mismatch.\");","typeGuard":null,"tryCatchPattern":"if (SetClipboardData(CF_DIB, hGlobal) == IntPtr.Zero)\n{\n    var e = Marshal.GetLastWin32Error();\n    GlobalFree(hGlobal);\n    throw new InvalidOperationException($\"Failed to set clipboard data (Win32 {e}).\");\n}","preventionTips":["Validate BITMAPINFOHEADER fields and stride before SetClipboardData.","Retry under clipboard contention between Empty and Set.","Disable clipboard managers that steal the clipboard mid-operation."],"tags":["clipboard","windows","win32","dib","image"],"backgroundTag":null,"analyzedSha":"17a9f0748781032255db3526b7215d2fb891e3af","analyzedAt":"2026-08-13T18:11:43.374Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}