我只使用一个名为PdfiumViewer的.NET端口Pdfium
。它在WinForm控件中呈现时效果很好,但是当我尝试在Bitmap
上呈现它以在WPF窗口中显示(甚至保存到磁盘)时,呈现的文本有问题。位图图形与WinForm控件图形
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)
没有明显的差异。
在我的闪屏项目中,我遇到过类似问题上的图像渲染文本流行的方法。它帮助设置TextRenderingHint。你已经尝试了所有不同的设置?结果如何查找不同的设置? – BoeseB 2015-02-09 14:08:09
请描述文本渲染问题,或者更好地包含错误屏幕截图的链接。此外,我只是注意到,WPF总是绘制缩放到他们的DPI的位图。由于您尚未在上面的代码中明确设置DPI,因此Bitmap的DPI与您的显示器的DPI之间可能存在不匹配。 – RogerN 2015-02-09 14:19:42
如果您使用Application.SetCompatibleTextRenderingDefault(false),会发生什么情况;显示控件之前?我想控制使用GDI +(graphics.DrawString)来显示文本和你的绘制到位图使用GDI(TextRenderer.DrawText)看到这个答案http://stackoverflow.com/a/23230570/4369295 – BoeseB 2015-02-09 14:28:27