如何将XPS文件转换为BMP文件 vb.net
如何将XPS文件转换为BMP文件
XPS是一种XML标记格式,以Windows Presentation Foundation矢量格式表示页面的内容。 XPS Document Writer允许您使用Windows上运行的任何程序来创建.xps文件。 XPS文档的打印效果与屏幕上的效果相同。 它们也很容易共享,因为您可以在装有XPS查看器的任何计算机上查看它们,即使该计算机没有用于创建原始文档的相同程序。
下面的VB.NET程序将xps文档转换为位图图像。 创建一个新的VB.NET项目,并将一个Button添加到Form,并将以下引用添加到您的项目中。
转到项目->添加引用,然后从.Net选项卡中选择这些文件
Windowsbase.dll
ReachFramework.dll
PresentationFramework.dll
PresentationCore.dll
Imports System Imports System.IO Imports System.IO.Packaging Imports System.Windows.Documents Imports System.Windows.Xps.Packaging Imports System.Windows.Media.Imaging Imports System.Collections.Generic Imports System.Windows.Forms Imports System.Text Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Try Dim xpsFile As String = "c:\Completed-Form.xps" xpsToBmp(xpsFile) MessageBox.Show("Done") Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub Public Shared Sub xpsToBmp(ByVal xpsFile As String) Dim xps As New XpsDocument(xpsFile, System.IO.FileAccess.Read) Dim sequence As FixedDocumentSequence = xps.GetFixedDocumentSequence() For pageCount As Integer = 0 To sequence.DocumentPaginator.PageCount - 1 Dim page As DocumentPage = sequence.DocumentPaginator.GetPage(pageCount) Dim toBitmap As New RenderTargetBitmap(CInt(page.Size.Width), CInt(page.Size.Height), 96, 96, System.Windows.Media.PixelFormats.[Default]) toBitmap.Render(page.Visual) Dim bmpEncoder As BitmapEncoder = New BmpBitmapEncoder() bmpEncoder.Frames.Add(BitmapFrame.Create(toBitmap)) Dim fStream As New FileStream("c:\xpstobmp" & pageCount & ".bmp", FileMode.Create, FileAccess.Write) bmpEncoder.Save(fStream) fStream.Close() Next End Sub End Class
Imports System Imports System.IO Imports System.IO.Packaging Imports System.Windows.Documents Imports System.Windows.Xps.Packaging Imports System.Windows.Media.Imaging Imports System.Collections.Generic Imports System.Windows.Forms Imports System.Text Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Try Dim xpsFile As String = "c:\Completed-Form.xps" xpsToBmp(xpsFile) MessageBox.Show("Done") Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub Public Shared Sub xpsToBmp(ByVal xpsFile As String) Dim xps As New XpsDocument(xpsFile, System.IO.FileAccess.Read) Dim sequence As FixedDocumentSequence = xps.GetFixedDocumentSequence() For pageCount As Integer = 0 To sequence.DocumentPaginator.PageCount - 1 Dim page As DocumentPage = sequence.DocumentPaginator.GetPage(pageCount) Dim toBitmap As New RenderTargetBitmap(CInt(page.Size.Width), CInt(page.Size.Height), 96, 96, System.Windows.Media.PixelFormats.[Default]) toBitmap.Render(page.Visual) Dim bmpEncoder As BitmapEncoder = New BmpBitmapEncoder() bmpEncoder.Frames.Add(BitmapFrame.Create(toBitmap)) Dim fStream As New FileStream("c:\xpstobmp" & pageCount & ".bmp", FileMode.Create, FileAccess.Write) bmpEncoder.Save(fStream) fStream.Close() Next End Sub End Class
Imports System Imports System.IO Imports System.IO.Packaging Imports System.Windows.Documents Imports System.Windows.Xps.Packaging Imports System.Windows.Media.Imaging Imports System.Collections.Generic Imports System.Windows.Forms Imports System.Text Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Try Dim xpsFile As String = "c:\Completed-Form.xps" xpsToBmp(xpsFile) MessageBox.Show("Done") Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub Public Shared Sub xpsToBmp(ByVal xpsFile As String) Dim xps As New XpsDocument(xpsFile, System.IO.FileAccess.Read) Dim sequence As FixedDocumentSequence = xps.GetFixedDocumentSequence() For pageCount As Integer = 0 To sequence.DocumentPaginator.PageCount - 1 Dim page As DocumentPage = sequence.DocumentPaginator.GetPage(pageCount) Dim toBitmap As New RenderTargetBitmap(CInt(page.Size.Width), CInt(page.Size.Height), 96, 96, System.Windows.Media.PixelFormats.[Default]) toBitmap.Render(page.Visual) Dim bmpEncoder As BitmapEncoder = New BmpBitmapEncoder() bmpEncoder.Frames.Add(BitmapFrame.Create(toBitmap)) Dim fStream As New FileStream("c:\xpstobmp" & pageCount & ".bmp", FileMode.Create, FileAccess.Write) bmpEncoder.Save(fStream) fStream.Close() Next End Sub End Class
赞 (0)