简体   繁体   中英

Read pixels from a HDC

I'm trying to read all pixels on a given area of a HDC to find if a color is present, currently I came up with:

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();

But this solution is slow, very slow, I need something somewhat more..uh professional

edit

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();
                }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM