FlowDocument中的XPS渲染图像中的错误

时间:2014-02-18 17:30:05

标签: wpf vb.net xps

这将是更长的帖子

目标

在保留原始图像(无转换)的情况下从FlowDocument渲染XPS文档

现有方案

  1. 图像添加正确(保持格式),但只有第一个。然后打破缓存,在XPS中只嵌入1个图像并用于所有图像

  2. 所有图片都已添加并正确,但已转换为PNG。

  3. 根据方法GetImage中单行的注释/取消注释发生差异(请参阅注释)

    最小的展示/问题娱乐代码

    Imports System.Windows.Documents
    Imports System.Windows.Documents.Serialization
    Imports System.Windows.Xps.Packaging
    Imports System.Windows.Xps
    Imports System.IO
    Imports System.IO.Packaging
    Imports System.Windows.Markup
    Imports System.Windows.Media.Imaging
    Imports System.Windows.Media
    Imports System.Windows.Controls
    
    Module Module1
    
        Sub Main()
    
            Render()
    
        End Sub
    
        Sub Render()
    
            Using image1 = IO.File.OpenRead("image1.jpg"),
                image2 = IO.File.OpenRead("image2.png"),
                file = IO.File.Create("asdf.xps"),
                pack = Package.Open(file, FileMode.Create),
                d As New XpsDocument(pack)
    
    
                Dim writer As XpsDocumentWriter = XpsDocument.CreateXpsDocumentWriter(d)
                Dim xpsVisWriter As SerializerWriterCollator = writer.CreateVisualsCollator()
    
                Dim x As New FlowDocument
                x.PageWidth = 100
                x.ColumnWidth = x.PageWidth
    
                Dim s As New Section
                DirectCast(x, IAddChild).AddChild(s)
                s.BreakPageBefore = True
    
                Dim p As New Paragraph()
                DirectCast(s, IAddChild).AddChild(p)
    
                Dim i As New Image
                DirectCast(p, IAddChild).AddChild(i)
                i.Source = GetImage(image1)
    
                Dim i2 As New Image
                DirectCast(p, IAddChild).AddChild(i2)
                i2.Source = GetImage(image2)
    
                Dim paginator = DirectCast(x, IDocumentPaginatorSource).DocumentPaginator
                Dim pageIndex As Integer = 0
    
                While Not paginator.IsPageCountValid OrElse paginator.PageCount > pageIndex
                    Dim page As DocumentPage = paginator.GetPage(pageIndex)
    
    
                    xpsVisWriter.Write(page.Visual)
                    pageIndex += 1
                End While
    
    
                xpsVisWriter.EndBatchWrite()
    
            End Using
    
        End Sub
    
        Function GetImage(stream As Stream) As ImageSource
    
            Dim result As ImageSource = BitmapFrame.Create(
                stream,
                BitmapCreateOptions.PreservePixelFormat Or BitmapCreateOptions.IgnoreImageCache,
                BitmapCacheOption.None)
    
            'If this Line Runs, scenario 2 occures, otherwise scenario 1
            'result = BitmapFrame.Create(result)
    
            Return result
    
        End Function
    
    End Module
    

    我不知道该怎么做。尝试了所有可能的缓存启用/禁用。

1 个答案:

答案 0 :(得分:0)

我没有做任何视觉基础,但我遇到了与c#类似的问题。尝试在GetImage方法中返回BitmapImage而不是ImageSource。 Using PngBitmapDecoder, MemoryStream, FlowDocument, XPSDocument to Preview Images

相关问题