C++:在 Windows 上使用 C++ 读写 BMP 文件的最简单方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/199403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++: What's the simplest way to read and write BMP files using C++ on Windows?
提问by Eugene Yokota
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 ImageMagickand 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.
我想加载一个 BMP 文件,在内存中对其进行一些操作,然后在 Windows(Win32 原生)上使用 C++ 输出一个新的 BMP 文件。我知道ImageMagick并且它是 C++ 绑定Magick++,但我认为这对这个项目来说太过分了,因为我目前对其他文件格式或平台不感兴趣。
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."
就读取和写入 BMP 文件的代码设置而言,最简单的方法是什么?答案可能是“只需使用 Magick++,它是最简单的。”
Related Question: What is the best image manipulation library?
相关问题:最好的图像处理库是什么?
采纳答案by Gerald
回答by sEver
回答by Chris Becke
A BMP file consists of 3 structures. A BITMAPFILEHEADER followed by a BITMAPINFO followed by an array of bytes.
BMP 文件由 3 个结构组成。一个 BITMAPFILEHEADER 后跟一个 BITMAPPINFO 后跟一个字节数组。
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. 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.
我撒谎,更简单的方法是调用 LoadImage。确保传递 LR_DIBSECTION 标志以确保 GDI 不会将加载的图像转换为您的主显示器配置的任何位深度。这具有为您提供 HBITMAP 的优势,您可以将其选择到 DC 中,从而使用 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 结构,写出来,然后是实际的像素数据。
回答by Mike C
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 (for CBitmap
) nor try to fathom IWIC.
我CImage
如上尝试,但我有一个充满像素值的 C 数组,我只是想将其转储为 BMP(或任何其他格式)。 CImage
没有构造函数,我不想链接 MFC (for 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");
}
回答by Tron
I've not used Magick++, but Windows has a library called the Windows Imaging Component which would likely suit your needs.
我没有使用过 Magick++,但 Windows 有一个名为 Windows Imaging Component 的库,它可能会满足您的需求。