c# – 位图图形与WinForm控制图形
发布时间:2020-12-15 22:11:21 所属栏目:百科 来源:网络整理
导读:我只是使用名为 PdfiumViewer的Pdfium的.NET端口.它在WinForm控件中呈现时效果很好但是当我尝试在Bitmap上呈现它以在WPF窗口中显示(或者甚至保存到磁盘)时文字有问题. var pdfDoc = PdfiumViewer.PdfDocument.Load(FileName);int width = (int)(this.ActualW
我只是使用名为
PdfiumViewer的Pdfium的.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,width,height))); pdfDoc.Render(1,g,g.DpiX,g.DpiY,new System.Drawing.Rectangle(0,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,false); 值得注意的是,我在Graphics对象上测试了几乎所有可能的选项,包括TextContrast,TextRenderingHint,SmoothingMode,PixelOffsetMode,…… 我在Bitmap对象上缺少哪些配置导致这种情况? 编辑2 经过大量的搜索和@BoeseB提到后,我发现Pdfium通过提供第二种渲染方法FPDF_RenderPageBitmap来渲染设备句柄和位图,目前我正在努力将其原生BGRA位图格式转换为托管位图. 编辑 TextRenderingHint的不同模式 还尝试了Application.SetCompatibleTextRenderingDefault(false),没有明显的区别. 解决方法
不是你的
issue吗?
看看最近的 fix吧. 如您所见,存储库所有者提交了较新版本的PdfiumViewer.现在你可以这样写: var pdfDoc = PdfDocument.Load(@"mydoc.pdf"); var pageImage = pdfDoc.Render(pageNum,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; } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |