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

时间:2010-05-26 02:25:47

标签: 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);
}

2 个答案:

答案 0 :(得分:5)

您需要在您调用的方法中显式处置Graphics对象。你可以用两种不同的方式做到这一点。

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

    using (Graphics gfx = CreateGraphics())
    {
        // call gfx methods liek DrawLine()
    }
    
  3. 您可以在CreateGraphics()方法的MSDN文档中阅读更多内容。

答案 1 :(得分:0)

也许这与一台机器JITing到x64有关,而另一台机器JIT到x86。