C++:使用 C++ 读写 BMP 文件的最简单方法是什么? 在 Windows 上?
我想加载一个BMP文件,在内存中对其进行一些操作,然后在Windows(Win32本机)上使用C++输出一个新的BMP文件。 我知道 ImageMagick 并且它是 C++ 绑定 Magick++,但我认为这对于这个项目来说有点矫枉过正,因为我目前对其他文件格式或平台不感兴趣。
就读取和写入 BMP 文件的代码设置而言,最简单的方法是什么? 答案可能是“就用Magick++吧,这是最简单的”。
相关问题:什么是最好的图像处理库?
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). 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.
What would be the simplest way in terms of code setup to read and write BMP files? The answer may be "just use Magick++, it's the simplest."
Related Question: What is the best image manipulation library?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
仅针对 Windows 进行开发时,我通常只使用 ATL CImage类
When developing just for Windows I usually just use the ATL CImage class
EasyBMP 如果您只需要 bmp 支持。 我不够简单,可以在几分钟内开始使用,如果您需要的话,它是多平台的。
EasyBMP if you want just bmp support. I'ts simple enough to start using within minutes, and it's multiplatform if you would need that.
BMP 文件由 3 个结构组成。 一个 BITMAPFILEHEADER 后跟一个 BITMAPINFO,后跟一个字节数组。
使用 Win32 加载 BMP 文件的最简单方法是调用 CreateFile、GetFileSize、ReadFile 和 CloseHandle 将文件图像加载到内存中,然后将指向缓冲区的指针转换为 BITMAPFILEHEADER 并从那里开始。
我撒谎,更简单的方法是调用LoadImage。 确保传递 LR_DIBSECTION 标志以确保 GDI 不会将加载的图像转换为主显示器配置的任何位深度。 这样做的好处是可以获得 HBITMAP,您可以将其选择到 DC 中,从而使用 GDI 进行绘制。
然而,要保存它,没有捷径。 您需要准备一个 BITMAPFILEHEADER,将其写出,填充 BITMAPINFO 结构,将其写出,然后是实际的像素数据。
A BMP file consists of 3 structures. A BITMAPFILEHEADER followed by a BITMAPINFO followed by an array of bytes.
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.
I lie, a simpler way is to call 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. This has the advantage of getting you a HBITMAP that you can select into a DC and therefore draw all over using 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.
我尝试了上面的 CImage,但我有一个充满像素值的 C 数组,我只想将其转储为 BMP(或任何其他格式)。
CImage
没有相应的构造函数,我不想链接 MFC(用于CBitmap
),也不想尝试理解 IWIC。简单的是 CImg:
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
has no constructor for that, and I did not want to link MFC (forCBitmap
) nor try to fathom IWIC.What was easy was CImg:
CBitmap 类执行 BMP I/O。
The CBitmap class does BMP I/O.
加载图片
LoadImage
我没有使用过 Magick++,但 Windows 有一个名为 Windows Imaging Component 的库,它可能会满足您的需求。
I've not used Magick++, but Windows has a library called the Windows Imaging Component which would likely suit your needs.