16

I am implementing a feature that will take screen shot repeatedly and output dirty rectangles between 2 different shots then send re-draw the screen in a window.

I can get it running between 20~30FPS currently. It is already acceptable. But then I made a benchmark and measured its performance. Found out that the Graphics.CopyFromScreen() takes up to 50% of the processing time. (Yep. Even in the worst case, it still takes longer than find all the dirty rectangles) Then I used native API implementation BitBlt() and get no improvement.

I know there may not be any practical reasons to make it any faster than 30FPS in this case. I am just wondering, is there any faster way to take a screen shot?

Thanks.

CharithJ
  • 46,289
  • 20
  • 116
  • 131
AKFish
  • 305
  • 1
  • 3
  • 7

3 Answers3

15

For those who come to this thread, I came to this solution :

using SharpDX;
using SharpDX.Direct3D11;
using SharpDX.DXGI;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Threading.Tasks;

You will need the package SharpDX and SharpDX.Direct3D11

public class ScreenStateLogger
{
    private byte[] _previousScreen;
    private bool _run, _init;

    public int Size { get; private set; }
    public ScreenStateLogger()
    {

    }

    public void Start()
    {
        _run = true;
        var factory = new Factory1();
        //Get first adapter
        var adapter = factory.GetAdapter1(0);
        //Get device from adapter
        var device = new SharpDX.Direct3D11.Device(adapter);
        //Get front buffer of the adapter
        var output = adapter.GetOutput(0);
        var output1 = output.QueryInterface<Output1>();

        // Width/Height of desktop to capture
        int width = output.Description.DesktopBounds.Right;
        int height = output.Description.DesktopBounds.Bottom;

        // Create Staging texture CPU-accessible
        var textureDesc = new Texture2DDescription
        {
            CpuAccessFlags = CpuAccessFlags.Read,
            BindFlags = BindFlags.None,
            Format = Format.B8G8R8A8_UNorm,
            Width = width,
            Height = height,
            OptionFlags = ResourceOptionFlags.None,
            MipLevels = 1,
            ArraySize = 1,
            SampleDescription = { Count = 1, Quality = 0 },
            Usage = ResourceUsage.Staging
        };
        var screenTexture = new Texture2D(device, textureDesc);

        Task.Factory.StartNew(() =>
        {
            // Duplicate the output
            using (var duplicatedOutput = output1.DuplicateOutput(device))
            {
                while (_run)
                {
                    try
                    {
                        SharpDX.DXGI.Resource screenResource;
                        OutputDuplicateFrameInformation duplicateFrameInformation;

                        // Try to get duplicated frame within given time is ms
                        duplicatedOutput.AcquireNextFrame(5, out duplicateFrameInformation, out screenResource);

                        // copy resource into memory that can be accessed by the CPU
                        using (var screenTexture2D = screenResource.QueryInterface<Texture2D>())
                            device.ImmediateContext.CopyResource(screenTexture2D, screenTexture);

                        // Get the desktop capture texture
                        var mapSource = device.ImmediateContext.MapSubresource(screenTexture, 0, MapMode.Read, SharpDX.Direct3D11.MapFlags.None);

                        // Create Drawing.Bitmap
                        using (var bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb))
                        {
                            var boundsRect = new Rectangle(0, 0, width, height);

                            // Copy pixels from screen capture Texture to GDI bitmap
                            var mapDest = bitmap.LockBits(boundsRect, ImageLockMode.WriteOnly, bitmap.PixelFormat);
                            var sourcePtr = mapSource.DataPointer;
                            var destPtr = mapDest.Scan0;
                            for (int y = 0; y < height; y++)
                            {
                                // Copy a single line 
                                Utilities.CopyMemory(destPtr, sourcePtr, width * 4);

                                // Advance pointers
                                sourcePtr = IntPtr.Add(sourcePtr, mapSource.RowPitch);
                                destPtr = IntPtr.Add(destPtr, mapDest.Stride);
                            }

                            // Release source and dest locks
                            bitmap.UnlockBits(mapDest);
                            device.ImmediateContext.UnmapSubresource(screenTexture, 0);

                            using (var ms = new MemoryStream())
                            {
                                bitmap.Save(ms, ImageFormat.Bmp);
                                ScreenRefreshed?.Invoke(this, ms.ToArray());
                                _init = true;
                            }
                        }
                        screenResource.Dispose();
                        duplicatedOutput.ReleaseFrame();
                    }
                    catch (SharpDXException e)
                    {
                        if (e.ResultCode.Code != SharpDX.DXGI.ResultCode.WaitTimeout.Result.Code)
                        {
                            Trace.TraceError(e.Message);
                            Trace.TraceError(e.StackTrace);
                        }
                    }
                }
            }
        });
        while (!_init) ;
    }

    public void Stop()
    {
        _run = false;
    }

    public EventHandler<byte[]> ScreenRefreshed;
}

