There is a KB : KB5022083 Change in how WPF-based applications render XPS documents
WPF DocumentViewer doesn't display images
I have an XPS file, originaly created from WPF with 2 controls, one TextBox and one Image.
I can open the XPS file in the Windows Viewer without problem.
When running this code, the image is not displayed, what is wrong?
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var xps = new XpsDocument("printPreview.xps", FileAccess.Read);
viewer.Document = xps.GetFixedDocumentSequence();
xps.Close();
}
}
<Window x:Class="XpsTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:XpsTest"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<DocumentViewer Name="viewer"/>
</Grid>
</Window>
Developer technologies XAML
5 additional answers
Sort by: Most helpful
-
Jasiel Tellez 11 Reputation points
2022-12-27T12:13:44.61+00:00 reg add "HKLM\SOFTWARE\Microsoft.NETFramework\Windows Presentation Foundation\XPSAllowedTypes" /v "DisableDec2022Patch" /t REG_SZ /d "*" /reg:64
This is de Solution for me
-
Hui Liu-MSFT 48,676 Reputation points Microsoft External Staff
2022-12-27T03:24:39.473+00:00 I used the exact same code to test in .NetFramework 4.8 and in .NET6 respectively. It doesn't show pictures in .NetFramework 4.8 and shows pictures in .NET6( .NET5).
Xaml:
<Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"></ColumnDefinition> <ColumnDefinition Width="*"></ColumnDefinition> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="40"></RowDefinition> <RowDefinition Height="*"></RowDefinition> </Grid.RowDefinitions> <Button Grid.Row="0" Grid.Column="1" HorizontalAlignment="Center" Width="80" Click="Button_Click_1">save as xps</Button> <FlowDocumentReader Height="150" Width="170" Grid.Column="1" Grid.Row="1"> <FlowDocument Name="flowDocument" ColumnWidth="400" FontSize="14" FontFamily="Georgia"> <Paragraph> <Grid> <Image Source="C:\Users\Admin\Downloads\WpfApp3\45.jpg" Width="200" Height="300" /> <TextBlock Text="Hello World!" HorizontalAlignment="Center" VerticalAlignment="Top" Foreground="Red" /> </Grid> </Paragraph> </FlowDocument> </FlowDocumentReader> <Button Grid.Row="0" HorizontalAlignment="Center" Width="80" Click="Button_Click">open xps file</Button> <DocumentViewer Name="viewer" Grid.Row="1"/> </Grid>
Codebehind:
274176-xps.txt
The result in .NetFramework 4.8 :
The result in .NET6(.NET5) :
----------------------------------------------------------------------------
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread. -
Castorix31 90,521 Reputation points
2022-12-26T10:58:43.847+00:00 Your printPreview.xps file is detected as a virus on my OS (Windows 10 21H1)
But if I force the download and open it in another test project, it works normally with the same code (on Button click to test): -
Jasiel Tellez 11 Reputation points
2022-12-26T20:17:06.007+00:00 Our software is used to print boreholes in the large mining industry. Today our customers started to ask us about printing error because the images are not displayed. I think something changed in windows and it affects how the XPS Document viewer works.
My OS is W10Pro 21H2.