3

I want to read a dicom image using simpleitk, convert it into a bitmap and then display the result in a pictureBox. But when I'm trying to do this, an ArgumentException is thrown. How can I solve this?

Here is my code:

OpenFileDialog dialog = new OpenFileDialog();
dialog.Title = "Open";
dialog.Filter = "DICOM Files (*.dcm;*.dic)|*.dcm;*.dic|All Files (*.*)|*.*";
dialog.ShowDialog();
if (dialog.FileName != "")
{
    using (sitk.ImageFileReader reader = new sitk.ImageFileReader())
    {
        reader.SetFileName(dialog.FileName);                   
        reader.SetOutputPixelType(sitk.PixelIDValueEnum.sitkFloat32);
        sitk.Image image = reader.Execute();

        var castedImage = sitk.SimpleITK.Cast(image, 
            sitk.PixelIDValueEnum.sitkFloat32);
        var size = castedImage.GetSize();
        int length = size.Aggregate(1, (current, i) => current * (int)i);
        IntPtr buffer = castedImage.GetBufferAsFloat();

        // Declare an array to hold the bytes of the bitmap.                    
        byte[] rgbValues = new byte[length];

        // Copy the RGB values into the array.
        Marshal.Copy(buffer, rgbValues, 0, length);

        Stream stream = new MemoryStream(rgbValues);

        Bitmap newBitmap = new Bitmap(stream);

        //I have tried in this way, but it generated ArgumentException too
        //Bitmap newBitmap = new Bitmap((int)image.GetWidth(), (int)image.GetHeight(), (int)image.GetDepth(), PixelFormat.Format8bppIndexed, buffer);

        Obraz.pic.Image = newBitmap;
    }
}
AudioDroid
  • 2,292
  • 2
  • 20
  • 31
PAPP
  • 57
  • 10
  • you should be checking your OpenFileDialog like this `if (dialog.ShowDialog() == DialogResult.OK){ }` also what line does the error happen on.. have you use the debugger..? have you googled about any BitMap Converter functions...etc..? – MethodMan Jan 05 '16 at 23:24
  • Thank you for your answer. You're right, I should check my OpenFileDialog like you said. The error occurs in the line: New Bitmap bitmap = new Bitmap (stream); I googled about how to create bitmap from stream or buffer, and how to convert SimpleITK.Image to Image or Bitmap. – PAPP Jan 05 '16 at 23:38
  • Have you checked [exception condition](https://msdn.microsoft.com/en-us/library/z7ha67kw(v=vs.110).aspx): stream does not contain image data or is null. -or- stream contains a PNG image file with a single dimension greater than 65,535 pixels? – Jacob Seleznev Jan 06 '16 at 01:45
  • 1
    try this example [ImageGetBuffer.cs](http://www.itk.org/SimpleITKDoxygen09/html/ImageGetBuffer_8cs-example.html). Note use of `float[]` instead of `byte[]` – Jacob Seleznev Jan 06 '16 at 02:00

1 Answers1

1

Thank you for your comments and attempts to help. After consultations and my own searching on the internet I solved this issue. The first problem was the inadequate representation of pixel image. I had to change Float32 to UInt8 to provide an eight-bit for pixel.

var castedImage = sitk.SimpleITK.Cast(image2, sitk.PixelIDValueEnum.sitkUInt8);

Then I would already create a Bitmap using the constructor that was was commented out in question, but with (int)image.GetWidth() instead of (int)image.GetDepth().

Bitmap newBitmap = new Bitmap((int)image.GetWidth(), (int)image.GetHeight(), (int)image.GetWidth(), PixelFormat.Format8bppIndexed, buffer);

Unfortunately, a new problem appeared. The image, that was supposed to be in gray scale, was displayed in strange colors. But I found the solution here

ColorPalette pal = newBitmap.Palette;
                for (int i = 0; i <= 255; i++)
                {
                    // create greyscale color table
                    pal.Entries[i] = Color.FromArgb(i, i, i);
                }
                newBitmap.Palette = pal; // you need to re-set this property to force the new ColorPalette
Community
  • 1
  • 1
PAPP
  • 57
  • 10
  • 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