directx – error in capture screen and save to bmp file?
I am using directx9 to capture screen and save to bmp file.
here is my code:
IDirect3DSurface9* pRenderTarget = NULL;
IDirect3DDevice9* device = NULL;
IDirect3D9* mpD3D = Direct3DCreate9(D3D_SDK_VERSION);
HWND hwnd = GetDesktopWindow();
D3DPRESENT_PARAMETERS md3dpp = { 0 };
IDirect3DSurface9* pRt = NULL;
D3DDISPLAYMODE mode;
mpD3D->GetAdapterDisplayMode(NULL, &mode);
md3dpp.Windowed = TRUE;
md3dpp.hDeviceWindow = hwnd;
md3dpp.BackBufferCount = 1;
md3dpp.EnableAutoDepthStencil = TRUE;
md3dpp.AutoDepthStencilFormat = D3DFMT_A8R8G8B8;
md3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
md3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
md3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
HRESULT hr = mpD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_MIXED_VERTEXPROCESSING, &md3dpp, &device);
hr = device->GetRenderTarget(0, &pRenderTarget);
hr = device->CreateOffscreenPlainSurface(1920, 1080, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &pRt, NULL);
//hr = device->GetRenderTargetData(pRenderTarget, pDestTarget);
//hr = device->GetFrontBufferData(0, pDestTarget);
hr = device->GetFrontBufferData(0, pRt);
D3DLOCKED_RECT pLockedRect = { 0 };
hr = pRt->LockRect(&pLockedRect, NULL, 0);
LPBYTE pBuffer = new BYTE[pLockedRect.Pitch * mode.Height];
CopyMemory(pBuffer, pLockedRect.pBits, pLockedRect.Pitch * mode.Height);
pRt->UnlockRect();
BITMAPINFOHEADER header;
header.biWidth = mode.Width;
header.biHeight = mode.Height;
header.biSizeImage = pLockedRect.Pitch * mode.Height;
header.biSize = sizeof(BITMAPINFOHEADER);
header.biPlanes = 1;
header.biBitCount = 3 * 8;
header.biCompression = 0;
header.biYPelsPerMeter = 0;
header.biXPelsPerMeter = 0;
header.biClrUsed = 0;
header.biClrImportant = 0;
BITMAPFILEHEADER bfh = { 0 };
bfh.bfType = 0x4d42;
bfh.bfOffBits = sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER);
bfh.bfSize = bfh.bfOffBits + header.biSizeImage;
unsigned int rough_size = sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER) + header.biSizeImage;
unsigned char* p = new unsigned char[rough_size];
memcpy(p, &bfh, sizeof(BITMAPFILEHEADER));
memcpy(p + sizeof(BITMAPFILEHEADER), &header, sizeof(BITMAPINFOHEADER));
memcpy(p + sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER), pBuffer, header.biSizeImage);
HANDLE file = CreateFileW(L"D:\\1.bmp", FILE_APPEND_DATA, 0, 0, CREATE_ALWAYS, 0, 0);
DWORD lp = 0;
WriteFile(file, p, rough_size, &lp, 0);
but the image is like this:
[![enter image description here][1]][1]
first capture the render target and save the bytes
you may say why I do not use directx 11 .yes I have done the directx11 now I want to know can I do it using directx9
what is wrong?
[1]: i.sstatic.net/Z4zrga7m.png
Read more here: Source link
