2

我最近开始评估 fo-dicom 作为未来项目可能的 DICOM 库,所以我对它很陌生。

我构建了一个基本的 C# Windows Forms 应用程序,它只读取一个 DICOM 文件,将其转换为 aSystem.Drawing.Bitmap并显示在 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;
    }
}

这段代码起作用,但如果我开始调整表单大小,异常来得比以后更早:

System.ArgumentException:参数无效。

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

异常实际上发生在Main()

Application.Run(new TestFoDicomForm());

但我无法添加一个功能try/catch来调查有效发生的事情。

我通过 NuGet 添加了对 fo-dicom 3.0.2 的引用(项目的目标框架是 4.6.1)。环境:Windows 10 专业版,VS 2017。

有趣的是,如果我生成如上面代码所示的位图,然后将其存储,并在应用程序中读取它(不参考 DICOM)并放入图片框中,则不会发生类似情况。这使我认为问题出在位图本身,但我无法发现,是什么。

我还有一个用 fo-dicom.1.0.37 制作的旧测试应用程序,它在调整大小时不会崩溃。

我很好奇可能是什么原因,如何摆脱这种影响或/以及我可能做错了什么。

(测试应用程序可以从http://jmp.sh/UGOg8Ai下载——我希望如此)。

4

2 回答 2

3

我的一个同事知道答案。下面的代码做的事情:

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;
    }
}

这里的诀窍是您需要保存您的实例(由于返回类型IImage为 ,因此必须以这种形式保存)。IImageRenderImage()

于 2018-02-06T08:51:09.043 回答
3

这是 fo-dicom 中的一个已知问题,并且已经有一个修复程序,将包含在下一个版本中。解释是,AsBitmap() 方法返回一个位图,其像素数据指向 IImage 实例拥有的内存。如果 IImage 实例释放,则 Bitmap 的指针无效。这对于性能原因和内存消耗非常有用,因为不必复制像素数据。因此,这不是错误,而是设计的。

新版本将有两种方法:一种 das 的行为与当前一样以具有最佳性能,另一种返回 Bitmap 并复制了自己的像素数据。

如果您有任何建议或意见,请随时将它们添加到 github 上的问题中:

https://github.com/fo-dicom/fo-dicom/issues/634

于 2018-03-13T10:06:57.710 回答