I am using the code below for years but suddenly stop to display images. I made a short version to test it and explain:
There is an UserControl
XamlPage
which contains only two controls:
<StackPanel>
<TextBlock Text="My Content"/>
<Image Source="/Images/flower.jpg" Height="100"/>
</StackPanel>
XamlPage
is used in derived class of DocumentPaginator
class:
public class Paginator : DocumentPaginator
{
double pageHeight = 1122.52;
double pageWidth = 793.7;
Size mySize;
public Paginator()
{
mySize = new Size(pageWidth, pageHeight);
}
public override DocumentPage GetPage(int pageNumber)
{
XamlPage page = new XamlPage();
page.Measure(PageSize);
page.Arrange(new Rect(new Point(0, 0), PageSize));
page.UpdateLayout();
return new DocumentPage(page, PageSize, new Rect(0, 0, 10, 10), new Rect());
}
public override bool IsPageCountValid
{
get { return true; }
}
public override int PageCount
{
get { return 1; }
}
public override Size PageSize
{
get { return mySize; }
set { mySize = value; }
}
public override IDocumentPaginatorSource Source
{
get { return null; }
}
}
In the MainWindow, only two buttons, one open new window which use the XamlPage
control, another one which open the Preview
window. The buttons are bound to Commands
in the MainVM
:
private void PreviewCommandAction()
{
Paginator paginator = new Paginator();
MemoryStream lMemoryStream = new MemoryStream();
Package package = Package.Open(lMemoryStream, FileMode.Create);
XpsDocument xpsDocument = new XpsDocument(package);
XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsDocument);
writer.Write(paginator);
xpsDocument.Close();
package.Close();
Preview previewWindow = new Preview(lMemoryStream);
previewWindow.ShowDialog();
}
private void WindowCommandCommandAction()
{
XamlWindow xw = new XamlWindow();
xw.Show();
}
The Preview
window contains only one control:
<DocumentViewer Document="{Binding Document}"/>
And the PreviewVM
:
public class PreviewVM : ObservableObject
{
private IDocumentPaginatorSource myDocument;
private XpsDocument xps;
private Uri packageUri;
private Stream docStream;
public PreviewVM(MemoryStream xpsStreamDocument)
{
docStream = xpsStreamDocument;
Package package = Package.Open(docStream);
//Create URI for Xps Package
//Any Uri will actually be fine here. It acts as a place holder for the
//Uri of the package inside of the PackageStore
string inMemoryPackageName = string.Format("memorystream://{0}.xps", Guid.NewGuid());
packageUri = new Uri(inMemoryPackageName);
//Add package to PackageStore
PackageStore.AddPackage(packageUri, package);
xps = new XpsDocument(package, CompressionOption.SuperFast, inMemoryPackageName);
Document = xps.GetFixedDocumentSequence();
}
public IDocumentPaginatorSource Document
{
get { return myDocument; }
set
{
if (myDocument == value) return;
myDocument = value;
OnPropertyChanged(nameof(Document));
}
}
}
So, in the MainVM
the WindowCommandCommandAction
method open a new window and display the UserControl
XamlPage
without any surprise.
BUT the PreviewCommandAction
method open correctly the Preview
window and this preview display only the content of the TextBox
but not the Image
.
No error message, nothing in the Windows Event Viewer. I say again that this was working for years until two days ago.
By the way is there any other solution to get the preview? I need to use DocumentPaginator to manage pages.
The whole test project is on this repository:
https://github.com/yannicklal/TestPreview
CodePudding user response:
It's a Microsoft problem due to the update of december 13, 2022.
KB5022083 Change in how WPF-based applications render XPS documents