Search code examples
c#system.drawingitksimpleitk

Display a Simpleitk image in a picture Box


I want to read a dicom or png image with simpleitk in a C# program and display the result in a pictureBox. I understand that picture Box allow only "system.drawing.image" and not itk. Is there a way to do it. Her is my code :

OpenFileDialog fd = new OpenFileDialog();
fd.Filter = "PNG|*.png";
if (fd.ShowDialog() == DialogResult.OK)
{
    string file = fd.FileName;
    ImageFileReader reader = new ImageFileReader();
    reader.SetFileName(file);
    itk.simple.Image image = reader.Execute();
     Box.Image = image;
}

Solution

  • You will need access to the raw image buffer which SimpleITK holds. This is accessible via the Image::GetBufferAs"TYPE" methods.

    Here is a brief example on using this method:

      // Cast so we know the the pixel type
      input = SimpleITK.Cast(input, PixelId.sitkFloat32);
      // calculate the nubmer of pixels
      VectorUInt32 size = input.GetSize();
      int len = 1;
      for (int dim = 0; dim < input.GetDimension(); dim++) {
        len *= (int)size[dim];
      }
      IntPtr buffer = input.GetBufferAsFloat();
    

    I believe this can then be converted into to a Bitmap with .Net.