.NET问题在另一台计算机上工作正常,存在堆栈溢出异常

Iva*_*kov 3 c# overflow

我在System.Drawing.Graphics中使用一些图形函数作为drawElipse和drawLine编写了一个简单的C#程序.它在一台计算机上完美运行,但在我的笔记本电脑上,它在图形功能上提供溢出异常.我需要该程序在笔记本电脑上工作五小时后进行演示,请帮助我.

以下是我收到错误的两个函数:

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);
}
Run Code Online (Sandbox Code Playgroud)

Fra*_*nov 5

您需要Graphics在您调用的方法中显式处置对象.您可以通过两种不同的方式完成此操作.

  1. gfx.Dispose()在方法结束时明确调用.
  2. 总结访问的代码gfxusing,如下所示:

    using (Graphics gfx = CreateGraphics())
    {
        // call gfx methods liek DrawLine()
    }
    
    Run Code Online (Sandbox Code Playgroud)

您可以在MSDN文档中阅读该CreateGraphics()方法的更多内容.