C# 打印屏幕功能中的System.ArgumentException

C# 打印屏幕功能中的System.ArgumentException,c#,memory-leaks,screenshot,C#,Memory Leaks,Screenshot,我在项目中使用此功能打印屏幕的一部分: public static Bitmap PrintArea(int x1, int y1, int x2, int y2) { int width = x2 - x1; int height = y2 - y1; var bmpScreenshot = new Bitmap(width, height, PixelFormat.Format32bppArgb); // Take the screenshot from the u

我在项目中使用此功能打印屏幕的一部分:

public static Bitmap PrintArea(int x1, int y1, int x2, int y2)
{
   int width = x2 - x1;
   int height = y2 - y1;
   var bmpScreenshot = new Bitmap(width, height, PixelFormat.Format32bppArgb);

   // Take the screenshot from the upper left corner to the right bottom corner.
   using (Graphics gfxScreenshot = Graphics.FromImage(bmpScreenshot))
            gfxScreenshot.CopyFromScreen(x1, y1, 0, 0, new Size(width, height));            
   return bmpScreenshot;
}
我在while循环的主类中调用这个方法并处理 每次使用后都返回位图,但过了一段时间我

System.ArgumentException:参数无效

gfxScreenshot.CopyFromScreen()上
。我在谷歌上搜索了这个问题,发现一些主题说
CopyFromScreen
内存泄漏。
我怎样才能修好它?我应该为我的.Net做一些更新来解决这个问题吗?或者有其他的方法来占据屏幕的一部分

首先假设你犯了一个错误,假设这个错误就是它所说的。您正在将哪些参数传递给CopyFromScreen?什么是x1,y1,。。。。价值观你签入调试器了吗?是的,我检查了调试器,参数没有错误。你为什么要分配gfxScreenshot两次?第二次似乎是安全的(在使用中),但我看不出第一次是如何处理的。我想我会丢掉第一个作业。谢谢你指出@bickets,我编辑了我的代码,我只是在复制代码时出错了。好的。如果看不到调用代码传递给方法的内容,很难判断x1、y1等的值是否正确。我将首先调试以查看值是什么。我开始认为这不是内存泄漏——特别是如果它第一次就不起作用的话。