2017-04-16 67 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