我只是使用一个名为PdfiumViewer的 .NET 版本的
Pdfium
。在 WinForm 控件中呈现后,它运行得非常好,但当我尝试在 Bitmap
上呈现它以在 WPF 窗口中显示(甚至保存到磁盘)时,呈现的文本存在问题。var pdfDoc = PdfiumViewer.PdfDocument.Load(FileName);
int width = (int)(this.ActualWidth - 30) / 2;
int height = (int)this.ActualHeight - 30;
var bitmap = new System.Drawing.Bitmap(width, height);
var g = System.Drawing.Graphics.FromImage(bitmap);
g.FillRegion(System.Drawing.Brushes.White, new System.Drawing.Region(
new System.Drawing.RectangleF(0, 0, width, height)));
pdfDoc.Render(1, g, g.DpiX, g.DpiY, new System.Drawing.Rectangle(0, 0, width, height), false);
// Neither of these are readable
image.Source = BitmapHelper.ToBitmapSource(bitmap);
bitmap.Save("test.bmp");
// Directly rendering to a System.Windows.Forms.Panel control works well
var controlGraphics = panel.CreateGraphics();
pdfDoc.Render(1, controlGraphics, controlGraphics.DpiX, controlGraphics.DpiY,
new System.Drawing.Rectangle(0, 0, width, height), false);
值得一提的是,我在Graphics
对象上测试了几乎所有可能的选项,包括TextContrast
、TextRenderingHint
、SmoothingMode
、PixelOffsetMode
等。
我错过了哪些Bitmap
对象的配置导致这种情况发生?
编辑2
经过大量搜索,正如@BoeseB所提到的那样,我刚刚发现Pdfium渲染设备通过提供第二个渲染方法FPDF_RenderPageBitmap来处理句柄和位图,目前我正在努力将其本地BGRA位图格式转换为托管Bitmap
。
编辑
还尝试了Application.SetCompatibleTextRenderingDefault(false)
,但没有明显的区别。