当绘制图像:System.Runtime.InteropServices.ExternalException:GDI中发生一般性错误 [英] When drawing an image: System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI

查看:3683
本文介绍了当绘制图像:System.Runtime.InteropServices.ExternalException:GDI中发生一般性错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个全球性的Graphics对象从Panel创建。定期的图像从盘使用Graphics.DrawImage拾取并吸入到面板()。它工作正常的几次迭代,然后我得到以下有用的异常:

I've got a global Graphics object created from a Panel. At regular intervals an image is picked up from the disk and drawn into the panel using Graphics.DrawImage(). It works fine for a few iterations and then I'm getting the following helpful exception:

System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.
at System.Drawing.Graphics.CheckErrorStatus(Int32 status)
at System.Drawing.Graphics.DrawImage(Image image, Int32 x, Int32 y)
at System.Drawing.Graphics.DrawImage(Image image, Point point)

我排除了内存泄漏,因为我处理图像对象的时候,我用它做。我知道,在面板显示停止前的图像不损坏,可以读细如程序执行罚款一段时间。

I ruled out memory leaks as I dispose of the image object when I'm done with it. I know that the images are not corrupted and can be read fine as the program executes fine for a while before the panel stops showing.

我遇到了同样的问题用一个图片时,但这次至少我得到了一个错误,而不是什么都没有。

I ran into the same problem when using a PictureBox but this time at least I got an error instead of nothing.

我检查在任务管理器的GDI对象和用户对象,但他们始终围绕65用户对象和165 GDI对象时,应用的工作​​原理,当它没有。

I checked the GDI objects and USER objects in the Task Manager but they're always around 65 user objects and 165 GDI objects when the app works and when it doesn't.

我需要尽快得到这条底线是,它不喜欢我可以在.NET系统库坚持断点并看到究竟执行失败。

I do need to get to the bottom of this as soon as and it's not like I can stick breakpoints in .NET System libraries and see where exactly execution fails.

先谢谢了。

修改:这是显示code:

EDIT: This is the display code:

private void DrawImage(Image image)
{
  Point leftCorner = new Point((this.Bounds.Width / 2) - (image.Width / 2), (this.Bounds.Height / 2) - (image.Height / 2));
  _graphics.DrawImage(image, leftCorner);
}

映像加载code:

the image load code:

private void LoadImage(string filename, ref Image image)
{
  MemoryStream memoryStream = DecryptImageBinary(Settings.Default.ImagePath + filename, _cryptPassword);

  image = Image.FromStream(memoryStream);

  memoryStream.Close();
  memoryStream.Dispose();
  memoryStream = null;
}

_image是全球性的参考是的LoadImage更新。因为我想改变为一些地方的全局引用的唯一可能,并保持自我包含的其他方法,他们作为参数传递。 _graphics也是全球性的。

_image is global and its reference is updated in LoadImage. They are passed as parameters as I want to change the global references from as few places as possible only and keep the other methods self contained. _graphics is also global.

我也得到了网站WebBrowser控件,我要么显示一个图像或一次一个网站。当有时间来显示图像,下面code执行:

I've also got a webBrowser control for web sites and I either show an image or a website at one time. when there's time to display an image, the following code executes:

webBrowser.Visible = false;
panel.Visible = true;
DrawImage(_image)
_image.Dispose();
_image = null;

_image被引用pre加载图像。

_image is referencing a pre-loaded image.

希望这有助于。

推荐答案

您的问题是类似我的想法,但并不完全。当你加载图像,你从一个MemoryStream加载它。你必须保持流开放形象的生命周期,请参阅 MSDN Image.FromStream

Your problem is similar to what I thought, but not quite. When you are loading the image, you are loading it from a MemoryStream. You have to keep the stream open for the lifetime of the image, see MSDN Image.FromStream.

您必须保持流打开图像的寿命。

You must keep the stream open for the lifetime of the Image.

解决的办法是让你的图像的副本在FromImage功能:

The solution is to make a copy of your image in the FromImage function:

private void LoadImage(string filename, ref Image image)
{
  using (MemoryStream memoryStream = DecryptImageBinary(Settings.Default.ImagePath + filename, _cryptPassword))
  {
      using (tmpImage = Image.FromStream(memoryStream))
      { 
         image = new Bitmap(tmpImage);
      }
  }

}

要我提到的处置问题类似,图像会显得工作,然后随机的垃圾回收的底层流时失败。

Similar to the dispose problem I mentioned, the image will seem to work and then randomly fail when the underlying stream is garbage collected.

这篇关于当绘制图像:System.Runtime.InteropServices.ExternalException:GDI中发生一般性错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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