0

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!

VVicera
  • 21
  • 5
  • if you want to optimize your `C#` code - don't mess with `GC` – vasily.sib Feb 19 '20 at 08:45
  • 2
    Bitmap is pretty heavy format since it's uncompressed. For a 1080p screen for example each of your image is 54 + 1920 * 1080 * 4 = 8527680 byteso approximately 8Mb. So you're trying to have a program that uses roughtly 200Mb/s of data. It's way too much. look for datastream and compression bit bitmap isn't the way to go for sure. Consider checking this answer for example https://stackoverflow.com/a/4124382/9240942 this is more what you're trying to achieve – Platypus Feb 19 '20 at 09:04
  • i previosly had a problem with memory usage even after disposing and deleting objects. that's why i put GC collect. works for me – VVicera Feb 19 '20 at 09:10
  • i was actually thinking of changing the format instead of bitmap. Thanks for the link, I will read it. – VVicera Feb 19 '20 at 09:12
  • I removed the GC from my code. It automatically collects around 300MB. but still having the same issue. Thank you for your inputs! – VVicera Feb 19 '20 at 09:20
  • If you are looking for predictable performance, don't use a platform or programming language that has non-deterministic performance characteristics. C++ (and C++/CLI) enables you to use deterministic garbage collection. – IInspectable Feb 19 '20 at 09:33

0 Answers0