0
我一直在C#中处理在C#中使用ClearCanvas打开dicom图像的这个项目,我有这种无法修复的重复错误。我的代码如下关于在C#中使用Xaml创建图像控件#
enter
private void pictureBox1_Click(object sender, EventArgs e)
{
string filename = @"C:\Users\Don jar\Pictures\Xray pics\fluro.dcm";
DicomFile dicomFile = new DicomFile(filename);
dicomFile.Load(DicomReadOptions.Default);
foreach (DicomAttribute attribute in dicomFile.DataSet)
{
Console.WriteLine("Tag: {0}, Value: {1}", attribute.Tag.Name, attribute.ToString());
}
int bitsPerPixel = dicomFile.DataSet.GetAttribute(DicomTags.BitsStored).GetInt32(0, 0);
int width = dicomFile.DataSet.GetAttribute(DicomTags.Columns).GetInt32(0, 0);
int height = dicomFile.DataSet.GetAttribute(DicomTags.Rows).GetInt32(0, 0);
int stride = width * 2;
byte[] bitmapBuffer = (byte[])dicomFile.DataSet.GetAttribute(DicomTags.PixelData).Values;
BitmapSource bitmapSource = BitmapImage.Create(width, height, 96, 96, System.Windows.Media.PixelFormats.Gray16, null, bitmapBuffer, stride);
image1.Source = bitmapSource;
}code here
我就上线收到错误
image1.Source = bitmapSource;
的错误指出
错误1名“图像1”不存在当前存在上下文。
但是,在做了一些研究之后,我读到必须在页面的XAML中创建一个图像控件,我将在其上显示Dicom图像。因此,它应该是这种格式
enter <Window x:Class="WpfImageTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Image Name="image1" />
</Grid>code
</Window>
与后面的代码
enter public MainWindow()
{
InitializeComponent();
// Create source
BitmapImage myBitmapImage = new BitmapImage();
// BitmapImage.UriSource must be in a BeginInit/EndInit block
myBitmapImage.BeginInit();
myBitmapImage.UriSource = new Uri(@"E:\Pictures\avatar.jpg");
myBitmapImage.DecodePixelWidth = 200;
myBitmapImage.EndInit();
//set image source
image1.Source = myBitmapImage;
}code here
我的问题是我怎么会在我的项目纳入这一点,我已经添加XAML作为参考,现在我的无能如何进一步发展。我是否将此代码合并到我的cs文件中或构建完整的cs文件。我会很感激任何信息。我非常感谢你
你xaml和cs文件应该是一个类的实现。你的CS文件是部分实施MainWindow类 XAML文件(示出了类)(MainWindow.xaml) <窗口x:类= “WindowGrid.MainWindow” ...进一步东西 CS文件(主窗口.xaml.cs) public partial class MainWindow:Window – whoisthis