如何从HBITMAP中获取字节数组

how can i get byte-array from HBITMAP

本文关键字:字节 字节数 数组 获取 HBITMAP      更新时间:2023-10-16

我的环境:
win32
visual c++
directx

我需要将另一个应用程序的脸映射到我的应用程序的纹理
例如,我的Internet Explorer屏幕显示在DirectX Cube的纹理上,并且它是实时更新的

我的方法是下一个:

//in DirectX Rendering Loop...
HWND hSrc = FindWindow(NULL, _T("application's classname"));
HDC hdc = GetDC(hSrc);
RECT targetRect;
GetClientRect(hSrc, &targetRect);
HBITMAP hBitmap = CreateCompatibleBitmap(hdc,targetRect.right,targetRect.bottom);
//now, target application's window is 'hBitmap'
LPDIRECT3DTEXTURE9 ptxt = NULL;    //my destination texture
if( SUCCEEDED(D3DXCreateTexture(gpDevice,targetRect.right, targetRect.bottom, 
        0, 0, D3DFMT_A8B8G8R8, D3DPOOL_MANAGED,&ptxt)))
        {
            D3DLOCKED_RECT lr;
            HRESULT hr = ptxt->LockRect(0,&lr,NULL,D3DLOCK_DISCARD);
             //TODO : memcpy(lr.pBits , 'Start pointer of Bit-Array' , targetRect.right*targetRect.bottom*4); //mydesktop's color is 32bit truecolor (= *4)
            ptxt->UnlockRect(0);
        }

问题:
如何从HBITMAP格式中获取字节数组
win32api中有这样的方法吗

GetByteArray(HBITMAP,无效**)

或者可以使用其他方法的组成来完成此操作?

CreateCompatibleBitmap创建一个DDB,您无法访问这些位。请尝试使用CreateDIBSection。

还要注意,在编写时,您还没有在位图中实际绘制任何内容。