图像未显示在 FlowDocument 中

Images not showing in FlowDocument

我正在努力让图像出现在打印为 PDF、XPS 或打印机的 FlowDocument 中。

我研究过这个问题,Missing images in FlowDocument saved as XPS document,但发现答案不令人满意。

这是我的代码...

        PrintDialog pd = new PrintDialog();
        if(pd.ShowDialog() == true)
        {
            FlowDocument fd = new FlowDocument();
            fd.Blocks.Add(new Paragraph(new Run("Line 1")));
            Uri uri = new Uri("Images/globe.png", UriKind.Relative);
//              Uri uri = new Uri(@"C:\...\Images\globe.png", UriKind.Absolute);
//              Uri uri = new Uri("pack://application:,,,/Images/globe.png", UriKind.Relative);
            BitmapImage bi = new BitmapImage(uri);
            Image i = new Image();
            i.Height = 20;
            i.Width = 20;
            i.Source = bi;
//                Image i = this.Resources["globeImage"] as Image;
            fd.Blocks.Add(new BlockUIContainer(i));
            fd.Blocks.Add(new Paragraph(new Run("Line 2")));
            pd.PrintDocument((fd as IDocumentPaginatorSource).DocumentPaginator, "A print document");
        }

此外,我已经定义了这个资源...

    <Image x:Key="globeImage" Source="Images/globe.png" Height="20" Width="20"/>

因此,显示的代码将不起作用。图像在打印文档中的位置是空白的。

这里是有趣的地方...

如果我使用绝对uri,图像就会出现。 如果我使用 windows 资源中定义的图像,图像就会出现。 如果我将相对 uri 与 pack uri 表示法一起使用,我会得到一个异常:"image not found",尽管此公式在 XAML.

中可以正常工作

那么这是怎么回事?根据我引用的问题,问题是图像直到显示在屏幕上才加载。如果这是真的,那么为什么绝对 URI 路径有效?图片源在 XAML 中的工作方式与编程方式有何不同。

对于下面的形式,应用程序正在相对于当前目录的 "Images" 文件夹中查找图像,该文件夹不存在。 (如果是双击exe启动应用程序,当前目录就是exe所在的文件夹)

new Uri("Images/globe.png", UriKind.Relative);

对于 Pack URI 形式

pack://application:,,,/Images/globe.png

这是绝对的,不是相对的。请参考this

由于您能够通过 ResourceDirectonary 引用您的图像,这表明您的图像能够被找到。

将假设您使用 BuildAction="Resource" 将图像添加到项目中。

查看这一行,我认为您错误地使用了 UriKind.Relative 而不是 UriKind.Absolute

其实第二个UriKind参数通常是没有必要的,就好像你的Uri字符串是"pack://"变种,那么是相对还是绝对编码在定位器中......或者如果你的字符串有一个“/”前缀,这将意味着 "absolute" 而其他任何东西通常都是相对的......如果你想使用“./”,你可以更明显, “../”等

(除非你告诉它以其他方式解释,这就是你似乎所做的......这就是它不起作用的原因)。

//              Uri uri = new Uri("pack://application:,,,/Images/globe.png", UriKind.Relative);

作为使用 "pack://" uris 引用图像的帮手...我想出了一个矩阵来显示一些不同的组合,以防您遇到其中一个问题。

这显示了引用图像的一些不同组合 "resource",具体取决于您向应用程序提供该资源的方式以及您引用它的方式(并非所有选项)。

4 张图片,已创建并添加为:image1.bmp、image2.bmp、image3.bmp、image4.bmp,作为直接位于 "Project" 节点下的文件.生成操作设置为 4 个不同的值。

然后探讨了引用 "images" 的一些不同方式。

