【问题标题】:A generic error occurred in GDI+ againGDI+ 中再次出现一般错误
【发布时间】:2013-03-21 13:48:48
【问题描述】:

我了解此消息的含义(需要对非托管资源执行 Dispose),但我真的不明白为什么会发生这种情况:

    System.Drawing.Image imgAnimaha, imgNoanimaha;
                using (System.IO.Stream file = thisExe.GetManifestResourceStream("WindowsApplication1.img.noanimaha135.gif"))
                {

                    using (System.Drawing.Image img = Image.FromStream(file))
                    {
                        imgNoanimaha = (System.Drawing.Image)img.Clone();
                    }
                }

                using (System.IO.Stream file = thisExe.GetManifestResourceStream("WindowsApplication1.img.animaha135.gif"))
                {

                    using (System.Drawing.Image img = Image.FromStream(file))
                    {
                        imgAnimaha = (System.Drawing.Image)img.Clone();
                    }
                }

            pbDiscovery.Image = imgAnimaha;

在这种情况下,我收到“GDI+ 中发生一般错误”为什么以及如何解决? PS。如果我写以下内容:

            pbDiscovery.Image = imgNoanimaha;

它工作正常。 我真的不明白在哪里以及哪些非托管资源没有被释放......

【问题讨论】:

  • 你用不同的位图试过这个吗?

标签: c# gdi+ system.drawing


【解决方案1】:

问题在于Image.Clone(),如:

using (System.Drawing.Image img = Image.FromStream(file))
{
  imgAnimaha = (System.Drawing.Image)img.Clone();
}

... 不会创建图像的深层副本。它会创建所有标题信息的副本,但不会创建实际的像素数据(它只是指向原始像素数据)。当使用超出范围时,原始(也是唯一的)像素数据与原始 img 对象一起处理。

那么问题就变成了,在这里使用的意义何在?我建议没有。将图像读入 System.Drawing.Image 对象,只要您需要像素数据(例如,只要图像需要重绘)就保持活动状态,并且仅在不需要再次显示后将其处置。

【讨论】:

  • 问题是图像加载代码似乎锁定了原始文件。从您克隆基础文件或流已关闭的内容的那一刻起,您就会收到该错误。
猜你喜欢
  • 2011-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多