[英]Error Displaying Tiff Image to DocumentViewer (WPF,C#)
大家好我想用DocumentViewer控件來顯示Multipage Tiff。我寫的代碼如下......
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Tiff_Viewer
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private System.Windows.Controls.Image _Image;
private System.Windows.Documents.FixedDocument _FixedDocument;
private System.Windows.Documents.FixedPage _FixedPage;
private System.Windows.Documents.PageContent _PageContent;
public MainWindow()
{
InitializeComponent();
}
private void testbutton_Click(object sender, RoutedEventArgs e)
{
try
{
this._Image = new Image();
FileStream ImageStream = new FileStream("C:\\Users\\ttsa\\Desktop\\DocumentViewerTest\\002544-20180907.tif", FileMode.Open, FileAccess.Read, FileShare.Read);
TiffBitmapDecoder ImageDecoder = new TiffBitmapDecoder(ImageStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
//BitmapSource bitmapSource = ImageDecoder.Frames[0];
this._Image.Source = ImageDecoder.Frames[0];
//this._Image.Source = new BitmapImage(new Uri("C:\\Users\\ttsa\\Desktop\\DocumentViewerTest\\AAA0011A.tif", UriKind.Relative));
this._Image.Stretch = Stretch.None;
this._Image.Margin = new Thickness(20);
this._FixedPage = new System.Windows.Documents.FixedPage();
this._FixedPage.Width = 1000;
this._FixedPage.Height = 1000;
this._FixedPage.Children.Add(this._Image);
this._PageContent = new System.Windows.Documents.PageContent();
this._PageContent.Child = this._FixedPage;
this._FixedDocument = new FixedDocument();
this._FixedDocument.Pages.Add(this._PageContent);
DocumentViewer.Document = this._FixedDocument;
//DocumentViewer.LastPage();
}
catch (Exception fd)
{
System.Windows.MessageBox.Show(fd.Message);
}
}
}
}
------------------------------- WPF ------------------ -------------------------------------------------- -------
<Window x:Class="Tiff_Viewer.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:Tiff_Viewer"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="4*"/>
</Grid.RowDefinitions>
<DocumentViewer Grid.Row="1" x:Name="DocumentViewer" HorizontalAlignment="Left" Margin="0,41,0,0" VerticalAlignment="Top"/>
<Button HorizontalAlignment="left" Content="TEST" Name="testbutton" Grid.Row="0" Width="30" Click="testbutton_Click"/>
</Grid>
我試圖從TIFF中僅顯示一個幀頁面。但是當我運行程序時它確實顯示了我想要的頁面當我在文檔查看器中移動鼠標光標時,特別是在圖像中我不斷獲取以下錯誤:
“System.IO.FileNotFoundException:'找不到文件'C:\\ Users \\ ttsa \\ Desktop \\ TIFF_Viewer \\ Tiff_Viewer \\ Tiff_Viewer \\ bin \\ Debug \\ image'。'”。
我已經嘗試了幾乎所有的東西,我無法找到解決這個問題的方法。 有誰知道這件事? 有什么我可以做的來解決它嗎? 或者有沒有人知道另一種顯示多頁tiff到DocumentViewer的方法???
提前致謝!!!
我確實復制了你的代碼,你並不是唯一一個有例外的人。 我不知道為什么文檔查看器會出現“文件未找到異常”,如果有人可以解釋這一點,我們將不勝感激。
我發現解決此問題的一種方法是在將圖像流加載到文檔查看器之前將其放入BitmapImage中。 唯一的問題是我無法使用多頁Tiff工作:
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = ImageStream;
bi.EndInit();
this._Image.Source = bi;
使用多頁tiff工作的一種方法是黑客,你可以創建程序要求的文件。 它需要是一個名為Image的文件,沒有文件擴展名,它也需要是一個Tiff文件結構。 它可以是任何tiff,但為此我確實復制了我們在documentviewer中顯示的tiff。 當已有圖像文件時,無需再次復制。
string pathToTiff = @"C: \Users\developer\Desktop\temp\test.tif";
this._Image = new Image();
FileStream ImageStream = new FileStream(pathToTiff, FileMode.Open, FileAccess.Read,
FileShare.Read);
TiffBitmapDecoder ImageDecoder = new TiffBitmapDecoder(ImageStream,
BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
this._FixedDocument = new FixedDocument();
if (!File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"\Image"))
{
File.Copy(pathToTiff, AppDomain.CurrentDomain.BaseDirectory + @"\Image", true);
}
foreach (BitmapFrame f in ImageDecoder.Frames)
{
this._Image = new Image();
this._Image.Source = f.Clone(); ;
this._Image.Stretch = Stretch.None;
this._Image.Margin = new Thickness(20);
this._FixedPage = new System.Windows.Documents.FixedPage();
this._FixedPage.Width = 1000;
this._FixedPage.Height = 1000;
this._FixedPage.Children.Add(this._Image);
this._PageContent = new System.Windows.Documents.PageContent();
this._PageContent.Child = this._FixedPage;
this._FixedDocument.Pages.Add(this._PageContent);
}
documentViewer.Document = this._FixedDocument;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.