I'm trying to take a snapshot of the whole screen for reading pixel values. Actually i'm doing it without any problem. But after exactly 214 snapshots, i'm getting out of memory exception.
Bitmap ScreenShot = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height);
public Bitmap TakeSnapshot()
{
Graphics graphic = null;
Rectangle rect = new Rectangle(0, 0, Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height);
using (graphic = Graphics.FromImage(ScreenShot))
{
graphic.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y,
0, 0,
ScreenShot.Size,
CopyPixelOperation.SourceCopy);
}
return ScreenShot.Clone(rect,System.Drawing.Imaging.PixelFormat.Format32bppArgb);
}
I'm using this method with timer
Bitmap bmp = TakeSnapshot();
var c = bmp.GetPixel(0,0);
It was giving invalid parameter exception. I solved it with "using". But now i'm stuck on this exception.
You need to dispose disposable resources once you're done working with them.
Bitmap
class implementsIDisposable
- so it is disposable resource. Correct pattern is instead ofSomething like
Or in short form (which is preferable):
Edit
You can easily emulate the issue: