WPF DocumentViewer doesn't display images

Yannick Lalbatry 21 Reputation points
2022-12-22T14:09:23.933+00:00

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>  

Git:
https://github.com/yannicklal/XpsTest

Developer technologies XAML
{count} votes

Accepted answer
  1. Castorix31 90,521 Reputation points
    2022-12-27T12:23:40.7+00:00
    1 person found this answer helpful.

5 additional answers

Sort by: Most helpful
  1. 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

    2 people found this answer helpful.
    0 comments No comments

  2. 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 :
    274135-image.png

    The result in .NET6(.NET5) :
    274103-image.png

    ----------------------------------------------------------------------------

    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.

    1 person found this answer helpful.
    0 comments No comments

  3. 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):

    274071-xps-test.gif


  4. 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.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.