2015-02-09 99 views
4

我只使用一个名为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对象上几乎每一个可能的选项包括TextContrastTextRenderingHintSmoothingModePixelOffsetMode ...

我错过哪些配置的Bitmap对象事业上这个?

enter image description here

编辑2

经过大量的搜索,并作为@BoeseB提到我刚刚发现Pdfium通过提供第二渲染方法FPDF_RenderPageBitmap渲染设备句柄和位图不同,目前我挣扎将其本地BGRA位图格式转换为托管Bitmap

编辑

TextRenderingHint enter image description here

不同的模式也试过Application.SetCompatibleTextRenderingDefault(false)没有明显的差异。

+1

在我的闪屏项目中,我遇到过类似问题上的图像渲染文本流行的方法。它帮助设置TextRenderingHint。你已经尝试了所有不同的设置?结果如何查找不同的设置? – BoeseB 2015-02-09 14:08:09

+1

请描述文本渲染问题,或者更好地包含错误屏幕截图的链接。此外,我只是注意到,WPF总是绘制缩放到他们的DPI的位图。由于您尚未在上面的代码中明确设置DPI,因此Bitmap的DPI与您的显示器的DPI之间可能存在不匹配。 – RogerN 2015-02-09 14:19:42

+0

如果您使用Application.SetCompatibleTextRenderingDefault(false),会发生什么情况;显示控件之前?我想控制使用GDI +(graphics.DrawString)来显示文本和你的绘制到位图使用GDI(TextRenderer.DrawText)看到这个答案http://stackoverflow.com/a/23230570/4369295 – BoeseB 2015-02-09 14:28:27

回答

1

难道不是你的issue? 最近看了fix。 如您所见,资料库所有者提交了PdfiumViewer的较新版本。现在,你可以写这样:

var pdfDoc = PdfDocument.Load(@"mydoc.pdf"); 
var pageImage = pdfDoc.Render(pageNum, width, height, dpiX, dpiY, isForPrinting); 
pageImage.Save("test.png", ImageFormat.Png); 

// to display it on WPF canvas 
BitmapSource source = ImageToBitmapSource(pageImage); 
canvas.DrawImage(source, rect);  // canvas is instance of DrawingContext 

这里是一个图像转换为ImageSource的

BitmapSource ImageToBitmapSource(System.Drawing.Image image) 
{ 
    using(MemoryStream memory = new MemoryStream()) 
    { 
     image.Save(memory, ImageFormat.Bmp); 
     memory.Position = 0; 
     var source = new BitmapImage(); 
     source.BeginInit(); 
     source.StreamSource = memory; 
     source.CacheOption = BitmapCacheOption.OnLoad; 
     source.EndInit(); 

     return source; 
    } 
} 
+0

是的,就是这样。 – 2015-02-14 13:13:50

+0

@MohsenAfshin我添加了一些代码示例。我用PdfiumViewer v 1.4.0.0测试过它,它的功能就像一个魅力。比ghostscrypt更快,更漂亮。猜猜你的问题已解决。 – shameleo 2015-02-14 21:47:54

+0

谢谢@shameleo,我已经做到了。 – 2015-02-15 05:09:23