wmjordan/PDFPatcher · error · InvalidOperationException

合并图片失败:无法转换图片颜色

Error message

合并图片失败:无法转换图片颜色

What it means

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.

Source

Thrown at App/Processor/ImageExtractor.cs:716

			static ColorType GetWhiteBlackType(RGBQUAD[] colors) {
				var c0 = colors[0].Color;
				var c1 = colors[1].Color;
				return c0 == Color.Black && c1 == Color.White ? ColorType.BlackAndWhite
					: c0 == Color.White && c1 == Color.Black ? ColorType.WhiteAndBlack
					: 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
		{

View on GitHub (pinned to 4782bbd9ad)

Solutions

  1. Pre-check the source bitmap's color depth and handle unsupported formats before attempting the merge.
  2. Fall back to per-image export instead of merging when conversion fails (catch and export individually).
  3. Update/redeploy the FreeImage native dependency to ensure conversion support.
  4. Validate the extracted image is loadable in a standalone FreeImage tool before batch processing.

Example fix

// before
static void TryExpandColorDepth(FreeImageBitmap bmp) {
  if (!bmp.ConvertColorDepth(FREE_IMAGE_COLOR_DEPTH.FICD_24_BPP))
    throw new InvalidOperationException("合并图片失败:无法转换图片颜色");
}

// after
static bool TryExpandColorDepth(FreeImageBitmap bmp) {
  return bmp.ConvertColorDepth(FREE_IMAGE_COLOR_DEPTH.FICD_24_BPP);
}
// caller: if (!TryExpandColorDepth(bmp)) { ExportSeparately(bmp); return; }
Defensive patterns

Strategy: try-catch

Validate before calling

if (bmp.HasPalette && !CanConvertTo24Bpp(bmp)) {
    ExportSeparately(bmp); return;
}

Type guard

static bool CanConvertTo24Bpp(FreeImageBitmap b) =>
    b.ColorDepth <= 24; // heuristic; actual check is via ConvertColorDepth

Try / catch

try { TryExpandColorDepth(bmp); }
catch (InvalidOperationException) { ExportSeparately(bmp); }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of wmjordan/PDFPatcher@4782bbd9ad (2026-08-13). Data as JSON: /api/errors/ef292022ace53ae2. Report an issue: GitHub.