.NET Problem Works fine on one computer and an exception on another

I wrote a simple C # program using some graphical functions like drawElipse and drawLine in System.Drawing.Graphics. It works fine on one computer, but on my laptop it gives the exception of overflowing graphic functions. I need a program for working on a laptop for presentation in five hours, please help me.

Here are two functions that I get on error:

private void drawDot(int n)
{
    Graphics gfx = CreateGraphics();
    int mapx = (int)verts[n].mapx;
    int mapy = (int)verts[n].mapy;
    Pen myPen = new Pen(Color.DarkOliveGreen, 5);
    if (mapx > 2 && mapy > 2)
    {

        Rectangle rect = new Rectangle((int)mapy - 2, (int)mapx - 2, 10, 10);
        gfx.DrawEllipse(myPen, rect);
    }

}

private void drawLine(int n, int k)
{
    int mapnx = (int)verts[n].mapx;
    int mapny = (int)verts[n].mapy;
    int mapkx = (int)verts[k].mapx;
    int mapky = (int)verts[k].mapy;
    Graphics gfx = CreateGraphics();
    Pen myPen = new Pen(Color.DarkOliveGreen, 3);
    gfx.DrawLine(myPen, mapny, mapnx, mapky, mapkx);
}
+3
source share
2 answers

You need to explicitly place the object Graphicsin the called method. You can do this in two different ways.

  • Explicitly call gfx.Dispose()at the end of your methods.
  • , gfx using, :

    using (Graphics gfx = CreateGraphics())
    {
        // call gfx methods liek DrawLine()
    }
    

MSDN CreateGraphics().

+5

, JITing x64, JIT x86.

0

Source: https://habr.com/ru/post/1747107/


All Articles