位图图形VS WinForm控件图形 [英] Bitmap Graphics vs WinForm Control Graphics
问题描述
我只是用 Pdfium
的.NET端口命名的 PdfiumViewer 。这只是工作得很好,一旦呈现在WinForm控件但是当我尝试以使其在位图
显示在WPF窗口(甚至是保存到磁盘)呈现的文本有问题。
I'm just using a .NET port of Pdfium
named PdfiumViewer. It just works very well once rendered in the WinForm controls but when I try to render it on a Bitmap
to show in the WPF windows (or even saving to disk) the rendered text has problem.
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);
这是引人注目的说,我测试了图形
对象上几乎每一个可能的选项包括<$c$c>TextContrast$c$c>,<$c$c>TextRenderingHint$c$c>,<$c$c>SmoothingMode$c$c>,<$c$c>PixelOffsetMode$c$c>, ...
It's notable to say that I tested almost every possible options on the Graphics
object including TextContrast
,TextRenderingHint
,SmoothingMode
,PixelOffsetMode
, ...
哪些配置我失踪,导致这个位图
对象?
Which configurations I'm missing on the Bitmap
object that cause this?
编辑2
很多搜索@BoeseB,并提到后,我只是发现Pdfium通过提供第二渲染方法渲染设备句柄和位图不同的<一个href=\"http://tickets.foxitsoftware.com/support/usermanuals/DLL311/group___f_p_d_f_v_i_e_w.html#ga78e560fce2c6efa038afd4ab64526baa\"相对=nofollow> FPDF_RenderPageBitmap ,目前我挣扎到其本机BGRA位图格式转换为托管位图
。
After lots of searching and as @BoeseB mentioned I just found that Pdfium render device handle and bitmaps differently by providing a second render method FPDF_RenderPageBitmap and currently I'm struggling to convert its native BGRA bitmap format to managed Bitmap
.
修改
也试过 Application.SetCompatibleTextRenderingDefault(假)
,没有明显的区别。
Also tried Application.SetCompatibleTextRenderingDefault(false)
with no noticeable difference.
推荐答案
这难道不是你的问题的?
看看最近修复它。
正如你所看到的,库所有者COMMITED PdfiumViewer的新版本。现在,你可以写这样的:
Isn't it your issue ? Look recent fix for it. As you can see, repository owner commited newer version of PdfiumViewer. Now you can write this way:
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流行的做法
Here is a popular approach to convert Image to 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;
}
}
这篇关于位图图形VS WinForm控件图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!