Home > Software engineering >  C# - Capture Screenshot - 60FPS
C# - Capture Screenshot - 60FPS

Time:11-25

I need to capture the screen faster: I'm using Visual Studio C# Windows Form (.Net 6.0) - 1440p Screen Resolution

Iv tried bellow and a few others and they all seem to take 30 ms for CopyFromScreen to complete:

        Bitmap frame = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
        Graphics graphics = Graphics.FromImage(frame as Image);
        graphics.CopyFromScreen(0, 0, 0, 0, frame.Size);            
        return frame;

I'm doing object detection with machine learning, the detection takes 30ms-50ms.

Making the whole loop take 80ms and this is to long.

How can I capture the screen faster? Id like to avoid using extra hardware like a capture card.

If I use video capture code it only takes 0.2ms to grab a frame. But this is from a video device and not desktop directly.

using Emgu.CV;

Mat frame = new();
var capture = new VideoCapture(0, VideoCapture.API.DShow);
capture.Read(frame);

CodePudding user response:

I don't think it's realist to try to use GDI to capture 60FPS video... Instead, you should use a tool like ffmpeg to capture video directly, including audio. You don't need extra hardware for this, just download ffmpeg and use a command line like this:

ffmpeg -f dshow -i audio="Stereo Mix (Realtek Audio)" 
       -f gdigrab -itsoffset 00:00:0.6 -i desktop -c:v libx264rgb 
       -framerate 60 -crf 20 -preset ultrafast output.mp4

This captures using DirectShow, including the audio from the specified audio output device, and offsets the audio by a short delay to sync it up with video. The -c:v parameter specifies the video codec, -crf specifies the compression (1-50, lower is better but bigger, default is 23), -preset ultrafast specifies the bitrate.

You can call that from C# if you want, of course...

CodePudding user response:

this is code to capture an image with GDI in C#

(idk how good it will work at 60fps, if you test it please let me know its performance)

public Image CaptureScreen()
{
    return new Bitmap(CaptureWindow(User32.GetDesktopWindow()));
}
private Image CaptureWindow(IntPtr handle)
{
    // get te hDC of the target window
    IntPtr hdcSrc = User32.GetWindowDC(handle);
    // get the size
    User32.RECT windowRect = new User32.RECT();
    User32.GetWindowRect(handle, ref windowRect);
    int width = windowRect.right - windowRect.left;
    int height = windowRect.bottom - windowRect.top;
    int monitorCount = PathManager.GetMonitorCount();
    width = (int)(width * getScalingFactor()) * (monitorCount);
    height = (int)(height * getScalingFactor()) * (monitorCount);
    // create a device context we can copy to
    IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
    // create a bitmap we can copy it to,
    // using GetDeviceCaps to get the width/height
    IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height);
    // select the bitmap object
    IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap);
    // bitblt over
    GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY);
    // restore selection
    GDI32.SelectObject(hdcDest, hOld);
    // clean up
    GDI32.DeleteDC(hdcDest);
    User32.ReleaseDC(handle, hdcSrc);

    // get a .NET image object for it
    Image img = Image.FromHbitmap(hBitmap);
    // free up the Bitmap object
    GDI32.DeleteObject(hBitmap);
    return img;
}

private float getScalingFactor()
{
    Graphics g = Graphics.FromHwnd(IntPtr.Zero);
    IntPtr desktop = g.GetHdc();
    int LogicalScreenHeight = GDI32.GetDeviceCaps(desktop, (int)GDI32.DeviceCap.VERTRES);
    int PhysicalScreenHeight = GDI32.GetDeviceCaps(desktop, (int)GDI32.DeviceCap.DESKTOPVERTRES);

    float ScreenScalingFactor = (float)PhysicalScreenHeight / (float)LogicalScreenHeight;

    return ScreenScalingFactor; // 1.25 = 125%
}


private static int GetMonitorCount()
{
    return new ManagementObjectSearcher(@"root\wmi", "SELECT * FROM WmiMonitorID").Get().Count;
}

private class GDI32
{
    [DllImport("gdi32.dll")]
    public static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
    public enum DeviceCap
    {
        VERTRES = 10,
        DESKTOPVERTRES = 117,

        // http://pinvoke.net/default.aspx/gdi32/GetDeviceCaps.html
    }

    public const int SRCCOPY = 0x00CC0020; // BitBlt dwRop parameter

    [DllImport("gdi32.dll")]
    public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest,
    int nWidth, int nHeight, IntPtr hObjectSource,
    int nXSrc, int nYSrc, int dwRop);

    [DllImport("gdi32.dll")]
    public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth,int nHeight);

    [DllImport("gdi32.dll")]
    public static extern IntPtr CreateCompatibleDC(IntPtr hDC);

    [DllImport("gdi32.dll")]
    public static extern bool DeleteDC(IntPtr hDC);

    [DllImport("gdi32.dll")]
    public static extern bool DeleteObject(IntPtr hObject);

    [DllImport("gdi32.dll")]
    public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
}

private class User32
{
    [StructLayout(LayoutKind.Sequential)]
    public struct RECT
    {
        public int left;
        public int top;
        public int right;
        public int bottom;
    }

    [DllImport("user32.dll")]
    public static extern IntPtr WindowFromDC(IntPtr hDC);

    [DllImport("user32.dll")]
    public static extern IntPtr GetDesktopWindow();

    [DllImport("user32.dll")]
    public static extern IntPtr GetWindowDC(IntPtr hWnd);

    [DllImport("user32.dll")]
    public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);

    [DllImport("user32.dll")]
    public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);

}
  • Related