0

スクリーンショットをファイルに保存する関数を作成してい.bmpます。画面をファイルに保存することはできましたが、保存するHBITMAPときに問題が発生しました。助けていただければ幸いです。

関数を含むヘッダーは次のとおりです。

#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

    ReleaseDC(NULL, hdc);

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

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

//test
    ReleaseDC(NULL, hdc);

    char memBuffer[10000];
    BITMAPINFO bitmapInfo;
    bitmapInfo.bmiHeader.biHeight = screenHeight;
    bitmapInfo.bmiHeader.biWidth = screenWidth;
    bitmapInfo.bmiHeader.biSize = screenWidth * screenHeight * 3;
    bitmapInfo.bmiHeader.biCompression = BI_RGB;//NOT SURE
    bitmapInfo.bmiHeader.biPlanes = 1;
    bitmapInfo.bmiHeader.biBitCount = 8; //NOT SURE
    bitmapInfo.bmiHeader.biClrImportant = 0;
    GetDIBits(hDest, hbCapture, 0, screenHeight, &memBuffer, &bitmapInfo, DIB_RGB_COLORS);

    FILE * fPointer = fopen("screen.png", "wb");//TO CHANGE
    WriteFile(fPointer, &memBuffer, (WORD) sizeof(memBuffer), 0, NULL);

    fclose(fPointer);
//test

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

また、main.c は次のとおりです。

#include "main.h"

int main()
{
    getScreen();

    return 0;
}

StackOverflow でこのトピックに関するすべての質問を既に読みましたが、どれも問題を明確にしていません。

4

1 に答える 1