【问题标题】:Images not showing in FlowDocument图像未显示在 FlowDocument 中
【发布时间】:2017-04-16 20:17:55
【问题描述】:

我正在努力让图像出现在打印为 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 资源中定义的图像,图像就会出现。 如果我使用带有 pack uri 表示法的相对 uri,我会得到一个异常:“找不到图像”,尽管这个公式在 XAML 中可以正常工作。

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

【问题讨论】:

  • 您是否尝试过使用:new Uri("pack://application:,,,/Images/globe.png", UriKind.Absolute); ?....亲戚似乎不对。

标签: c# wpf image printing flowdocument


【解决方案1】:

您可以通过ResourceDirectonary 引用您的图片,这表明您的图片可以被定位。

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

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

事实上,通常不需要使用第二个UriKind 参数,就好像您的Uri 字符串属于“pack://”类型,那么它是相对还是绝对在定位器中进行编码。 .或者如果你的字符串有一个“/”前缀,这将意味着“绝对”,而其他任何东西通常都是相对的......如果你想使用“./”、“../”等,你可以更明显.

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

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

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

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

4 个图像,已创建并添加为:image1.bmp、image2.bmp、image3.bmp、image4.bmp,作为“项目”节点下的文件。构建操作设置为 4 个不同的值。

然后探索一些引用“图像”的不同方式。

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

【讨论】:

  • 这个答案对我有帮助,尽管对我来说只使用了 pack://application:,,,/。我正在动态地将图像添加到 Flowdocument。而且我仍然不太确定为什么其他方法对我不起作用。
【解决方案2】:

对于下面的表单,应用程序正在相对于当前目录的“Images”文件夹中查找图像,该目录不存在。 (如果您通过双击exe启动应用程序,则当前目录是exe所在的文件夹)

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

对于 Pack URI 表单

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-30
    • 1970-01-01
    • 2017-01-28
    • 2014-05-07
    • 1970-01-01
    • 2015-06-09
    • 2017-01-10
    • 2013-08-19
    相关资源
    最近更新 更多