This will be little longer post
Render XPS document from FlowDocument while maintaining original Images (no transformation)
Image is added correctly (keeps format), but only first one. Cache is then broken, only 1 image is embedded in XPS and used for all images
All images are added and correct, but converted to PNG.
Difference occurs based on commenting / uncommenting of single line in method GetImage
(see comments)
Minimal showcase / problem recreation code
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
I no longer have any idea what to do with this. All possible cache enabling / disabling was tried.
I have not done any visual basic, but I ran into similar problem with c#. Try returning a BitmapImage instead of ImageSource in your GetImage method. Using PngBitmapDecoder, MemoryStream, FlowDocument, XPSDocument to Preview Images
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.