将CreateDIBSection BMP数据写入文件

时间:2015-11-29 22:05:39

标签: c++ winapi

我已经以程序方式拍摄了截图,我想要写出要归档的BGRA。 BGRA保存在pixelBuffer

以下是我用来写入文件的代码:

    BYTE *pixelBuffer;
    HBITMAP hbmp;
    hbmp = CreateDIBSection(hdcScreen, &bmi, DIB_RGB_COLORS, (void **)&pixelBuffer, NULL, 0);


FILE *stream;\
if (fopen_s(&stream, "C:\\Users\Vayeate\\Desktop\\blah.txt", "wb+") == 0) {
    fwrite(pixelBuffer, (screenHeight * screenWidth * 4), (size_t)(sizeof(CHAR) + 1), stream); // (screenWidth * screenHeight * 4)
    fclose(stream);
}

然而,这写了一堆乱码,看起来像:

òîîÿòîîÿ

永远重复。我希望得到类似255, 100, 100, 255的内容。

这是我的完整代码:

#include <Windows.h>
#include <stdio.h>

int WINAPI WinMain(HINSTANCE hinst, HINSTANCE, LPSTR, int)
{
    //Sleep(1000)

    MessageBox(0, L"Hello World", L"Unipen", MB_ICONINFORMATION);


    int i = 0;
    DISPLAY_DEVICE device;

    device.cb = sizeof(device);


    while (EnumDisplayDevices(NULL, i, &device, 0) && ++i) {
        if ((device.StateFlags & DISPLAY_DEVICE_ACTIVE) != DISPLAY_DEVICE_ACTIVE) {
            MessageBox(0, device.DeviceName, L"CONTINUE", MB_ICONINFORMATION);
            continue;
        }
        MessageBox(0, device.DeviceName, L"BREAK", MB_ICONINFORMATION);
        break;
    }

    size_t screenWidth = 1920;
    size_t screenHeight = 1200;
    size_t colorLen = 4;

    HDC hdcScreen;
    hdcScreen = CreateDC(NULL, device.DeviceName, NULL, NULL);
    if (hdcScreen == (HDC)NULL) {
        MessageBox(0, L"UnableToCreateDC", L"ERROR", MB_ICONINFORMATION);
        return 0;
    }

    HDC hdcMemoryDC;
    hdcMemoryDC = CreateCompatibleDC(hdcScreen);
    if (hdcMemoryDC == (HDC)NULL) {
        DeleteDC(hdcScreen);
        MessageBox(0, L"UnableToCreateCompatibleDC", L"ERROR", MB_ICONINFORMATION);
        return 0;
    }

    BITMAPINFO bmi;
    bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bmi.bmiHeader.biWidth = (LONG)screenWidth;
    bmi.bmiHeader.biHeight = (-1)*(LONG)screenHeight;
    bmi.bmiHeader.biPlanes = 1;
    bmi.bmiHeader.biBitCount = 32;
    bmi.bmiHeader.biCompression = BI_RGB;

    BYTE *pixelBuffer;
    HBITMAP hbmp;
    hbmp = CreateDIBSection(hdcScreen, &bmi, DIB_RGB_COLORS, (void **)&pixelBuffer, NULL, 0);
    if (hbmp == (HBITMAP)NULL) {
        DeleteDC(hdcScreen);
        DeleteDC(hdcMemoryDC);
        MessageBox(0, L"UnableToCreateDIBSection", L"ERROR", MB_ICONINFORMATION);
        return 0;
    }

    //HGDIOBJ rez_selected;
    HBITMAP rez_selected_bmp;
    rez_selected_bmp = (HBITMAP)SelectObject(hdcMemoryDC, hbmp);
    if (rez_selected_bmp == (HBITMAP)NULL) {
        DeleteDC(hdcScreen);
        DeleteDC(hdcMemoryDC);
        DeleteObject(hbmp);
        MessageBox(0, L"UnableToCreateDIBSection", L"ERROR", MB_ICONINFORMATION);
        return 0;
    }

    BitBlt(hdcMemoryDC, 0, 0, screenWidth, screenHeight, hdcScreen, 0, 0, SRCCOPY);


    //(void) SelectObject(hdcMemoryDC, rez_selected_bmp); // i dont do this step in nativeshot

    FILE *stream;\
    if (fopen_s(&stream, "C:\\Users\Vayeate\\Desktop\\blah.txt", "wb+") == 0) {
        fwrite("asdfasdfasdf", 1, (size_t)(sizeof(CHAR) + 1), stream); // (screenWidth * screenHeight * 4)
        fclose(stream);
    }

    DeleteDC(hdcScreen);
    DeleteDC(hdcMemoryDC);
    DeleteObject(hbmp);

    MessageBox(0, L"DONE", L"Unipen", MB_ICONINFORMATION);

    return 0;
}

如何将空格分隔的BGRA数据存档?

1 个答案:

答案 0 :(得分:3)

您正在编写二进制数据并将其作为文本读取。因此,òîîÿòîîÿ偶然可能是正确的输出,因为这些是二进制值映射到的字符,根据显示它的文本编辑器使用的字符映射。

如果要将缓冲区中像素的空格分隔值作为写入文本文件的“人类可读”ASCII,则首先必须使用fprintf来相应地转换这些值。 / p>

以此为例:

if (fopen_s(&stream, "C:\\Users\Vayeate\\Desktop\\blah.txt", "w") == 0) {
    for(size_t px = 0 ; px < (screenHeight * screenWidth * 4) ; ++px) {
        fprintf(stream, "%hhu, ", pixelBuffer[px]);
    }
    fclose(stream);
}