{"record":{"id":"4498c2559eafb11e","repo":"SubtitleEdit/subtitleedit","slug":"failed-to-decode-bitmap","errorCode":null,"errorMessage":"Failed to decode bitmap","messagePattern":"Failed to decode bitmap","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/ui/Logic/ClipboardHelper.cs","lineNumber":192,"sourceCode":"        await Task.Run(() =>\n        {\n            IntPtr hGlobal = IntPtr.Zero;\n\n            try\n            {\n                var width = bitmap.PixelSize.Width;\n                var height = bitmap.PixelSize.Height;\n\n                // Save bitmap to memory stream and read pixel data\n                using var memoryStream = new MemoryStream();\n                bitmap.Save(memoryStream, PngBitmapEncoderOptions.Default);\n                memoryStream.Position = 0;\n\n                // Use SkiaSharp to decode the image and get pixel data\n                using var skBitmap = SkiaSharp.SKBitmap.Decode(memoryStream);\n                if (skBitmap == null)\n                {\n                    throw new InvalidOperationException(\"Failed to decode bitmap\");\n                }\n\n                // Ensure we have BGRA format\n                using var bgraBitmap = new SkiaSharp.SKBitmap(width, height, SkiaSharp.SKColorType.Bgra8888, SkiaSharp.SKAlphaType.Premul);\n                using var canvas = new SkiaSharp.SKCanvas(bgraBitmap);\n                canvas.DrawBitmap(skBitmap, 0, 0);\n                canvas.Flush();\n\n                var pixels = bgraBitmap.Bytes;\n\n                // Calculate sizes\n                var stride = width * 4; // 4 bytes per pixel (BGRA)\n                var imageSize = stride * height;\n                var headerSize = Marshal.SizeOf<BITMAPINFOHEADER>();\n                var totalSize = headerSize + imageSize;\n\n                // Allocate global memory\n                hGlobal = GlobalAlloc(GMEM_MOVEABLE, new UIntPtr((uint)totalSize));","sourceCodeStart":174,"sourceCodeEnd":210,"githubUrl":"https://github.com/SubtitleEdit/subtitleedit/blob/17a9f0748781032255db3526b7215d2fb891e3af/src/ui/Logic/ClipboardHelper.cs#L174-L210","documentation":"Thrown in CopyImageToClipboardWindows after re-encoding the bitmap to PNG and calling SKBitmap.Decode: SkiaSharp returned null, meaning the decoded image is null (the PNG bytes produced by bitmap.Save could not be decoded back). This is a Skia decode failure on the Windows clipboard path just before converting to a BGRA DIB.","triggerScenarios":"bitmap.Save produced an empty/corrupt PNG stream (disposed bitmap, zero-size bitmap, or encoder failure), or the SkiaSharp build cannot decode that PNG; SKBitmap.Decode returns null on malformed input.","commonSituations":"Source bitmap was already disposed or has zero pixel size; a corrupt/empty in-memory image; a SkiaSharp version mismatch failing to round-trip its own PNG output; out-of-memory during decode.","solutions":["Check bitmap is non-null, has non-zero PixelSize, and is not disposed before copying.","Verify the SkiaSharp version is intact/consistent (re-add the NuGet package).","Log the PNG stream length to distinguish an empty stream from a decode failure."],"exampleFix":"// before\nusing var skBitmap = SKBitmap.Decode(memoryStream);\nif (skBitmap == null) throw new InvalidOperationException(\"Failed to decode bitmap\");\n\n// after - validate the source and the encoded stream\nif (bitmap.PixelSize.Width <= 0 || bitmap.PixelSize.Height <= 0)\n    throw new InvalidOperationException(\"Source bitmap has no pixels.\");\nif (memoryStream.Length == 0)\n    throw new InvalidOperationException(\"PNG re-encode produced no bytes.\");\nusing var skBitmap = SKBitmap.Decode(memoryStream) ?? throw new InvalidOperationException(\"Failed to decode bitmap\");","handlingStrategy":"validation","validationCode":"if (bitmap is null || bitmap.PixelSize.Width <= 0 || bitmap.PixelSize.Height <= 0)\n    throw new InvalidOperationException(\"Source bitmap is empty or disposed.\");","typeGuard":null,"tryCatchPattern":"try { await CopyImageToClipboardWindows(bitmap); }\ncatch (InvalidOperationException ex) when (ex.Message == \"Failed to decode bitmap\")\n{ SeLogger.Error(ex, \"SkiaSharp failed to round-trip the PNG\"); throw; }","preventionTips":["Validate the bitmap has non-zero size and is not disposed before copying.","Keep SkiaSharp package versions consistent.","Log the encoded stream length to distinguish empty vs corrupt."],"tags":["clipboard","skiasharp","image","windows","decode"],"backgroundTag":null,"analyzedSha":"17a9f0748781032255db3526b7215d2fb891e3af","analyzedAt":"2026-08-13T18:11:43.374Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}