microsoft / Windows.UI.Composition-Win32-Samples

Windows.UI.Composition Win32 Samples
MIT License
459 stars 186 forks source link

Capture area of a monitor #119

Open michiproep opened 1 year ago

michiproep commented 1 year ago

Is there a way to capture only an area of the desktop or monitor by a given Rectangle?

robmikh commented 1 year ago

No, right now you need to capture the entire monitor and copy out the portion you're interested in using CopySubresourceRegion.

michiproep commented 1 year ago

do you have a short code snippet for this?

robmikh commented 1 year ago

I don't have a self-contained snippet to share with you, but the idea is straight forward. Let's say that you have two textures, texture A comes from the capture API and texture B is the texture that will receive the pixels you're interested in. You would call CopySubresourceRegion on the ID3D11DeviceContext and supply the area of interest from texture A and specify that it will be copied into texture B.

robmikh/GifSnip does this very thing to copy a portion of the screen to an output texture used to encode a gif.

MartinClementson commented 1 year ago

Here are some snippets of the relevant code I used. It's just the relevant lines of my code, It won't run.

Member variable:
D3D11_BOX m_roiWindow; //Region of interest rect
ID3D11Texture2D* m_stagingTexture= NULL;
//Set Function
SetROIWindow(float x, float y, float width, float height) {
    m_roiWindow.left   = max(0.0f,x);
    m_roiWindow.top    = max(0.0f,y);
    m_roiWindow.right  = m_roiWindow.left + width;
    m_roiWindow.bottom = m_roiWindow.top + height;
    }

//Inside OnFrameArrived:
 auto frame = sender.TryGetNextFrame();
 auto swapChainResizedToFrame = TryResizeSwapChain(frame);
 auto surfaceTexture = GetDXGIInterfaceFromObject<ID3D11Texture2D>(frame.Surface());
 m_d3dContext->CopySubresourceRegion(m_stagingTexture, 0, 0, 0, 0, surfaceTexture.get(), 0, &m_roiWindow);
 D3D11_MAPPED_SUBRESOURCE mappedSubResource;
 auto hr = m_d3dContext->Map(m_stagingTexture, 0, D3D11_MAP_READ, 0, &mappedSubResource);

<Use mappedSubResource.pData for something>

//Unmap
m_d3dContext->Unmap(m_stagingTexture, 0);
frame.Close();

An issue I had is that I had to make sure the width and height of the region is 32bit aligned, but that is probably because I use the data in an openGL texture. You might not have that issue.