0

I have been working in displaying a DICOM image in C# with ClearCanvas, the developer Steve aided me in reading in the tags for the image. However, I have been stuck for almost two weeks working on this project of and on trying to display the image. I have tried and added code to what Steve helped with and yet I am unable to display the DICOM image. Maybe what i'm doing is incorrect, can someone take a look at the code and hopefully tell me what is wrong or is their an alternative way to display it. I have been trying to get this project to work in C# with hopefully being able to convert to Visual basic. I am obtaining the same error over again. The code is posted below.

enter
        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);

        pictureBox1.Image = bitmapSource; code here

The error being displayed is on the last line pictureBox1.Image = bitmapSource; and the the error is

Error 1 'System.Windows.Forms.PictureBox' does not contain a definition for 'Source' and no extension method 'Source' accepting a first argument of type 'System.Windows.Forms.PictureBox' could be found (are you missing a using directive or an assembly reference?) C:\Users\Don jar\Documents\Visual Studio 2010\Projects\DICOMCA\DICOMCA\Form1.c

The second error is Error 2 Cannot implicitly convert type 'System.Windows.Media.Imaging.BitmapSource' to 'System.Drawing.Image' C:\Users\Don jar\Documents\Visual Studio 2010\Projects\DICOMCA\DICOMCA\Form1.cs 62 33 DICOMCA

dirty_sanchez
  • 69
  • 1
  • 1
  • 12

1 Answers1

0

BitmapSource is, as the error says, a System.Windows.Media.Imaging.BitmapSource image, i.e. it's a WPF image.
Your pictureBox1 object is a System.Windows.Forms.PictureBox object.
Those 2 things are incompatible, you cannot display a WPF image in a Windows Forms PictureBox.

You'll need to convert the BitmapSource to a regular System.Drawing.Bitmap in order to get it to display, but luckily there are multiple ways to do this, although the best option kind of depends on the pixel format of the image.
How to convert BitmapSource to Bitmap
Is there a good way to convert between BitmapSource and Bitmap?

The answer on that second link is the faster way to do it though.

Community
  • 1
  • 1
Nanhydrin
  • 4,332
  • 2
  • 38
  • 51