Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
LPDIRECT3DSURFACE9 g_pSurface;
LPVOID pBits// a pointer point to a HBITMAP;
LPDIRECT3DTEXTURE9 g_pTexture=NULL;//The size of the texture is equal to the surface;
C++
g_pd3dDevice->GetFrontBufferData(0, g_pSurface);
D3DLOCKED_RECT	lockedRect;
D3DLOCKED_RECT	lockedTexRect;
if(FAILED(g_pSurface>LockRect(&lockedRect,NULL,
D3DLOCK_NO_DIRTY_UPDATE|D3DLOCK_NOSYSLOCK|D3DLOCK_READONLY)))
				{
					ErrorMessage("LockRect Failed!");	break;
				}
for(int i=0;i<g_ScreenRect.bottom;i++)
{
memcpy((BYTE*)pBits+(g_ScreenRect.bottom-i-1)*g_ScreenRect.right*BITSPERPIXEL/8,						(BYTE*)lockedRect.pBits+i*lockedRect.Pitch,g_ScreenRect.right*BITSPERPIXEL/8);
}
if(FAILED(hr=g_pTexture->LockRect(0,&lockedTexRect,
NULL,0)))
				{break;}
memcpy(lockedTexRect.pBits,lockedRect.pBits,lockedTexRect.Pitch*g_ScreenRect.Bottom);

pSurface->UnlockRect();
g_pTexture->UnlockRect(0);

I can draw the HBITMAP success,but the texture is worse.
Is there any wrong with the memcpy function to texture?
Posted

1 solution

1. GetFrontBufferData does not recommended to use as it slow.
2. You are getting screen into system memory and what for you copy it back to the texture?
3. You copy via Locking which is not good.

To solve your issues see:
This API D3DXLoadSurfaceFromSurface usd to copy from one surface to another at any surface loacation and colorspace conversion due that it maybe slow. In case if the surfaces are both on GPU memory used StretchRect method - but see the remarks to that method they are important. If you need update surface on texture from system memory and the texture on GPU used UpdateSurface. To get surface of the texture used GetSurfaceLevel method of the texture.

Related to the screen capture see my answer here: Taking Screenshot using DirectX9[^]

Regards,
Maxim.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900