0

我正在尝试读取 HDC 给定区域上的所有像素以查找是否存在颜色,目前我想出了:

IDirect3DSurface9* pSurface = 0;
p1->CreateOffscreenPlainSurface(1280, 1024,D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &pSurface, NULL);
p1->GetFrontBufferData(0, pSurface);
//assert( pSurface );
if( pSurface && GetTickCount() > dwGAKS_Fix )
{
    HDC dc;
    pSurface->GetDC( &dc );
    COLORREF dpurp = D3DCOLOR_ARGB (255,102,0 ,153);
    for( DWORD h = 610; h <= 670; h++ )
    {
        for( DWORD w = 480; w<=530; w++ )
        {
            COLORREF dwPixel = GetPixel( dc, h, w );
        //  CString strPixel; strPixel.Format( "Pixel col: %u at: %u X %u", dwPixel, d, i );
            //if( dx_Font )
            if( dwPixel == dpurp )
            {
                dx_Font->DrawTextA(NULL, "Shoot", strlen("Shoot"), &pos, DT_NOCLIP, D3DCOLOR_XRGB(0, 255, 0));
            }
            else
                dx_Font->DrawTextA(NULL, "NoShoot", strlen("NoShoot"), &pos, DT_NOCLIP, D3DCOLOR_XRGB(0, 255, 0));
        }
    }
    dwGAKS_Fix = GetTickCount() + 15;
    pSurface->ReleaseDC( dc );
    pSurface->Release();

但是这个解决方案很慢,非常慢,我需要更多的东西......呃专业

编辑

D3DLOCKED_RECT d3dlocked;
                if( D3D_OK == pSurface->LockRect( &d3dlocked, 0, 0 ) )
                {
                    UINT *pixels=(UINT *)locked.pBits;
                    if(pixels[52+15*1024]&0xFFFFFF00==dpurp)
                    {
                        
                    }

                    pSurface->UnlockRect();
                }
4

1 回答 1

2

GetPixel is always slow. You can get direct access to the bits in the off-screen surface using IDirect3DSurface9::LockRect and then scan through the bitmap yourself, which should be much quicker.

(Edit) Any given pixel (x,y) is the 32 bit value found at:

*(DWORD*)(((BYTE*)d3dlocked.pBits) + y * d3dlocked.Pitch + x * sizeof(DWORD));

You should AND the value with 0x00ffffff to ignore the alpha channel.

于 2013-06-15T01:50:39.513 回答