wmjordan/PDFPatcher · error · NotSupportedException
不支持的像素格式: colorants={colorants}, spots={spots}, alpha={hasAl
Error message
不支持的像素格式: colorants={colorants}, spots={spots}, alpha={hasAlpha} What it means
Thrown as NotSupportedException by the parameterless ToBitmap(this Pixmap) overload when the pixmap's channel layout does not match one of the four supported conversions: 8bpp grayscale (1 colorant / 1 colorant+alpha), 24bpp RGB (3 colorants, no alpha), 32bpp ARGB (3 colorants + alpha), or CMYK converted to RGB (4 colorants, no alpha). Any spot-color channels (spots>0), CMYK-with-alpha, or an unsupported colorant count falls through to this throw.
Source
Thrown at App/Processor/Mupdf/MuPDFExtensions.cs:286
if (colorants == 3 && hasAlpha && spots == 0) {
var bmp = new Bitmap(w, h, PixelFormat.Format32bppArgb);
var data = bmp.LockBits(true);
Copy32bppBgraUnpremultiply(pix, w, h, data);
bmp.UnlockBits(data);
return bmp;
}
if (colorants == 4 && !hasAlpha && spots == 0) {
var bmp = new Bitmap(w, h, PixelFormat.Format24bppRgb);
var data = bmp.LockBits(true);
using (var rgbPix = pix.ConvertColorspace(ColorspaceKind.RGB, null)) {
Copy24bppImage(rgbPix, w, h, false, data);
}
bmp.UnlockBits(data);
return bmp;
}
throw new NotSupportedException(
$"不支持的像素格式: colorants={colorants}, spots={spots}, alpha={hasAlpha}");
}
/// <summary>
/// 将 Pixmap 的数据转换为 <see cref="Bitmap"/>。
/// </summary>
public static unsafe Bitmap ToBitmap(this Pixmap pix, ImageRendererOptions options) {
int width = pix.Width;
int height = pix.Height;
bool grayscale = options.ColorSpace == (ColorSpace)ColorspaceKind.Gray;
bool invert = options.InvertColor;
var bmp = new Bitmap(width, height, grayscale ? PixelFormat.Format8bppIndexed : PixelFormat.Format24bppRgb);
var imageData = bmp.LockBits(true);
try {
if (grayscale) {
bmp.CreateStandardGrayscalePalette();
Copy8bppImage(pix, width, height, invert, imageData);
}
View on GitHub (pinned to 4782bbd9ad)
Solutions
- Flatten the pixmap to DeviceRGB before conversion: using var rgb = pix.ConvertColorspace(ColorspaceKind.RGB, null); then call ToBitmap-equivalent copy on rgb.
- Prefer the ToBitmap(this Pixmap pix, ImageRendererOptions options) overload (line 293) or RenderBitmapPage, which allocate a fixed RGB/Gray target and copy with Copy24bppImage/Copy8bppImage, bypassing this guard entirely.
- Strip spot channels by converting through an intermediate RGB pixmap so spots/alpha are resolved by MuPDF.
- Inspect pix.Colorants, pix.Spots, pix.Alpha before calling and skip/log unsupported layouts instead of letting it throw.
Example fix
// before (throws for spot/CMYK+alpha layouts) var bmp = pix.ToBitmap(); // after (force RGB, no guard hit) using var rgb = pix.ConvertColorspace(ColorspaceKind.RGB, null); var bmp = new Bitmap(rgb.Width, rgb.Height, PixelFormat.Format24bppRgb); var data = bmp.LockBits(true); Copy24bppImage(rgb, rgb.Width, rgb.Height, false, data); // or use rgb.ToBitmap(options)
Defensive patterns
Strategy: validation
Validate before calling
static bool IsSupportedPixmapLayout(Pixmap pix) {
int c = pix.Colorants, s = pix.Spots;
bool a = pix.Alpha == 1;
if (s != 0) return false;
if (c == 1) return true; // grayscale +/- alpha
if (c == 3) return true; // RGB or RGBA
if (c == 4 && !a) return true; // CMYK (converted to RGB)
return false;
} Type guard
static bool CanConvertToBitmap(Pixmap pix) =>
pix.Spots == 0 && (pix.Colorants == 1 || pix.Colorants == 3 || (pix.Colorants == 4 && pix.Alpha == 0)); Try / catch
try {
return pix.ToBitmap();
}
catch (NotSupportedException) {
using var rgb = pix.ConvertColorspace(ColorspaceKind.RGB, null);
return rgb.ToBitmap(); // retry on flattened RGB Prevention
- Prefer RenderBitmapPage or ToBitmap(pix, ImageRendererOptions) for display paths; they allocate a fixed RGB/Gray target and never hit this guard.
- For image extraction, always ConvertColorspace to RGB (or Gray) before copying samples so spot/alpha/CMYK layouts are flattened by MuPDF.
- Inspect pix.Colorants/Spots/Alpha and log unsupported layouts instead of letting the throw propagate.
When it happens
Trigger: Calling ToBitmap() on a Pixmap with spots > 0 (separation/space colorspaces), or colorants==4 && hasAlpha (CMYK+transparency), or colorants not in {1,3,4} (e.g. Lab/DeviceN with an unusual channel count), or colorants==0 && !hasAlpha. This is the parameterless ToBitmap(Pixmap) at line 237, NOT the RenderBitmapPage path which uses ToBitmap(pix, options) that forces RGB/Gray and never reaches this throw.
Common situations: Extracting embedded images that use spot/separation colorspaces (Pantone etc.); CMYK images that carry a soft mask or SMask alpha channel; DeviceN or ICCBased colorspaces whose pixmap exposes extra channels; rendering a page whose colorspace was not flattened to RGB before extraction.
Related errors
- 无法渲染页面:{(page.PageNumber + 1).ToText()}
- 在简易书签第 {lineNum} 行的缩进格式不正确。 说明:下级书签最多只能比上级书签多一个缩进标记。
- 简易书签文件内容不足。
- 找不到文件:{sourceFile}
AI-assisted analysis of wmjordan/PDFPatcher@4782bbd9ad (2026-08-13).
Data as JSON: /api/errors/c1f35bd0640684f5.
Report an issue: GitHub.