c++ - 如何保存24位BMP

标签 c++ bitmap screen-capture

我有一个功能来捕获屏幕并将其保存到位图(32 位)。这个功能很好用,但我还需要 24 位位图。 我不知道如何转换这个函数。

        HWND okno=GetDesktopWindow();       
        HDC _dc = GetWindowDC(okno); 
        RECT re;
        GetWindowRect( okno, & re );
        unsigned int w = re.right, h = re.bottom;

        HDC dc = CreateCompatibleDC( 0 );
        HBITMAP bm = CreateCompatibleBitmap( _dc, w, h );
        SelectObject( dc, bm );
        StretchBlt( dc, 0, 0, w, h, _dc, 0, 0, w, h, SRCCOPY );

        void * file = CreateFile(file_name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 ); //create file
        void * buf = new char[ w * h * 3 ]; //buffor

        GetObject( bm, 84, buf );
        HDC ddd = GetDC( 0 );
        HDC dc2 = CreateCompatibleDC( ddd );

        tagBITMAPINFO bit_info; //bitmapinfo
        bit_info.bmiHeader.biSize = sizeof( bit_info.bmiHeader );  
        bit_info.bmiHeader.biWidth = w;
        bit_info.bmiHeader.biHeight = h;  
        bit_info.bmiHeader.biPlanes = 1;  
        bit_info.bmiHeader.biBitCount = 32;
        bit_info.bmiHeader.biCompression = 0; 
        bit_info.bmiHeader.biSizeImage = 0; 
        CreateDIBSection( dc, & bit_info, DIB_RGB_COLORS, & buf, 0, 0 );
        GetDIBits( dc, bm, 0, h, buf, & bit_info, DIB_RGB_COLORS );

        BITMAPFILEHEADER bit_header;
        bit_header.bfType = MAKEWORD( 'B', 'M' );
        bit_header.bfSize = w * h * 4 + 54;
        bit_header.bfOffBits = 54;

        BITMAPINFOHEADER bit_info_header;
        bit_info_header.biSize = 40;
        bit_info_header.biWidth = w;
        bit_info_header.biHeight = h;
        bit_info_header.biPlanes = 0;
        bit_info_header.biBitCount = 32;
        bit_info_header.biCompression = 0;
        bit_info_header.biSizeImage = w * h *4;

        DWORD r;
        WriteFile( file, & bit_header, sizeof( bit_header ), & r, NULL );
        WriteFile( file, & bit_info_header, sizeof( bit_info_header ), & r, NULL );
        WriteFile( file, buf, w * h * 4, & r, NULL );

对不起我的英语:-)

最佳答案

研究图像的 32 位和 24 位表示。每个图像文件都有标题和数据两部分。在普通图像中,最多固定大小的字节包含 header (通常为 1024 字节)。然后数据开始。如果图像尺寸为 WxH,那么您将获得 WxHx32 字节数据。每个 32 位包含一个像素信息,因此您将获得 R、G、B 和 alpha 信息 (4x8)。你只用 RGB 数据以 24 格式编写它。这就是你所需要的。我还没有找到它的任何内置功能。<​​/p>

关于c++ - 如何保存24位BMP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17147485/

相关文章:

c++ - boost::spirit ,如何获取占位符的 "value"

c++ - 在 QLCDNumber 上设置厚度

c# - 在循环中创建位图时出现内存溢出异常

具有 16 位颜色深度的 C++ Direct3D9 GetFrontBufferData

windows - 使用 FFMPEG 记录屏幕区域?

ios-simulator - Quicktime X - 如何在屏幕捕获期间隐藏鼠标?

c++ - 如何将仿函数用作类模板中的成员?

c++ - 如何组织我的 C++ 游戏的 Windows 和 Android 版本?

Android打开带有特定文件夹链接的默认图库无法加载

java - BitmapFactory.decodeByteArray 内存不足