我只使用一個名爲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