【问题标题】:Full screenshot to BMP. Issue with bliting and savingBMP 的完整屏幕截图。闪烁和保存问题
【发布时间】:2017-10-27 12:49:01
【问题描述】:

我想截取部分屏幕并将其保存到 BMP 中。为了保存图片,我计划使用 SOIL。我得到here 的位 bliting 函数。

代码:

bool saveScreen(string path)
{
    string name;
    SYSTEMTIME sm;
    GetSystemTime(&sm);
    name = to_string(sm.wHour) + to_string(sm.wMinute) + to_string(sm.wSecond) + to_string(sm.wMilliseconds) 
    + "_" + to_string(sm.wDay) + to_string(sm.wMonth) + to_string(sm.wYear);

    path = /*path + "/" +*/ name + ".bmp";
    const char *charPath = path.c_str();

    BITMAPINFO bmi;
    auto& hdr = bmi.bmiHeader;
    hdr.biSize = sizeof(bmi.bmiHeader);
    hdr.biWidth = screenWidth;
    hdr.biHeight = screenHeight;
    hdr.biPlanes = 1;
    hdr.biBitCount = 32;
    hdr.biCompression = BI_RGB;
    hdr.biSizeImage = 0;
    hdr.biXPelsPerMeter = 0;
    hdr.biYPelsPerMeter = 0;
    hdr.biClrUsed = 0;
    hdr.biClrImportant = 0;

    unsigned char* bitmapBits;
    HDC hdc = GetDC(NULL);
    HDC hBmpDc = CreateCompatibleDC(hdc);

    BITMAP bm;
    HBITMAP hBmp = CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, (void**)&bitmapBits, nullptr, 0);
    SelectObject(hBmpDc, hBmp);
    BitBlt(hBmpDc, 0, 0, screenWidth, 1024, hdc, 0, 0, SRCCOPY);

    vector< unsigned char > buf(screenWidth* screenHeight* 3);

    glPixelStorei(GL_PACK_ALIGNMENT, 1);
    glReadPixels(0, 0, screenWidth, screenHeight, GL_RGB, GL_UNSIGNED_BYTE, bitmapBits);

    int texture = SOIL_save_image(charPath, SOIL_SAVE_TYPE_BMP, screenWidth, screenHeight, 3, bitmapBits);

    return texture;
}

在输出我得到这个:

Broken BMP

看起来是 RGBA/RGB 问题,但我没有在任何地方设置 RGBA。 我在代码中遗漏了什么?获取屏幕截图的正确方法是什么?

【问题讨论】:

    标签: c++ winapi opengl soil


    【解决方案1】:

    您创建了 32 bpp 图像,但是将 3 传递给 SOIL_save_image 表明它是 24 bpp 图像。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-12
      • 2017-02-28
      • 2011-09-29
      • 1970-01-01
      相关资源
      最近更新 更多