<Window x:Class="WpfApplication4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="600" Width="1200">
    <Window.Resources>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Margin" Value="4"/>
            <Setter Property="FontSize"  Value="14"/>
        </Style>
        <BitmapImage x:Key="bitmap1" UriSource="Image1.bmp"/>
        <BitmapImage x:Key="bitmap2" UriSource="Image2.bmp"/>
        <BitmapImage x:Key="bitmap3" UriSource="Image3.bmp"/>
        <BitmapImage x:Key="bitmap4" UriSource="Image4.bmp"/>
        <Image x:Key="image1" Source="Image1.bmp"/>
        <Image x:Key="image2" Source="Image2.bmp"/>
        <Image x:Key="image3" Source="Image3.bmp"/>
        <Image x:Key="image4" Source="Image4.bmp"/>
    </Window.Resources>
    <Grid ShowGridLines="True">
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <TextBlock Background="LightYellow"  Grid.Column="0" Grid.Row="1">BuildAction=<LineBreak/>"Resource"</TextBlock>
        <TextBlock Background="LightYellow" Grid.Column="0" Grid.Row="2">BuildAction=<LineBreak/>"Embedded Resource"</TextBlock>
        <TextBlock Background="LightYellow" Grid.Column="0" Grid.Row="3">BuildAction=<LineBreak/>"Content"</TextBlock>
        <TextBlock Background="LightYellow" Grid.Column="0" Grid.Row="4">BuildAction=<LineBreak/>"Content (copied to output)"</TextBlock>
        <TextBlock Background="PeachPuff"  Grid.Column="1" Grid.Row="0">pack://application:,,,/</TextBlock>
        <TextBlock Background="PeachPuff" Grid.Column="2" Grid.Row="0">pack://application:,,,/WpfApplication4;component/</TextBlock>
        <TextBlock Background="PeachPuff" Grid.Column="3" Grid.Row="0">pack://siteoforigin:,,,/</TextBlock>
        <TextBlock Background="PeachPuff" Grid.Column="4" Grid.Row="0">Image<LineBreak/>referencing BitmapImage<LineBreak/>via {StaticResource}<LineBreak/>referencing "Resource"</TextBlock>
        <TextBlock Background="PeachPuff" Grid.Column="5" Grid.Row="0">ContentPresenter<LineBreak/>referencing Image<LineBreak/>via {StaticResource}<LineBreak/>referencing "Resource"</TextBlock>
        <Image Grid.Column="1" Grid.Row="1" Source="pack://application:,,,/Image1.bmp"/>
        <Image Grid.Column="1" Grid.Row="2" Source="pack://application:,,,/Image2.bmp"/>
        <Image Grid.Column="1" Grid.Row="3" Source="pack://application:,,,/Image3.bmp"/>
        <Image Grid.Column="1" Grid.Row="4" Source="pack://application:,,,/Image4.bmp"/>
        <Image Grid.Column="2" Grid.Row="1" Source="pack://application:,,,/WpfApplication4;component/Image1.bmp"/>
        <Image Grid.Column="2" Grid.Row="2" Source="pack://application:,,,/WpfApplication4;component/Image2.bmp"/>
        <Image Grid.Column="2" Grid.Row="3" Source="pack://application:,,,/WpfApplication4;component/Image3.bmp"/>
        <Image Grid.Column="2" Grid.Row="4" Source="pack://application:,,,/WpfApplication4;component/Image4.bmp"/>
        <Image Grid.Column="3" Grid.Row="1" Source="pack://siteoforigin:,,,/Image1.bmp"/>
        <Image Grid.Column="3" Grid.Row="2" Source="pack://siteoforigin:,,,/Image2.bmp"/>
        <Image Grid.Column="3" Grid.Row="3" Source="pack://siteoforigin:,,,/Image3.bmp"/>
        <Image Grid.Column="3" Grid.Row="4" Source="pack://siteoforigin:,,,/Image4.bmp"/>
        <Image Grid.Column="4" Grid.Row="1" Source="{StaticResource bitmap1}"/>
        <Image Grid.Column="4" Grid.Row="2" Source="{StaticResource bitmap2}"/>
        <Image Grid.Column="4" Grid.Row="3" Source="{StaticResource bitmap3}"/>
        <Image Grid.Column="4" Grid.Row="4" Source="{StaticResource bitmap4}"/>
        <ContentPresenter Grid.Column="5" Grid.Row="1" Content="{StaticResource image1}"/>
        <ContentPresenter Grid.Column="5" Grid.Row="2" Content="{StaticResource image2}"/>
        <ContentPresenter Grid.Column="5" Grid.Row="3" Content="{StaticResource image3}"/>
        <ContentPresenter Grid.Column="5" Grid.Row="4" Content="{StaticResource image4}"/>
    </Grid>
</Window>