wmjordan/PDFPatcher · error · InvalidOperationException
合并图片失败:粘贴操作出错
Error message
合并图片失败:粘贴操作出错
What it means
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.
Source
Thrown at App/Processor/ImageExtractor.cs:722
: ColorType.IndexedColor;
}
void PasteIntoFullColor(FreeImageBitmap bmp) {
if (bmp.HasPalette) {
TryExpandColorDepth(bmp);
}
PasteAndIncrementHeight(bmp);
}
static void TryExpandColorDepth(FreeImageBitmap bmp) {
if (!bmp.ConvertColorDepth(FREE_IMAGE_COLOR_DEPTH.FICD_24_BPP)) {
throw new InvalidOperationException("合并图片失败:无法转换图片颜色");
}
}
void PasteAndIncrementHeight(FreeImageBitmap bmp) {
if (!_Image.Paste(bmp, 0, _Height, Int32.MaxValue)) {
throw new InvalidOperationException("合并图片失败:粘贴操作出错");
}
_Height += bmp.Height;
}
void ExpandColorSpaceAndPaste(FreeImageBitmap bmp) {
TryExpandColorDepth(_Image);
PasteIntoFullColor(bmp);
}
}
enum ColorType
{
Undefined,
BlackAndWhite,
WhiteAndBlack,
IndexedColor,
FullColor,
}
View on GitHub (pinned to 4782bbd9ad)
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.
Example fix
// before
void PasteAndIncrementHeight(FreeImageBitmap bmp) {
if (!_Image.Paste(bmp, 0, _Height, Int32.MaxValue))
throw new InvalidOperationException("合并图片失败:粘贴操作出错");
_Height += bmp.Height;
}
// after
void PasteAndIncrementHeight(FreeImageBitmap bmp) {
if (bmp.Width != _Image.Width || !_Image.Paste(bmp, 0, _Height, Int32.MaxValue)) {
Tracker.TraceMessage(Tracker.Category.Warning, "粘贴失败,改为单独导出。");
ExportSeparately(bmp);
return;
}
_Height += bmp.Height;
} Defensive patterns
Strategy: try-catch
Validate before calling
if (bmp.Width != _Image.Width) { ExportSeparately(bmp); return; }
// also ensure matching color depth before paste Type guard
static bool CanPasteInto(FreeImageBitmap target, FreeImageBitmap src) =>
target.Width == src.Width && target.ColorDepth == src.ColorDepth; Try / catch
try { PasteAndIncrementHeight(bmp); }
catch (InvalidOperationException) { ExportSeparately(bmp); } Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- 合并图片失败:无法转换图片颜色
- File is not a valid JPEG
- Could not find Exif data block
- Exif data not found
- Malformed Exif data
AI-assisted analysis of wmjordan/PDFPatcher@4782bbd9ad (2026-08-13).
Data as JSON: /api/errors/4275f7bd32a0c5fb.
Report an issue: GitHub.