【问题标题】:Read pixels from a HDC从 HDC 读取像素
【发布时间】:2013-06-11 17:55:33
【问题描述】:

我正在尝试读取 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();
                }

【问题讨论】:

    标签: c++ directx gdi


    【解决方案1】:

    GetPixel 总是很慢。您可以使用IDirect3DSurface9::LockRect 直接访问屏幕外表面中的位,然后自己扫描位图,这应该会快得多。

    (编辑)任何给定的像素 (x,y) 都是在以下位置找到的 32 位值:

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

    您应该将值与 0x00ffffff 相加以忽略 Alpha 通道。

    【讨论】:

    • 嗨,乔纳森,感谢您的回答,您能否发布一个示例,说明如何使用D3DLOCKED_RECT 比较像素颜色?谢谢! (编辑问题,我目前的解决方案是错误的......)
    • 编辑了我的问题以添加一个小例子。
    猜你喜欢
    • 2019-08-25
    • 2013-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-27
    • 1970-01-01
    相关资源
    最近更新 更多