[英]C++: What's the simplest way to read and write BMP files using C++ on Windows?
I would like to load a BMP file, do some operations on it in memory, and output a new BMP file using C++ on Windows (Win32 native). 我想加载一个BMP文件,在内存中对其进行一些操作,然后在Windows(Win32本机)上使用C ++输出一个新的BMP文件。 I am aware of ImageMagick and it's C++ binding Magick++ , but I think it's an overkill for this project since I am currently not interested in other file formats or platforms.
我知道ImageMagick及其它是C ++绑定的Magick ++ ,但由于我目前对其他文件格式或平台不感兴趣,因此我认为这对于该项目来说是一个过大的杀伤力。
What would be the simplest way in terms of code setup to read and write BMP files? 就读和写BMP文件的代码设置而言,最简单的方法是什么? The answer may be "just use Magick++, it's the simplest."
答案可能是“只使用Magick ++,这是最简单的”。
Related Question: What is the best image manipulation library? 相关问题: 最好的图像处理库是什么?
仅针对Windows开发时,我通常只使用ATL CImage类
A BMP file consists of 3 structures. 一个BMP文件由3个结构组成。 A BITMAPFILEHEADER followed by a BITMAPINFO followed by an array of bytes.
BITMAPFILEHEADER,后跟BITMAPINFO,后跟字节数组。
The absolute simplest way to load a BMP file using Win32 is to call CreateFile, GetFileSize, ReadFile and CloseHandle to load the file image into memory, and then just cast a pointer to the buffer to a BITMAPFILEHEADER and go from there. 使用Win32加载BMP文件的最简单方法是调用CreateFile,GetFileSize,ReadFile和CloseHandle将文件图像加载到内存中,然后将指向缓冲区的指针转换为BITMAPFILEHEADER并从那里去。
I lie, a simpler way is to call LoadImage. 我说谎,一种更简单的方法是调用LoadImage。 Making sure to pass the LR_DIBSECTION flag to ensure that GDI doesnt convert the loaded image into whatever bitdepth your primary display is configured to.
确保传递LR_DIBSECTION标志,以确保GDI不会将加载的图像转换为您的主显示器配置的位深度。 This has the advantage of getting you a HBITMAP that you can select into a DC and therefore draw all over using GDI.
这具有使您可以选择DC的HBITMAP的优势,因此可以使用GDI全面绘制。
To save it however, there is no shortcut. 但是要保存它,没有快捷方式。 You need to prepare a BITMAPFILEHEADER, write it out, fill in a BITMAPINFO struct, write that out, and then the actual pixel data.
您需要准备一个BITMAPFILEHEADER,将其写出,填写BITMAPINFO结构,将其写出,然后再输入实际的像素数据。
CBitmap类执行BMP I / O。
#include <windows.h>
I tried CImage
as above, but I had a C array full of pixel values that I simply wanted to dump as a BMP (or any other format). 我如上所述尝试了
CImage
,但是我有一个充满像素值的C数组,我只是想将其转储为BMP(或任何其他格式)。 CImage
has no constructor for that, and I did not want to link MFC (for CBitmap
) nor try to fathom IWIC. CImage
没有为此构造函数,并且我不想链接MFC(用于CBitmap
),也不想尝试理解IWIC。
What was easy was CImg : 简单的是CImg :
#include <cimg/cimg.h>
using namespace cimg_library;
//...
void SaveMyData(uint8_t *pxarray, int width, int height)
{
CImg<uint8_t> img(pxarray, width, height);
img.save_bmp("sav.bmp");
}
我没有使用Magick ++,但是Windows有一个称为Windows Imaging Component的库,该库可能会满足您的需求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.