1

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;
}
blowekamp
  • 1,401
  • 7
  • 7
user2827482
  • 199
  • 1
  • 2
  • 9
  • possible duplicate of [load a bitmap image into windows form using open file dialog!](http://stackoverflow.com/questions/6122984/load-a-bitmap-image-into-windows-form-using-open-file-dialog) – string.Empty Jun 04 '14 at 13:45

2 Answers2

3

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.

blowekamp
  • 1,401
  • 7
  • 7
  • How did you import the `Simpleitk` library? When I try to import the DLLs using `regsvr32`, getting an error like this: `SimpleITKCSharpNative.dll was loaded but the entry-point DllRegisterServer was not found` – talha06 May 22 '16 at 14:00
-2
using (OpenFileDialog fd = new OpenFileDialog())
{
    fd.Filter = "PNG|*.png";

    if (fd.ShowDialog() == DialogResult.OK)
    {
        Box.Image = new Bitmap(fd.FileName);
    }
}
string.Empty
  • 10,393
  • 4
  • 39
  • 67