如何将 XPS 文件转换为高质量图像(而不是模糊的低分辨率)?
我正在尝试使用 WPF 转换 XPS。
这个想法是这些图像可以使用 silverlight 4 加载,为此我使用以下代码:
// XPS Document
XpsDocument xpsDoc = new XpsDocument(xpsFileName, System.IO.FileAccess.Read);
FixedDocumentSequence docSeq = xpsDoc.GetFixedDocumentSequence();
// The number of pages
PageCount = docSeq.References[0].GetDocument(false).Pages.Count;
DocumentPage sizePage = docSeq.DocumentPaginator.GetPage(0);
PageHeight = sizePage.Size.Height;
PageWidth = sizePage.Size.Width;
// Scale dimensions from 96 dpi to 600 dpi.
double scale = 300/ 96;
// Convert a XPS page to a PNG file
for (int pageNum = 0; pageNum < PageCount; pageNum++)
{
DocumentPage docPage = docSeq.DocumentPaginator.GetPage(pageNum);
BitmapImage bitmap = new BitmapImage();
RenderTargetBitmap renderTarget =
new RenderTargetBitmap((int)(scale * (docPage.Size.Height + 1)),
(int)(scale * (docPage.Size.Height + 1)),
scale * 96,
scale * 96, PixelFormats.Pbgra32);
renderTarget.Render(docPage.Visual);
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(renderTarget));
FileStream pageOutStream = new FileStream(name + ".Page" + pageNum + ".png", FileMode.Create, FileAccess.Write);
encoder.Save(pageOutStream);
pageOutStream.Close();
此代码取自 http ://xpsreader.codeplex.com/ 一个用于转换 XPS 文档的项目。 效果很好! 但问题是图像分辨率低且模糊。 我研究并发现 RenderTargetBitmap 并在此页面上找到: http://www.codeproject.com/Questions/213737/Render -target-bitmap-quality-issues
这里的问题是您没有使用硬件 RenderTargetBitmap 渲染。
一种解决方案是使用 DirectX 和 WPF 来执行此操作,但是还没有找到任何明确的例子来向我展示正确的方法。
我很欣赏建议。提前致谢。
更新:我附上了XPS文档,我正在尝试转换图像 请下载test.xps
I'm trying to convert an XPS with WPF.
The idea is that these images can be loaded with silverlight 4, for this I am using the following code:
// XPS Document
XpsDocument xpsDoc = new XpsDocument(xpsFileName, System.IO.FileAccess.Read);
FixedDocumentSequence docSeq = xpsDoc.GetFixedDocumentSequence();
// The number of pages
PageCount = docSeq.References[0].GetDocument(false).Pages.Count;
DocumentPage sizePage = docSeq.DocumentPaginator.GetPage(0);
PageHeight = sizePage.Size.Height;
PageWidth = sizePage.Size.Width;
// Scale dimensions from 96 dpi to 600 dpi.
double scale = 300/ 96;
// Convert a XPS page to a PNG file
for (int pageNum = 0; pageNum < PageCount; pageNum++)
{
DocumentPage docPage = docSeq.DocumentPaginator.GetPage(pageNum);
BitmapImage bitmap = new BitmapImage();
RenderTargetBitmap renderTarget =
new RenderTargetBitmap((int)(scale * (docPage.Size.Height + 1)),
(int)(scale * (docPage.Size.Height + 1)),
scale * 96,
scale * 96, PixelFormats.Pbgra32);
renderTarget.Render(docPage.Visual);
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(renderTarget));
FileStream pageOutStream = new FileStream(name + ".Page" + pageNum + ".png", FileMode.Create, FileAccess.Write);
encoder.Save(pageOutStream);
pageOutStream.Close();
This code is taken from http://xpsreader.codeplex.com/ a project to convert an XPS document.
works great!
But the problem is that the image is low resolution and blurry.
I researched and found that RenderTargetBitmap and find on this page:
http://www.codeproject.com/Questions/213737/Render-target-bitmap-quality-issues
The issue here is you Have That does not use hardware RenderTargetBitmap rendering.
One solution is to use DirectX with WPF to do this, but have not found any clear example to show me the right way to do it.
I appreciate suggestions. Thanks in advance.
Update:I attached the XPS document, I am trying to convert the image
Please download test.xps
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
sourceforge.net 上有一个名为 xps2img 的项目,可以将 xps 转换为图像。它是用 C# 编写的,还包含源代码。一探究竟。它将帮助您实现您想要的目标。
http://sourceforge.net/projects/xps2img/files/
There is a project named xps2img on sourceforge.net which converts an xps to image. It is made in C# and also contains the source code. Check it out. It will help you to achieve what you want.
http://sourceforge.net/projects/xps2img/files/
我在这篇文章和许多其他文章中看到人们在将 DocumentPage 转换为图像并将其保存在 HDD 上时遇到问题。
此方法从文档查看器中获取所有页面并将它们作为 jpg 图像保存在 HDD 上。
I saw in this post and in many others that peoples have problems with conversion of DocumentPage to Image and saving it on HDD.
This method took all pages from document viewer and save them on HDD as jpg images.
基于 xps2img 的 nuget 包现已推出:
https://www.nuget.org/packages/xps2img/
API 可用:
https://github.com/peters/xps2img#usage
A nuget package based on xps2img is now available:
https://www.nuget.org/packages/xps2img/
Api available here:
https://github.com/peters/xps2img#usage