-1

スクリーンショットを撮って保存するプログラムを作成しようとしていますが、今までは変数 (hbCapture) に保存するだけでした。コードは書かれているように見え、ドキュメントを何度か読みましたが、BITMAP の作成時に無効な初期化エラーが発生します。

#include <stdlib.h>
#include <windows.h>
#include <stdbool.h>
#include <wingdi.h>
#include <winuser.h>

typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned long u32;
typedef unsigned long long u64;

void getScreen()
{
    u16 screenHeight = GetSystemMetrics(SM_CYVIRTUALSCREEN);
    u16 screenWidth = GetSystemMetrics(SM_CXVIRTUALSCREEN);

    HDC hdc = GetDC(NULL); //get a desktop dc (NULL for entire screen)
    HDC hDest = CreateCompatibleDC(hdc); //create a dc for capture

    BITMAP hbCapture = CreateCompatibleBitmap(hdc, screenWidth, screenHeight);
    SelectObject(hDest, hbCapture);

    //Copy screen to bitmap
    BitBlt(hDest, 0, 0, screenWidth, screenHeight, hdc, 0, 0, SRCCOPY);

    //Clean up
    ReleaseDC(NULL, hdc);
    DeleteDC(hDest);
}

これは関数が入るヘッダーです

そして、ここに main.c があります

#include "main.h"

int main()
{
    getScreen();

    return 0;
}

--------------------------私もこれを試しましたが、うまくいきません------------- -------------------------------------------

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <stdbool.h>
#include <wingdi.h>
#include <winuser.h>

typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned long u32;
typedef unsigned long long u64;

void getScreen()
{
    u16 screenHeight = GetSystemMetrics(SM_CYVIRTUALSCREEN);
    u16 screenWidth = GetSystemMetrics(SM_CXVIRTUALSCREEN);

    HDC hdc = GetDC(NULL); //get a desktop dc (NULL for entire screen)
    HDC hDest = CreateCompatibleDC(hdc); //create a dc for capture

    //test
    ReleaseDC(NULL, hdc);
    //DeleteDC(hdc);
    //test

    BITMAP bCapture = CreateCompatibleBitmap(hdc, screenWidth, screenHeight);
    HBITMAP hbCapture = CreateBitmapIndirect(&bCapture);
    SelectObject(hDest, hbCapture);

    //Copy screen to bitmap
    BitBlt(hDest, 0, 0, screenWidth, screenHeight, hdc, 0, 0, SRCCOPY);

    //Clean up
    ReleaseDC(NULL, hdc);
    DeleteDC(hDest);
}

4

1 に答える 1

0

CreateCompatibleBitmapのドキュメントを参照してください

戻り値はHBITMAPではなくBITMAP

への変更:

HBITMAP hbCapture = CreateCompatibleBitmap(hdc, screenWidth, screenHeight);

削除するCreateBitmapIndirect

GetDIBits次に、ビットマップ ヘッダーとピクセルをファイルに保存する必要があります。

また、DeleteObject(hbCapture)クリーンアップのためにコードの最後に含めます。

于 2019-09-14T19:07:26.260 に答える