如何保存24位BMP



我有一个功能来捕获屏幕并将其保存为位图(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位图像表示。每个图像文件有头和数据两部分。在普通图像中,最多固定大小的字节包含头(通常为1024字节)。然后数据就开始了。如果图像大小是WxH,那么您将获得WxHx32字节的数据。每个32位包含单个像素信息,因此您将获得R, G, B和alpha信息(4x8)。你只写RGB数据的24格式。这就是你所需要的。

最新更新