【问题标题】:Error Displaying Tiff Image to DocumentViewer (WPF,C#)向 DocumentViewer 显示 Tiff 图像时出错(WPF,C#)
【发布时间】:2019-03-08 22:31:33
【问题描述】:

大家好,我想用DocumentViewer控件来显示Multipage Tiffs。我写的代码如下...

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

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

提前致谢!!!

【问题讨论】:

  • 错误信息是简单的英文。从实际阅读错误消息开始。它非常能告诉你你的问题是什么(还指出了如何解决它......)
  • 感谢您的回复!!!我已经尝试了一切手段 ofc 我已经阅读了消息并且我已经搜索了整个谷歌以及放置断点以查看代码中断的位置但我还没有找到解决方案。所以如果你有一个与我们分享我真的很感激它:)
  • 我的建议是,在尝试加载/访问 TIFF 图像时,请确保您的代码使用正确的路径。您在问题中的代码示例使用占位符 "THE PATH TO MULTIPAGE TIF" 作为实际文件路径。只需在此处遵循您自己的建议:使用(正确!)多页 tif 的路径...
  • 我 100% 确定我输入了正确的路径,所以这不是问题,但感谢您的建议和帮助。如果您有其他建议,我将很高兴听到。我如果你想试试的话,已经上传了 .xaml 和 .cs 的所有代码 :)
  • 好吧,如果你真的 100% 确定你把正确的路径放在那里,那么看看异常的堆栈跟踪。它会告诉你哪个方法抛出了异常,它还会告诉你你自己代码的哪些方法参与了调用抛出异常的方法。有了这些信息,您应该能够以有意义的方式调试您的代码,以了解实际情况......

标签: c# wpf tiff multipage documentviewer


【解决方案1】:

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

我发现解决此问题的一种方法是将图像流放入 BitmapImage 中,然后再将其加载到文档查看器中。唯一的问题是我无法让它与多页 Tiff 一起使用:

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

使它与多页 tiff 一起工作的一种方法是一种 hack,您可以创建程序要求的文件。它需要是一个名为 Image 且没​​有文件扩展名的文件,它还需要是一个 Tiff 文件结构。它可以是任何 tiff,但为此我确实复制了我们在文档查看器中显示的 tiff。已有Image文件时,无需再复制。

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;

【讨论】:

  • 目前我没有话要感谢你。它确实有效,如果你相信它,我也尝试过,然后在这里发布错误,但我只是创建了一个 txt 文件,我将其重命名为 image没有扩展,它没有工作。有了你对 tiff 的建议,它就像一个魅力。你是一个生命的救星兄弟,我真的很感激它。非常感谢 :)
  • 对我有用的是使用将 Uri 作为解码器的构造函数。出于某种原因,仅当您使用流加载文件时才会出现此问题。我确定这是一个需要解决的 Microsoft 问题,但我什至不确定从哪里开始。
猜你喜欢
  • 1970-01-01
  • 2023-03-16
  • 2015-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多