'Capturing screen in c# wpf

I'm trying to create a simple remote desktop control using TCP client/server in c#. I have this method to capture screenshots:

    private Bitmap CaptureScreen()
    {
        CURSORINFO info;
        info.cbSize = Marshal.SizeOf(typeof(CURSORINFO));
        GetCursorInfo(out info);
        IntPtr cursorHandle = info.flags == CURSOR_SHOWING ? info.hCursor : IntPtr.Zero;

        Rectangle captureRectangle = Screen.AllScreens[0].Bounds;
        Bitmap captureBitmap = new Bitmap(captureRectangle.Width, captureRectangle.Height, PixelFormat.Format16bppRgb565);
        Graphics captureGraphics = Graphics.FromImage(captureBitmap);
        captureGraphics.CopyFromScreen(captureRectangle.Left, captureRectangle.Top, 0, 0, captureRectangle.Size);
        DrawIcon(captureGraphics.GetHdc(), Cursor.Position.X, Cursor.Position.Y, cursorHandle);

        captureGraphics.ReleaseHdc();
        Cursor.Current.Dispose();
        captureGraphics.Flush();

        return captureBitmap;
    }

This code works fine. However, if I use stopwatch to measure the run time of this method, I always get values between 70-100ms which is too much time for desktop remote control... (I get only about 6-8 FPS with this code (and the read time).

Does someone know how to improve this method? I couldn't find any better replacement for Graphics.CopyFromScreen().



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source