gpt4 book ai didi

c# - 位图图形与 WinForm 控件图形

转载 作者:行者123 更新时间:2023-11-30 12:44:28 25 4
gpt4 key购买 nike

我只是使用名为 PdfiumViewerPdfium 的 .NET 端口.一旦在 WinForm 控件中呈现,它就可以很好地工作,但是当我尝试在 Bitmap 上呈现它以在 WPF 窗口中显示(甚至保存到磁盘)时,呈现的文本有问题。

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 对象上几乎所有可能的选项,包括 TextContrastTextRenderingHintSmoothingMode,PixelOffsetMode, ...

Bitmap 对象缺少哪些配置会导致此问题?

enter image description here

编辑2

经过大量搜索,正如@BoeseB 提到的那样,我刚刚发现 Pdfium 渲染设备句柄和位图通过提供第二种渲染方法而有所不同 FPDF_RenderPageBitmap目前我正在努力将其原生 BGRA 位图格式转换为托管 Bitmap

编辑

TextRenderingHint的不同模式 enter image description here

还尝试了 Application.SetCompatibleTextRenderingDefault(false),没有明显差异。

最佳答案

这不是你的吗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

这是一种流行的将Image转换为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;
}
}

关于c# - 位图图形与 WinForm 控件图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28411460/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com