{"record":{"id":"8625ddc4f0305227","repo":"wmjordan/PDFPatcher","slug":"page-pagenumber-1-totext","errorCode":null,"errorMessage":"无法渲染页面：{(page.PageNumber + 1).ToText()}","messagePattern":"无法渲染页面：(.+?)","errorType":"exception","errorClass":"MuException","httpStatus":null,"severity":"error","filePath":"App/Processor/Mupdf/MuPDFExtensions.cs","lineNumber":151,"sourceCode":"\t\treturn StringBuilderCache.GetStringAndRelease(sb);\r\n\t}\r\n\r\n\t#region 渲染页面\r\n\tpublic static Bitmap RenderBitmapPage(this Page page, int width, int height, ImageRendererOptions options, Cookie cookie) {\r\n\t\tusing var pix = InternalRenderPage(page, width, height, options, cookie);\r\n\t\treturn pix?.ToBitmap(options);\r\n\t}\r\n\r\n\tstatic Pixmap InternalRenderPage(Page page, int width, int height, ImageRendererOptions options, Cookie cookie) {\r\n\t\tvar b = page.Bound;\r\n\t\tif (b.Width == 0 || b.Height == 0) {\r\n\t\t\treturn null;\r\n\t\t}\r\n\t\tvar ctm = CalculateMatrix(page, width, height, options);\r\n\t\tvar bbox = width > 0 && height > 0 ? new BBox(0, 0, width, height) : b.Transform(ctm).Round();\r\n\r\n\t\tvar pix = Pixmap.Create(((ColorspaceKind)options.ColorSpace).SubstituteDefault(ColorspaceKind.RGB), bbox)\r\n\t\t\t?? throw new MuException($\"无法渲染页面：{(page.PageNumber + 1).ToText()}\");\r\n\t\tpix.Clear(0xFF);\r\n\t\ttry {\r\n\t\t\tusing var dev = Device.NewDraw(pix, Matrix.Identity);\r\n\t\t\tif (options.LowQuality) {\r\n\t\t\t\tdev.EnableDeviceHints(DeviceHints.DontInterpolateImages | DeviceHints.NoCache);\r\n\t\t\t}\r\n\t\t\tif (cookie.IsCancellationPending) {\r\n\t\t\t\tgoto CANCEL;\r\n\t\t\t}\r\n\t\t\tpage.RunContents(dev, ctm, cookie);\r\n\t\t\tif (!options.HideAnnotations) {\r\n\t\t\t\tpage.RunAnnotations(dev, ctm, cookie);\r\n\t\t\t\tpage.RunWidgets(dev, ctm, cookie);\r\n\t\t\t}\r\n\t\t\tdev.Close();\r\n\r\n\t\t\tif (cookie.IsCancellationPending) {\r\n\t\t\t\tgoto CANCEL;\r","sourceCodeStart":133,"sourceCodeEnd":169,"githubUrl":"https://github.com/wmjordan/PDFPatcher/blob/4782bbd9ada850b56258f29999b7a1c9076c9b1b/App/Processor/Mupdf/MuPDFExtensions.cs#L133-L169","documentation":"Thrown as a MuException when MuPDF's Pixmap.Create returns null while allocating the output pixmap for a page render in InternalRenderPage. Pixmap.Create fails when the computed BBox has non-positive dimensions after rounding, when the requested area is too large to allocate, or when the resolved colorspace is invalid. The message reports the 1-based page number (page.PageNumber + 1). Note this fires only at allocation time; later drawing errors surface from page.RunContents/RunAnnotations.","triggerScenarios":"RenderBitmapPage(page,width,height,options,cookie) -> InternalRenderPage -> Pixmap.Create(colorspace, bbox) returns null. Two bbox paths: when width>0 && height>0 the bbox is literally (0,0,width,height) so an enormous explicit width/height causes allocation failure; otherwise bbox = b.Transform(ctm).Round() which can round to a degenerate rectangle. Worker.cs calls RenderBitmapPage(options.ImageWidth, 0, ...) so height==0 forces the transform-round path, and a near-zero or negative rounded bbox yields null.","commonSituations":"Rendering at very high DPI or very large explicit ImageWidth against a big page (pixmap exceeds available memory); a page whose MediaBox/CropBox is tiny or rotated such that the scaled+rounded bbox collapses to zero area; corrupted page geometry where Bound.Width/Height passed the early guard but the transform still degenerates; passing a colorspace option value outside the valid ColorspaceKind enum so SubstituteDefault cannot resolve it.","solutions":["Clamp width/height and DPI before calling RenderBitmapPage so the resulting pixmap (width*height*components bytes) stays within memory limits.","If you pass height==0 (as Worker.cs does), verify the page Bound is non-degenerate and that ImageWidth * (Bound.Height/Bound.Width) is a positive, finite number.","Wrap the RenderBitmapPage call in try/catch(MuException) and treat the page as unrenderable (skip / show placeholder) rather than aborting the batch.","Validate that options.ColorSpace maps to a real ColorspaceKind before rendering; pass a known colorspace (RGB/Gray/BGR)."],"exampleFix":"// before\nusing var bmp = p.RenderBitmapPage(options.ImageWidth, 0, options, cookie);\n\n// after\nvar bound = p.Bound;\nvar h = bound.Width > 0 ? (int)Math.Round(options.ImageWidth * bound.Height / bound.Width) : 0;\nif (options.ImageWidth <= 0 || h <= 0 || (long)options.ImageWidth * h > 100_000_000) {\n    Tracker.TraceMessage($\"跳过无法渲染的页面：尺寸异常 ({options.ImageWidth}x{h})\");\n    continue;\n}\nusing var bmp = p.RenderBitmapPage(options.ImageWidth, h, options, cookie);","handlingStrategy":"try-catch","validationCode":"var bound = page.Bound;\nif (bound.Width <= 0 || bound.Height <= 0) return null; // degenerate page\nint w = width > 0 ? width : (int)Math.Round(bound.Width * options.ScaleRatio * options.Dpi / 72);\nint h = height > 0 ? height : (int)Math.Round(bound.Height * options.ScaleRatio * options.Dpi / 72);\nif (w <= 0 || h <= 0 || (long)w * h > 50_000_000) return null; // too small or too large to allocate","typeGuard":null,"tryCatchPattern":"Bitmap bmp = null;\ntry {\n    bmp = page.RenderBitmapPage(width, height, options, cookie);\n}\ncatch (MuException ex) when (!cookie.IsCancellationPending) {\n    Tracker.TraceMessage($\"渲染第 {page.PageNumber + 1} 页失败：{ex.Message}\");\n    bmp = null; // skip / placeholder\n}\nif (bmp == null && cookie.IsCancellationPending) return null;","preventionTips":["Cap render DPI and explicit width so width*height stays under a memory budget (e.g. 50 MP).","Never pass a non-positive target height with a positive width unless you have confirmed the page Bound is finite and non-degenerate.","Resolve options.ColorSpace to a concrete ColorspaceKind (RGB/Gray/BGR) before rendering.","Always handle the MuException from RenderBitmapPage in batch/worker loops so one bad page does not abort the run."],"tags":["mupdf","pdf-rendering","pixmap","memory","csharp"],"backgroundTag":null,"analyzedSha":"4782bbd9ada850b56258f29999b7a1c9076c9b1b","analyzedAt":"2026-08-13T17:44:50.812Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}