【问题标题】:XPS from FlowDocument Rendering bug in Images来自 FlowDocument 的 XPS 图像中的渲染错误
【发布时间】:2014-03-18 15:20:38
【问题描述】:

这篇文章会长一点

目标

从 FlowDocument 渲染 XPS 文档,同时保持原始图像(无转换)

现有场景

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

  2. 所有图像均已添加且正确,但已转换为 PNG。

基于方法 GetImage 中单行的注释/取消注释会产生差异(参见 cmets)

最小的展示/问题重现代码

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

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

【问题讨论】:

    标签: wpf vb.net xps


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2012-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-15
      • 2020-07-26
      • 2010-10-29
      • 2021-11-15
      相关资源
      最近更新 更多