I have some c# experiece, but I'm new to WinForms.
I have a method that takes a screenshot as a BitMap
:
public Bitmap GetSreenshot()
{
Rectangle bounds = Screen.PrimaryScreen.Bounds;
Bitmap bmp = new Bitmap(bounds.Width, bounds.Height,
PixelFormat.Format32bppArgb);
Graphics gfx = Graphics.FromImage(bmp);
gfx.CopyFromScreen(bounds.X,
bounds.Y,
0,
0,
bounds.Size,
CopyPixelOperation.SourceCopy);
return bmp;
}
My problem is that I'm using this in a timer with an interval of ~100ms. Every time a screenshot is taken, I can notice that the UI thread is affected (lag when moving the form etc).
Is there any way to generate the screenshot on another thread so that it wont affect the UI? I've read something about BackgroundWorker
, is that the way to go? I would appreciate if someone could point me in the right direction here.