2015-02-09 91 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