This code will get as fast as possible the frames from the front buffer of the graphic device and retrieve the byte[] from the bitmap it creates. The code seems to be stable in memory and processor usage (GPU and CPU).

usage :

var screenStateLogger = new ScreenStateLogger();
screenStateLogger.ScreenRefreshed += (sender, data) =>
{
    //New frame in data
};
screenStateLogger.Start();
TheXenocide
  • 1,060
  • 8
  • 22
Pomme De Terre
  • 342
  • 4
  • 14
  • 2
    I got 17-18fps at 3840x2160 and ~60fps at 1920x1080 with a Fury X and i7-3770. – Rick Velde Oct 12 '17 at 19:22
  • 1
    @Pomme De Terre Could you kindly tell how could this be done for multiple screens? – AbbasFaisal Apr 23 '18 at 13:31
  • It's been a long time and I don't have the code anymore (nor the config for it). But I can at least say that : I think you can loop over adapters (for different device). On each adapter you can get multiple output. Looping over outputs will, I think, retrieve you the front buffer of the output targeted. This might drain resources. – Pomme De Terre Apr 24 '18 at 18:14
  • http://sharpdx.org/wiki/class-library-api/dxgi/ in the API reference (whatever its rendering problems), we can see that the Output is defined as : "An interface represents an adapter output (such as a monitor)". It seems you can count the outputs with GetOutputCount on the adapter interface. In the same logic the GetAdapterCount method from the factory interface should help get the different adapter for the different device in case you got such a configuration. Hope that helped. – Pomme De Terre Apr 24 '18 at 18:29
  • Does that also record audio or just the screen? – Beyondo Aug 05 '20 at 13:49
  • Only screen is recorded – Pomme De Terre Nov 13 '20 at 18:17
  • Thx, your example is quite a good example to start with. However, your "huge" try/catch block is not a good practice ;) Each line in there might require individual error handling, just wrapping everything with "try/catch" is somewhat ugly, tho ;) – dognose Jan 19 '21 at 15:56
  • You are totally right. I will see if I can manage to clean it when I get some time. – Pomme De Terre Jan 19 '21 at 16:12
8

This is very similar to a question asked several years ago: Here. That question was whether directx's capturing abilities could be used to get better performance.

The consensus was that it probably wouldn't provide any performance increase, TightVNC does it very quickly by cheating. It uses a driver that doesn't have to use the API that (presumably) .NET is using.

At some point I recall looking at the source code for Camstudio and I believe they use directx's capturing capabilities. I don't think you can push that much past 30 fps, and most of the time not even that. I'm not sure if that is a problem with the hooks camstudio uses to figure out when something has changed or the actual capture mechanism.

Tremmors
  • 2,906
  • 17
  • 13
0

For alternative to CopyFromScreen() check this. Note that the Graphics.CopyFromScreen() itself calls the API BitBlt() to copy from screen, you can check the source code using Reflector.

Jalal Said
  • 15,906
  • 7
  • 45
  • 68
  • 3
    Or nowadays you can even look at the original code here: http://referencesource.microsoft.com/#System.Drawing/commonui/System/Drawing/Graphics.cs,f76c9e39776eeb24 – Jens Jan 27 '16 at 11:58
  • @jens yes indeed as Microsoft is now smarter by making this available :) – Jalal Said Jan 31 '16 at 14:03