Screen capture performance with directx

I am trying to make a fast screen capture with direct x, but the performance is horrible.. This function GetFrontBufferData is some horrible crap.. It takes around 33ms to execute it, which is a horrible result considering i only want around 200px by 200px area... BAd performance is the the main problem.

Regardless if i set the resolution to 200 or 1300, the delay is the same(both for the buffer size and image resolution), which is even more stupid. I was trying to use that back buffer solution, found some tutorials and articles and code examples, but all of them will just freeze the program or return blank / black image all the times, regardless what i try.

The front buffer approach works good, but it's just useless, since standard HBITMAP and wingdi has similar performance if not better and the code is basically few lines of code. If someone could help me to get it to work i would be happy. All i have to do is somehow fix the performance, i guess as everyone suggested by using this backbuffer.

This is a pice of code i found somewhere online:

'void get_dx9_image(IDirect3DDevice9* device)
{
HRESULT hr;
IDirect3DSurface9* backbuffer;
hr = device->GetRenderTarget(0, &backbuffer);
if(FAILED(hr)) {
return;
}

D3DSURFACE_DESC desc;
hr = backbuffer->GetDesc(&desc);
if(FAILED(hr))
{
return;
}

IDirect3DSurface9* buffer;
hr = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &buffer, nullptr);
if(FAILED(hr))
{
return;
}

hr = device->GetRenderTargetData(backbuffer, buffer);
if(FAILED(hr))
{
return;
}

backbuffer->Release();
D3DLOCKED_RECT rect;
hr = buffer->LockRect(&rect, NULL, D3DLOCK_READONLY);
if(FAILED(hr))
{
return;
}

BYTE* img = new BYTE[rect.Pitch * desc.Height];
CopyMemory(img, rect.pBits, rect.Pitch * desc.Height);
buffer->Release();
}'

After i get theraw data into 'img', i will use:

BYTE* raw_image = dx9_capture(&cap);
QImage img((const unsigned char*)raw_image, 1366, 768, QImage::Format_ARGB32);
ui->label->setPixmap(QPixmap().fromImage(img));

I am using Qt framework, i tried also to convert manually the data, or find some piece of code to convert it, but the result is a black image with some broken artifacts.
Topic archived. No new replies allowed.