我想通过使用“memcpy”函数将我的 Directx 屏幕从后台缓冲区捕获到内存地址。所以我写了这段代码:
int ScreenWidth =1024;
int ScreenHeight =768;
int BITSPERPIXEL =32; // XRGB
BYTE* pBits ;
IDirect3DSurface9* pSurface;
pBits=new BYTE[ScreenWidth*ScreenHeight * 4 ]; //1024 x 768 x 4 for XRGB
pd3dDevice->CreateOffscreenPlainSurface(ScreenWidth , ScreenHeight ,
D3DFMT_X8R8G8B8, D3DPOOL_SYSTEMMEM,
&pSurface, NULL);
pd3dDevice->GetBackBuffer(0,0,D3DBACKBUFFER_TYPE::D3DBACKBUFFER_TYPE_MONO, &pSurface);
D3DLOCKED_RECT lockedRect;
pSurface->LockRect(&lockedRect,NULL,
D3DLOCK_NO_DIRTY_UPDATE|
D3DLOCK_NOSYSLOCK|D3DLOCK_READONLY);
for( int i=0 ; i < ScreenHeight ; i++)
{
memcpy( (BYTE*) pBits + i * ScreenWidth * BITSPERPIXEL / 8 ,
(BYTE*) lockedRect.pBits + i* lockedRect.Pitch ,
ScreenWidth * BITSPERPIXEL / 8);
}
pSurface->UnlockRect();
但是当程序运行时我得到了这个错误:
Unhandled exception at 0x58b6be20 (msvcr100d.dll) in SimpleSample.exe: 0xC0000005: Access violation reading location 0x00000000.
和这一行的程序中断:
memcpy( (BYTE*) pBits + i * ScreenWidth * BITSPERPIXEL / 8 ,
(BYTE*) lockedRect.pBits + i* lockedRect.Pitch ,
ScreenWidth * BITSPERPIXEL / 8);
它与lockedRect.pBits 或lockedRect.Pitch 地址mem 没有创建相同......你怎么看?请帮我解决这个问题。谢谢你。