Capturing a region with SlimDX

274 Views Asked by At

So I'm trying to capture a region using SlimDX. What I mean by this is that I'm not looking to capture from [0,0] to [1920,1080]. Preferably I want to pass a Rectangle object that holds the information needed for the capturing.

I would like to do this with SlimDX (DirectX) as it would drastically improve the capturing time if we look at alternatives like CopyFromScreen.

I need to be capturing around 30 chunks of 100x100 pixels and I think using DirectX might be my best bet. All starting coördinates plus width/height are stored in integer arrays.

I'm currently using the following code:

Rectangle rect = new Rectangle(chunk[0], chunk[1], chunk[2], chunk[3]); 
Bitmap temp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb); 
Graphics g2 = Graphics.FromImage(temp); 
g2.CopyFromScreen(rect.Left, rect.Top, 0, 0, temp.Size, CopyPixelOperation.SourceCopy);

This code is living in a foreach loop that iterates over the chunks variable. However, this takes about ~500ms to complete with 30 iterations.

1

There are 1 best solutions below

1
Xiaoguo Ge On BEST ANSWER

as @Nico pointed out, copy once and then split is faster than many small copies. Here is an example

        var rects = Enumerable.Range(1, 30)
            .Select(x => new Rectangle(x, x, x + 100, x + 100));

        var bounds = Screen.PrimaryScreen.Bounds;
        Bitmap bigBmp = new Bitmap(bounds.Width, bounds.Height, PixelFormat.Format32bppArgb);
        Graphics g2 = Graphics.FromImage(bigBmp);
        g2.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy);

        var bmps = rects.Select(rect =>
            {
                return bigBmp.Clone(rect, PixelFormat.Format32bppArgb);
            });