【发布时间】: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