When drawing an image: System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI

后端 未结 2 2005
感情败类
感情败类 2020-12-06 06:20

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 wor

2条回答
  •  粉色の甜心
    2020-12-06 06:42

    Without a little more code there is not enough to properly diagnose here, however, one thing to look at is that you may have disposed on the image your a drawing with at some point earlier and it is only after the garbage collector runs that your code is failing. Are you using cloned images anywhere? One thing I was suprised to learn is that if you do a straight clone of an image, you are not cloning the underlying bitmap that the image rely's upon, only the image structure, to create a proper copy of an image you have to create a new image:

    var newImage = new Bitmap(img)
    

    as

    var newImage = oldImg.Clone();
    oldImg.Dispose();
    ...
    gr.DrawImage(newImage, new Rectangle(0,0,newImage.Width,newImage.Height);
    

    will work for a while, but then fail at some random point...

提交回复
热议问题