【发布时间】:2014-09-14 18:56:29
【问题描述】:
此代码每 100 毫秒运行一次。内存使用量不断增加,直到达到 1.5 GB,然后崩溃。
void takeScreenShot()
{
Surface s;
s = CaptureScreen();
pictureBox1.Image = new Bitmap(Surface.ToStream(s, ImageFileFormat.Bmp));
s.Dispose();
}
public Surface CaptureScreen()
{
int width = Screen.PrimaryScreen.Bounds.Width;
int height = Screen.PrimaryScreen.Bounds.Height;
Device device = new Device(new Direct3D(), 0, DeviceType.Hardware, IntPtr.Zero, CreateFlags.HardwareVertexProcessing, new PresentParameters(width, height));
DisplayMode disp = device.GetDisplayMode(0);
Surface s = Surface.CreateOffscreenPlain(device, disp.Width, disp.Height, Format.A8R8G8B8, Pool.Scratch);
device.GetFrontBufferData(0, s);
return s;
}
【问题讨论】:
-
不能重用
CaptureScreen方法中的一些对象吗?喜欢Device和Surface?当它崩溃时会发生什么?您是否收到异常或其他形式的反馈? -
if (pictureBox1.Image != null) pictureBox1.Image.Dispose();
-
@HansPassant
pictureBox1.Image.Dispose(),否则这也是我的猜测。