关于在c中使用Xaml创建图像控件#

关于在c中使用Xaml创建图像控件#,xaml,c#-4.0,image-processing,dicom,Xaml,C# 4.0,Image Processing,Dicom,我一直在用C#处理这个项目,用ClearCanvas在C#中打开dicom图像,我有一个反复出现的错误,我无法修复。我的代码如下 enter private void pictureBox1_Click(object sender, EventArgs e) { string filename = @"C:\Users\Don jar\Pictures\Xray pics\fluro.dcm"; DicomFile dicomFile = ne

我一直在用C#处理这个项目,用ClearCanvas在C#中打开dicom图像,我有一个反复出现的错误,我无法修复。我的代码如下

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当前上下文中不存在名称“image1”

然而,在做了一些研究之后,我读到我必须创建一个在页面的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> 

我的问题是如何将其纳入我的项目中,我已经添加了XAML作为参考,现在我不知道如何进一步进行。我是将此代码合并到我的cs文件中还是构建一个完整的cs文件。如能提供任何信息,我将不胜感激。非常感谢

作为答案发布,以显示格式(在两个文件之间)


您的xaml和cs文件应该是一个类的实现。您的cs文件是MainWindow类的部分实现

XAML文件(显示类)(MainWindow.XAML)


您的xaml和cs文件应该是一个类的实现。您的cs文件是MainWindow类XAML文件(显示类)(MainWindow.XAML)的部分实现
    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
<Window x:Class="WindowGrid.MainWindow"
... further things
public partial class MainWindow : Window