I am trying to create a program that takes the screenshot of the screen and display it inside a WPF form.
I am trying to get at least 30FPS but i am having a performance issue.
I am getting the screenshot of a screen using CopyFromScreen then process it to display on an image control.
Whenever I run the program, the mouse seems "heavy" or it became less sensitive.
Based on my observation, CPU usage spikes whenever i call the function.
How can i achieve 30 FPS? I've read about WriteableBitmap but i can't seem to understand how i will incorporate it on my current code. Here is my current code:
private void ScreenCapture(object sender, EventArgs e)
{
System.GC.Collect();
using (Bitmap backgroundImage = new Bitmap(this.auxScreen.Bounds.Width, this.auxScreen.Bounds.Height))
{
using (Graphics gBackgroundImage = Graphics.FromImage(backgroundImage))
{
gBackgroundImage.CopyFromScreen(this.auxScreen.Bounds.X, this.auxScreen.Bounds.Y, 0, 0, backgroundImage.Size);
if (MainWindowValues.Instance.CurrentMode == MainWindowValues.Modes.PM_Duplicate)
{
CURSORINFO pci;
pci.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(typeof(CURSORINFO));
if (GetCursorInfo(out pci))
{
if (pci.flags == CURSOR_SHOWING)
{
DrawIcon(gBackgroundImage.GetHdc(), Mouse.Instance.Position.X, Mouse.Instance.Position.Y, pci.hCursor);
gBackgroundImage.ReleaseHdc();
}
}
}
handleBG = backgroundImage.GetHbitmap();
ImageSource bground = Imaging.CreateBitmapSourceFromHBitmap(handleBG, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
ImageContainer.Dispatcher.Invoke(() => ImageContainer.Source = bground);
gBackgroundImage.Dispose();
DeleteObject(handleBG);
}
backgroundImage.Dispose();
}
System.GC.WaitForPendingFinalizers();
}
Thanks you!