Search code examples
c#graphics2dpictureboxgdi

picture box crossed out C#


I have a project in which I draw several rectangles using GDI. I do it using i++ from i=0 to i<=10. I use e.Graphics.FillRectangles. I refresh the drawn rectangles every 1/10s by calling Graphics.Invalidate() My problem is that my picture box gets crossed out when drawing more the 4 rectangles. I guess it's because the picture box is out of memory. What can I do against it?

Thx in advance :D


Solution

  • This red cross appears when an uncaught exception is thrown during the paint of the control. The most common cause of this is, that one has attached a custom handler to the Paint event of the contol and has a bug in the attached code, that leads to the exception getting thrown.

    Surround your custom paint logic with a try...catch block and log the exception to a location of your liking (Debug.WriteLine(), file, etc.) to investigate.

    A by product of this will be: The red cross will immediatly vanish, because the exception is no longer passed down to the drawing routine of the control.