Search code examples
c#bitmapdicomfo-dicom

FO-DICOM: Resizing a window with a rendered bitmap in a C# Windows Forms application results in crash


I have recently started to evaluate fo-dicom as a possible DICOM library for a future project, so I am quite new with it.

I built a basic C# Windows Forms application that only reads a DICOM file, converts it into a System.Drawing.Bitmap and displays in a PictureBox:

public partial class TestFoDicomForm : Form
{
    public TestFoDicomForm()
    {
        InitializeComponent();

        DicomImage di               = new DicomImage("Image_01.dcm");
        Bitmap bmp                  = di.RenderImage().AsBitmap();
        this._pbDicomImage.Image    = bmp;
    }
}

This code functions, but if I start to resize the form, an exception comes more earlier than later telling:

System.ArgumentException: Parameter is not valid.

at System.Drawing.Image.get_RawFormat()
at System.Drawing.Graphics.DrawImage(Image image, Int32 x, Int32 y, Int32 width, Int32 height)
at System.Drawing.Graphics.DrawImage(Image image, Rectangle rect)
at System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)
at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

The exception occurs actually in Main()

Application.Run(new TestFoDicomForm());

but I was unable to add a functioning try/catch to investigate what effectively happens.

I added the reference to fo-dicom 3.0.2 via NuGet (the project's target framework being 4.6.1). Environment: Windows 10 Pro, VS 2017.

An interesting thing is that if I produce the bitmap as shown in the code above, then store it, and in the application read it (with no reference to DICOM) and place into the picture box, nothing similar happens. This makes me to think that the problem lies in the bitmap itself, but I was unable to discover, what.

I also have an old test application made with fo-dicom.1.0.37 which does not crash when being resized.

I am very curious what the reason may be, how to get rid of this effect or/and what is it I am possibly doing wrong.

(The test application can be downloaded - I hope - from http://jmp.sh/UGOg8Ai).


Solution

  • A colleague of mine knew the answer. The following code does the thing:

    public partial class TestFoDicomForm : Form
    {
        private IImage image;
    
        public TestFoDicomForm()
        {
            InitializeComponent();
    
            this.image = new DicomImage("Image_01.dcm").RenderImage();
            Bitmap bmp = image.AsBitmap();
            this.pictureBox1.Image  = bmp;
        }
    }
    

    The trick here is that you need to save the instance of your IImage (necessarily in this form, as IImage, due to the return type of RenderImage()).