将Tiff图像显示到DocumentViewer(WPF,C#)时出错

时间:2019-03-08 11:18:58

标签: c# wpf tiff multipage documentviewer

大家好,我想使用DocumentViewer控件显示多页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'。”。

enter image description here

我几乎尝试了所有方法,但找不到解决此问题的方法。 有谁知道这件事吗?有什么我可以解决的吗? 否则有人知道另一种向DocumentViewer显示多页tiff的方法吗?

提前谢谢!

1 个答案:

答案 0 :(得分:1)

我确实复制了您的代码,但您不是唯一的例外。 我不知道为什么文档查看器会引发“找不到文件异常”,如果有人可以解释这一点,将不胜感激。

我发现解决此问题的一种方法是将图像流放在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;