首先,要给出清晰的图片。
在我的课堂上,我有:
Color FocusArea;
Bitmap FocusImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
和一个用于截图的小功能
private void Screenshot()
{
Bitmap screenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics g = Graphics.FromImage(screenshot);
g.CopyFromScreen(startscanx, startscany, 0, 0, scanarea, CopyPixelOperation.SourceCopy);
g.Dispose();
FocusImage = screenshot;
//screenshot.Dispose(); // Shit happens when Disposing this one, wtf ?
}
[大约一分钟后(每秒+-3至6个屏幕截图),内存已累积约4至10gb并明显崩溃。
崩溃时指出的那行是:
Bitmap screenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
我在FocusImage
中放置了屏幕快照后尝试进行处理,但是随后在应用程序中使用ForusImage
时它立即崩溃,我想我在这里做错了什么可怕的事,我只是无法弄清楚是什么,我希望您可以帮助我指出我在做什么错?
答案来自GSerg的评论。
在将值重新分配给FocusImage之前,我必须先对其进行处置,这似乎可以解决问题,内存很好且稳定在固定值。