2013-07-23 30 views
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文件。我會很感激任何信息。我非常感謝你

+0

你xaml和cs文件應該是一個類的實現。你的CS文件是部分實施MainWindow類 XAML文件(示出了類)(MainWindow.xaml) <窗口x:類= 「WindowGrid.MainWindow」 ...進一步東西 CS文件(主窗口.xaml.cs) public partial class MainWindow:Window – whoisthis

回答

0

發佈作爲回答,以顯示格式(兩個文件之間)

你的XAML和CS文件應該是對執行一類。你的CS文件是部分實現MainWindow類的

XAML文件(顯示類)(MainWindow.xaml)

<Window x:Class="WindowGrid.MainWindow" 
... further things 

CS文件(MainWindow.xaml.cs)

public partial class MainWindow : Window 
相關問題