简体   繁体   English

如何保存24bit BMP

[英]How to save 24bit BMP

I have a function to capture screen and save it to bitmap (32 bit). 我具有捕获屏幕并将其保存到位图(32位)的功能。 This function works great but I also need 24 bit bitmap. 这个功能很好用,但是我也需要24位的位图。 I have no idea how to convert this function. 我不知道如何转换此功能。

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

Sorry for my english :-) 对不起我的英语不好 :-)

Study about 32 bit and 24 bit presentation of images. 研究图像的32位和24位表示。 Every image file has two parts header and data. 每个图像文件都有标头和数据两部分。 In normal images a upto a fixed size of bytes contains header (Usually 1024byte). 在普通图像中,最大固定大小的字节包含标头(通常为1024byte)。 Then the data starts. 然后数据开始。 If the image size is WxH then you will get WxHx32 byte data. 如果图像大小为WxH,则将获得WxHx32字节数据。 Each 32 bit contains a single pixel information so you will get R, G, B and alpha information (4x8). 每32位包含一个像素信息,因此您将获得R,G,B和alpha信息(4x8)。 you write it in 24 format with only RGB data. 您只用RGB数据以24格式写入它。 That's all you need. 这就是您所需要的。 I haven't find any build in function for it. 我还没有找到任何内置函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何区分32bit和24bit bmp文件? 另外,如何将32位bmp文件读入C ++数组? - How to differentiate between a 32bit and 24bit bmp file? Also how do I read a 32bit bmp file into a C++ array? GDI +加载jpg并保存为24bit png问题 - GDI+ Load a jpg and save as 24bit png problem 24位地址和24位算术与24位地址与16位地址的地址算术之间的区别? - Difference between 24bit addresses and 24bit arithmetic vs 24 bit address with address arithmetic of 16 bit address? Win32工具栏和24位图像 - Win32 Toolbars and 24bit Images 将.bmp(24位)读入2D数组 - Reading .bmp(24 bit) into a 2D array FlatBuffers/Protobuf 中是否有支持任意 24 位有符号整数定义的可移植二进制序列化模式? - Is there a portable Binary-serialisation schema in FlatBuffers/Protobuf that supports arbitrary 24bit signed integer definitions? 现代c ++编译器是否为24位图像处理自动生成代码? - Do modern c++ compilers autovectorize code for 24bit image processing? 如何将图形保存到bmp - How to save graphic to bmp 如何使用OpenGL加载8位bmp? - How can I load 8 bit bmp with OpenGL? 将 24 位“.bmp”图像转换为黑白/单色图像的可能算法是什么? - What are possible algorithm for converting a 24-bit ".bmp" image to black and white/Monochrome image?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM