C#位图和图形,内存问题并将屏幕截图存储在内存中

问题描述 投票:-1回答:1

首先,要给出清晰的图片。

在我的课堂上,我有:

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时它立即崩溃,我想我在这里做错了什么可怕的事,我只是无法弄清楚是什么,我希望您可以帮助我指出我在做什么错?

c# bitmap gdi
1个回答
0
投票

答案来自GSerg的评论。

在将值重新分配给FocusImage之前,我必须先对其进行处置,这似乎可以解决问题,内存很好且稳定在固定值。

© www.soinside.com 2019 - 2024. All rights reserved.