大家好我想用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;