3

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'.'".

enter image description here

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!!!

  • The error message is in plain simple English. Start with actually reading the error message. It very much tells you what your problem is (and also points at how you can fix it...) –  Mar 08 '19 at 11:33
  • Thanks for your response!!! I have tried everything means ofc i have read the message and ofc i have searched the whole google as well as putting breakpoints to see where the code breaks but i havent find a solution.So if you have one share it with us i would really appreciate it :) – Fanis Tsatsaronis Mar 08 '19 at 11:50
  • My advice is that you make sure your code is using the correct path when trying to load/access the TIFF image. Your code example in the question uses the placeholder `"THE PATH TO MULTIPAGE TIF"` for the actual file path. Just follow your own suggestion there: use the (correct!) path to the multipage tif... –  Mar 08 '19 at 11:53
  • I am 100% sure that i put the correct path so this is not the problem but thanks for your advice and for your help.If you have anything else to suggest i will be glad to hear.I have uploaded all the code both .xaml and .cs if you want to try it :) – Fanis Tsatsaronis Mar 08 '19 at 12:06
  • Well, if you are really 100% sure that you put the correct path there, then look at the stacktrace of the exception. It will tell you which method threw the exception, and it will also tell you which methods of your own code were involved in calling the method that threw the exception. With that information you should be able to debug your code in a meaningful manner to see what is really going on... –  Mar 08 '19 at 12:10
  • An unhandled exception of type 'System.IO.FileNotFoundException' occurred in PresentationCore.dll Could not find file 'C:\Users\ttsa\Desktop\TIFF_Viewer\Tiff_Viewer\Tiff_Viewer\bin\Debug\image'. The program '[416] Tiff_Viewer.exe' has exited with code -1 (0xffffffff). – Fanis Tsatsaronis Mar 08 '19 at 12:19
  • The problem is i dont use anywhere in my code an image from this path.Its like it creates it of its own like a temp file i dont know really. – Fanis Tsatsaronis Mar 08 '19 at 12:21

1 Answers1

1

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;
DjangoSoe
  • 46
  • 3
  • I have no words to thank you at the moment.It actually worked and if you believe it i tried it too before i post the error here but i just created a txt file that i renamed it to image without the extension and it didnt work.With your suggestion for tiff it worked like a charm.You are a life savior bro i really appreciate it.Thank you sooo much :) – Fanis Tsatsaronis Mar 08 '19 at 15:34
  • Something that worked for me was to use the constructor that takes a Uri for the decoder. For some reason, this issue only occurs when you're using a stream to load the file. I'm sure this is a Microsoft issue that needs to be resolved, but I'm not even sure where I would start. – Chris Altig Sep 14 '20 at 17:56