2017-04-16 47 views
0

我很努力地让图像出现在打印成PDF,XPS或打印机的FlowDocument中。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符号的相对uri,我会得到一个异常:“image not found”,事件虽然这个公式在XAML中可以正常工作。

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

+0

你有没有尝试使用新的URI( “包://应用:,,, /图片/ globe.png”,UriKind.Absolute); ?....亲戚看起来不对。 –

回答

0

正如您能够通过您的ResourceDirectonary引用您的图像,表明您的图像能够被找到。

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

看着这条线,我认为你错误地使用了UriKind.Relative而不是UriKind.Absolute

事实上,它通常不需要使用第二UriKind参数,因为如果你的Uri字符串是的“包://”的品种,那么无论是相对或绝对的定位器编码...或者,如果你的字符串有一个“/”前缀,这意味着“绝对”,而其他任何东西通常都是相对的......如果你想通过使用“./”,“../”等,你可以更明显。

(除非你让它解释,否则,这是你似乎做了...这就是为什么它不工作)。

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

上使用辅助 “包://” URI来引用图像...我想出了一个矩阵来展示一些不同的组合,以防你遇到了一个陷阱。

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

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

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

enter image description here

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

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

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

对于包URI形式

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

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