This code runs every 100 ms. The memory usage just keeps increasing until it hits 1.5 GB and then it crashes.
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;
}