BMP的完整屏幕截图.出血和保存问题 [英] Full screenshot to BMP. Issue with bliting and saving

查看:136
本文介绍了BMP的完整屏幕截图.出血和保存问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想捕获部分屏幕并将其保存到BMP中.为了保存图片,我计划使用SOIL.我在此处获得位位转换功能

I want to take a capture of part of screen and save it into BMP. To save picture I plan with SOIL. Bit bliting functions I get here.

代码:

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;
}

在输出中我得到这个:

BMP损坏

它看起来像是RGBA/RGB问题,但是我没有在任何地方设置RGBA. 我在代码中错过了什么?这是获取屏幕截图的正确方法吗?

It looks as RGBA/RGB issue, but I don't set RGBA nowhere. What I missed in the code? It's the right way to get screenshot?

推荐答案

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

You create 32 bpp image, however pass 3 to SOIL_save_image indicating that it is 24 bpp image.

这篇关于BMP的完整屏幕截图.出血和保存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