{"record":{"id":"4275f7bd32a0c5fb","repo":"wmjordan/PDFPatcher","slug":"error-4275f7","errorCode":null,"errorMessage":"合并图片失败：粘贴操作出错","messagePattern":"合并图片失败：粘贴操作出错","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"App/Processor/ImageExtractor.cs","lineNumber":722,"sourceCode":"\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\n\t\t\tUndefined,\r\n\t\t\tBlackAndWhite,\r\n\t\t\tWhiteAndBlack,\r\n\t\t\tIndexedColor,\r\n\t\t\tFullColor,\r\n\t\t}\r","sourceCodeStart":704,"sourceCodeEnd":740,"githubUrl":"https://github.com/wmjordan/PDFPatcher/blob/4782bbd9ada850b56258f29999b7a1c9076c9b1b/App/Processor/ImageExtractor.cs#L704-L740","documentation":"ImageExtractor's PasteAndIncrementHeight throws InvalidOperationException when FreeImageBitmap.Paste returns false, indicating the paste of a source bitmap into the composite _Image at the current _Height offset failed. Paste can fail on dimension mismatches, format incompatibilities, or internal FreeImage errors.","triggerScenarios":"Calling the merge paste path with a bitmap whose dimensions/format are incompatible with the target composite, or when the composite _Image is in a state (e.g. wrong color depth) that rejects the paste. The guard treats the boolean Paste return as success/failure.","commonSituations":"Merging extracted PDF images where one image has a different bit depth or palette than the composite built so far; a width mismatch between the source and composite; memory pressure causing the native paste to fail; a corrupted extracted image.","solutions":["Ensure the composite and source share the same color depth (call TryExpandColorDepth first) before pasting.","Verify the source bitmap width matches the composite width before attempting paste.","Catch the exception and fall back to exporting images individually rather than merging.","Update the FreeImage native library and check for corrupt source images."],"exampleFix":"// before\nvoid PasteAndIncrementHeight(FreeImageBitmap bmp) {\n  if (!_Image.Paste(bmp, 0, _Height, Int32.MaxValue))\n    throw new InvalidOperationException(\"合并图片失败：粘贴操作出错\");\n  _Height += bmp.Height;\n}\n\n// after\nvoid PasteAndIncrementHeight(FreeImageBitmap bmp) {\n  if (bmp.Width != _Image.Width || !_Image.Paste(bmp, 0, _Height, Int32.MaxValue)) {\n    Tracker.TraceMessage(Tracker.Category.Warning, \"粘贴失败，改为单独导出。\");\n    ExportSeparately(bmp);\n    return;\n  }\n  _Height += bmp.Height;\n}","handlingStrategy":"try-catch","validationCode":"if (bmp.Width != _Image.Width) { ExportSeparately(bmp); return; }\n// also ensure matching color depth before paste","typeGuard":"static bool CanPasteInto(FreeImageBitmap target, FreeImageBitmap src) =>\n    target.Width == src.Width && target.ColorDepth == src.ColorDepth;","tryCatchPattern":"try { PasteAndIncrementHeight(bmp); }\ncatch (InvalidOperationException) { ExportSeparately(bmp); }","preventionTips":["Match composite and source widths and color depths before paste.","Fall back to per-image export on paste failure.","Validate extracted images aren't corrupt before merging."],"tags":["image","freeimage","merge","paste"],"backgroundTag":null,"analyzedSha":"4782bbd9ada850b56258f29999b7a1c9076c9b1b","analyzedAt":"2026-08-13T17:44:50.812Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}