简体   繁体   中英

Error Displaying Tiff Image to DocumentViewer (WPF,C#)

Hello Everyone I want to use DocumentViewer control to display Multipage Tiffs.The code i have written is the following...

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>

I have tried to take and display only one frame-page from the TIFF.However when i run the program it does show the page i want BUT when i move the cursor of the mouse inside the Document Viewer and specifically inside the image i keep getting the following error:

"System.IO.FileNotFoundException: 'Could not find file 'C:\\Users\\ttsa\\Desktop\\TIFF_Viewer\\Tiff_Viewer\\Tiff_Viewer\\bin\\Debug\\image'.'".

在此输入图像描述

I have tried almost everything and i cant find a way to fix this. Does anyone knows anything about this? Is there something i can do to fix it? Or else does anyone knows another way to display multipage tiff to DocumentViewer???

Thanks in advance!!!

I did make a copy of your code and you are not the only one with the exception. I don't know why the documentviewer trows the 'File not found exception', if someone can explain this it would be appreciated.

One way I have found to solve this is to put the image stream inside a BitmapImage before loading it in the document viewer. The only problem is that I can't get this to work with a multipage Tiff:

BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = ImageStream;
bi.EndInit();
this._Image.Source = bi;

One way to make it work with a multipage tiff is kind of a hack, you could just create the file the program is asking for. It needs to be a file named Image witout a file extension, also it needs to be a Tiff file structure. It can be any tiff but for this I did make a copy of the tiff we show in the documentviewer. When there is already a Image file, there is no need to copy it again.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM