{"record":{"id":"ef292022ace53ae2","repo":"wmjordan/PDFPatcher","slug":"error-ef2920","errorCode":null,"errorMessage":"合并图片失败：无法转换图片颜色","messagePattern":"合并图片失败：无法转换图片颜色","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"App/Processor/ImageExtractor.cs","lineNumber":716,"sourceCode":"\r\n\t\t\tstatic ColorType GetWhiteBlackType(RGBQUAD[] colors) {\r\n\t\t\t\tvar c0 = colors[0].Color;\r\n\t\t\t\tvar c1 = colors[1].Color;\r\n\t\t\t\treturn c0 == Color.Black && c1 == Color.White ? ColorType.BlackAndWhite\r\n\t\t\t\t\t: c0 == Color.White && c1 == Color.Black ? ColorType.WhiteAndBlack\r\n\t\t\t\t\t: ColorType.IndexedColor;\r\n\t\t\t}\r\n\r\n\t\t\tvoid PasteIntoFullColor(FreeImageBitmap bmp) {\r\n\t\t\t\tif (bmp.HasPalette) {\r\n\t\t\t\t\tTryExpandColorDepth(bmp);\r\n\t\t\t\t}\r\n\t\t\t\tPasteAndIncrementHeight(bmp);\r\n\t\t\t}\r\n\r\n\t\t\tstatic void TryExpandColorDepth(FreeImageBitmap bmp) {\r\n\t\t\t\tif (!bmp.ConvertColorDepth(FREE_IMAGE_COLOR_DEPTH.FICD_24_BPP)) {\r\n\t\t\t\t\tthrow new InvalidOperationException(\"合并图片失败：无法转换图片颜色\");\r\n\t\t\t\t}\r\n\t\t\t}\r\n\r\n\t\t\tvoid PasteAndIncrementHeight(FreeImageBitmap bmp) {\r\n\t\t\t\tif (!_Image.Paste(bmp, 0, _Height, Int32.MaxValue)) {\r\n\t\t\t\t\tthrow new InvalidOperationException(\"合并图片失败：粘贴操作出错\");\r\n\t\t\t\t}\r\n\t\t\t\t_Height += bmp.Height;\r\n\t\t\t}\r\n\r\n\t\t\tvoid ExpandColorSpaceAndPaste(FreeImageBitmap bmp) {\r\n\t\t\t\tTryExpandColorDepth(_Image);\r\n\t\t\t\tPasteIntoFullColor(bmp);\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tenum ColorType\r\n\t\t{\r","sourceCodeStart":698,"sourceCodeEnd":734,"githubUrl":"https://github.com/wmjordan/PDFPatcher/blob/4782bbd9ada850b56258f29999b7a1c9076c9b1b/App/Processor/ImageExtractor.cs#L698-L734","documentation":"ImageExtractor's TryExpandColorDepth throws InvalidOperationException when FreeImageBitmap.ConvertColorDepth fails to promote an image to 24-bit BPP. This happens during image merging when a paletted/indexed source bitmap must be converted to full color before pasting into the composite image. The boolean return of ConvertColorDepth is treated as success/failure.","triggerScenarios":"Calling the image-merge paste path (PasteIntoFullColor / ExpandColorSpaceAndPaste) with a FreeImageBitmap whose palette/color depth FreeImage cannot convert to 24-bit. ConvertColorDepth returns false for unsupported or corrupted source formats.","commonSituations":"An extracted PDF image in an unusual or corrupted indexed format that FreeImage's converter rejects; a 1-bit or 4-bit image with a damaged palette; an out-of-memory or GDI failure during conversion that surfaces as a false return; version mismatch in the FreeImage native library.","solutions":["Pre-check the source bitmap's color depth and handle unsupported formats before attempting the merge.","Fall back to per-image export instead of merging when conversion fails (catch and export individually).","Update/redeploy the FreeImage native dependency to ensure conversion support.","Validate the extracted image is loadable in a standalone FreeImage tool before batch processing."],"exampleFix":"// before\nstatic void TryExpandColorDepth(FreeImageBitmap bmp) {\n  if (!bmp.ConvertColorDepth(FREE_IMAGE_COLOR_DEPTH.FICD_24_BPP))\n    throw new InvalidOperationException(\"合并图片失败：无法转换图片颜色\");\n}\n\n// after\nstatic bool TryExpandColorDepth(FreeImageBitmap bmp) {\n  return bmp.ConvertColorDepth(FREE_IMAGE_COLOR_DEPTH.FICD_24_BPP);\n}\n// caller: if (!TryExpandColorDepth(bmp)) { ExportSeparately(bmp); return; }","handlingStrategy":"try-catch","validationCode":"if (bmp.HasPalette && !CanConvertTo24Bpp(bmp)) {\n    ExportSeparately(bmp); return;\n}","typeGuard":"static bool CanConvertTo24Bpp(FreeImageBitmap b) =>\n    b.ColorDepth <= 24; // heuristic; actual check is via ConvertColorDepth","tryCatchPattern":"try { TryExpandColorDepth(bmp); }\ncatch (InvalidOperationException) { ExportSeparately(bmp); }","preventionTips":["Validate source image format before merging.","Fall back to individual export on conversion failure.","Keep the FreeImage native library version current."],"tags":["image","freeimage","color-depth","merge"],"backgroundTag":null,"analyzedSha":"4782bbd9ada850b56258f29999b7a1c9076c9b1b","analyzedAt":"2026-08-13T17:44:50.812Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}