C#System.Drawing.Image.get_Width()在WinForms表单最大化时引发异常 [英] C# System.Drawing.Image.get_Width() throws exception on WinForms form is maximized

查看:636
本文介绍了C#System.Drawing.Image.get_Width()在WinForms表单最大化时引发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了Windows Forms Application应用程序,该应用程序应在PictureBox控件上显示图像.

I writing Windows Forms Application application which should show image on the PictureBox control.

要从DICOMDIR文件中检索此图像,我使用 fo-dicom 库(由指南驱动):

To retrieve this image from DICOMDIR file I use fo-dicom library (driven by this guide):

....
private void MainForm_Load(object sender, EventArgs e)
{
    ImageManager.SetImplementation(WinFormsImageManager.Instance);
}
....

// this function is just for example
// real function is bit complicated
private void ShowImage()
{
    // Getting DICOM file, retrieving all info from it
    // Getting dicomDataset instance
    ....

    var id = dicomDataset.Get<string>(DicomTag.ReferencedFileID, -1);
    var dicomImage = new DicomImage(id);
    var bitmap = dicomImage.RenderImage().AsBitmap();
    pictureBox.Image = bitmap ?? pictureBox.ErrorImage;
}

检索图像时,一切正常.但是,一旦我maximize我的MainForm,我就收到System.ArgumentException并带有Parameter is not valid消息:

When image is retrieving all works fine. But as soon as I maximize my MainForm, I got System.ArgumentException with Parameter is not valid message:

这似乎是.NET Framework错误,但是也许有一种方法可以通过override ing PictureBox控件的OnPaint()方法来修复它?

It looks like this is a .NET Framework bug, but maybe there is a way to fix it by overrideing OnPaint() method of PictureBox control?

以前有人见过这个错误吗?

Have anyone see this bug previously?

谢谢.

P.S.在开发此项目期间,我使用以下软件:

P.S. During development this project I use following software:

  1. Windows 10 x64
  2. Visual Studio 2017社区版
  3. .NET Framework 4.5.1
  4. fo-dicom版本3.0.2

编辑#1

Panel而不是PictureBox相同的问题:

推荐答案

您正面临fo-dicom 3.0.2中一个已知且已修复的错误.另请参见 https://github.com/fo-dicom/fo-dicom/Issues/634 . 出于性能原因,DicomImage.RenderImage().AsBitmap()返回的位图没有自己的像素数据,但是具有指向DicomImage字节的指针.因此,AsBitmap()不会复制内存中的所有像素数据. 但是,如果您在局部变量中实例化DicomImage并将Bitmap保存在控件中,则DicomImage将放置在方法的末尾,并且像素数据将被垃圾收集.下次,位图尝试访问像素数据时,会发生此异常.

You are facing a known and already fixed bug in fo-dicom 3.0.2. See also https://github.com/fo-dicom/fo-dicom/issues/634. For performance reason the Bitmap, that DicomImage.RenderImage().AsBitmap() returns, does not have its own pixel data, but has a pointer to the bytes of DicomImage. So the AsBitmap() does not duplicate all the pixel data in memory. But if you instanciate the DicomImage in a local variable and save the Bitmap in the control, then the DicomImage is disposed at the end of the method and the pixel data gets garbace collected. The next time, the Bitmap tries to access the pixel data this exception happens.

下一版本将具有两种方法:AsSharedBitmap()-与现在相同,但对用户更明显-和AsClonedBitmap().

The next release will have two methods: AsSharedBitmap() - the same as now but more obvious to the user - and AsClonedBitmap().

现在的解决方法是,通过调用以下命令手动复制像素数据:

The workaround now is, to copy the pixel data manually by calling:

var bitmap = dicomImage.RenderImage().AsBitmap() .Clone();

var bitmap = dicomImage.RenderImage().AsBitmap().Clone();

这篇关于C#System.Drawing.Image.get_Width()在WinForms表单最大化时引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