.NET Problem Works just fine on one computer on another there's stack overflow exception
I wrote a simple C# program using some graphic functions as drawElipse and drawLine at System.Drawing.Graphics. It works perfectly on one computer, but at my laptop it gives overflow exception at the graphics functions. I need the program to work on the laptop for a presentation after five hours, please help me.
Here are the two functions I get the error in:
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);
}
}
开发者_JAVA百科
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);
}
You need to explicitly dispose the Graphics
object in the method you have called. You can do this in two different ways.
- Explicitly call
gfx.Dispose()
at the end of your methods. Wrap the code that accesses
gfx
inusing
, like this:using (Graphics gfx = CreateGraphics()) { // call gfx methods liek DrawLine() }
You can read a bit more in the MSDN documentation for the CreateGraphics()
method.
Perhaps this is related to one machine JITing to x64, while the other machine JITs to x86.
精彩评论