0

後で画像内のオブジェクトを分析するために、画面の画像データをバッファに入れる純粋なWIN32の方法を探していました...悲しいことに、何も見つかりませんでした。「画面の印刷」を実行できるライブラリ/クラスの提案を受け入れるようになりましたが、それでもメモリバッファにアクセスできるはずです。

どんな助けでも感謝します。

編集:画面を連続してキャプチャすることを忘れていたので、操作速度は非常に重要です。多分誰かがこれのための良いライブラリを知っていますか?

4

2 に答える 2

2
// get screen DC and memory DC to copy to
HDC hScreenDC = GetDC(0);
HDC hMemoryDC = CreateCompatibleDC(hScreenDC);

// create a DIB to hold the image
BITMAPINFO bmi = { 0 };
bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
bmi.bmiHeader.biWidth = GetDeviceCaps(hScreenDC, HORZRES);
bmi.bmiHeader.biHeight = -GetDeviceCaps(hScreenDC, VERTRES);
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
LPVOID pBits;
HBITMAP hBitmap = CreateDIBSection(hMemoryDC, &bmi, DIB_RGB_COLORS, &pBits, NULL, 0);

// select new bitmap into memory DC
HGDIOBJ hOldBitmap = SelectObject(hMemoryDC, hBitmap);

// copy from the screen to memory
BitBlt(hMemoryDC, 0, 0, bmi.bmiHeader.biWidth, -bmi.bmiHeader.biHeight, hScreenDC, 0, 0, SRCCOPY);

// clean up
SelectObject(hMemoryDC, hOldBitmap);
DeleteDC(hMemoryDC);
ReleaseDC(0, hScreenDC);

// the image data is now in pBits in 32-bpp format
// free this when finished using DeleteObject(hBitmap);
于 2013-09-17T20:45:12.997 に答える
1

複数の方法がありますが (DirectX を使用することもできます)、おそらく最も単純で簡単な方法は GDI を使用することです。

    // GetDC(0) will return screen device context
    HDC hScreenDC = GetDC(0);
    // Create compatible device context which will store the copied image
    HDC hMyDC= CreateCompatibleDC(hScreenDC );

    // Get screen properties
    int iScreenWidth = GetSystemMetrics(SM_CXSCREEN);
    int iScreenHeight = GetSystemMetrics(SM_CYSCREEN);
    int iBpi= GetDeviceCaps(hScreenDC ,BITSPIXEL);

    // Fill BITMAPINFO struct
    BITMAPINFO info;
    info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    info.bmiHeader.biWidth = iScreenWidth;
    info.bmiHeader.biHeight = iScreenHeight ;
    info.bmiHeader.biPlanes = 1;
    info.bmiHeader.biBitCount  = iBpi;
    info.bmiHeader.biCompression = BI_RGB;

    // Create bitmap, getting pointer to raw data (this is the important part)
    void *data;
    hBitmap = CreateDIBSection(hMyDC,&info,DIB_RGB_COLORS,(void**)&data,0,0);
    // Select it into your DC
    SelectObject(hMyDC,hBitmap);

    // Copy image from screen
    BitBlt(hMyDC, 0, 0, iScreenWidth, iScreenHeight, hScreenDC , 0, 0, SRCCOPY);

    // Remember to eventually free resources, etc.

かなり前のことなので忘れているかもしれませんが、要点は以上です。警告の言葉: これは高速ではありません。画面が大きく動いている場合、1 フレームをキャプチャするのに 50 ミリ秒以上かかる場合があります。基本的に、PrintScreen キーを押すのと同じことをしています。

于 2013-09-17T20:45:34.253 に答える