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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:38:51  来源:igfitidea点击:

C++: What's the simplest way to read and write BMP files using C++ on Windows?

c++windowswinapiimage-manipulationbmp

提问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

When developing just for Windows I usually just use the ATL CImageclass

在为 Windows 开发时,我通常只使用 ATL CImage

回答by sEver

EasyBMPif you want just bmp support. I'ts simple enough to start using within minutes, and it's multiplatform if you would need that.

EasyBMP如果您只需要 bmp 支持。我不够简单,可以在几分钟内开始使用,如果您需要,它是多平台的。

回答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 Mr Fooz

The CBitmapclass does BMP I/O.

关于CBitmap类不BMP I / O。

回答by Mike C

I tried CImageas above, but I had a C array full of pixel values that I simply wanted to dump as a BMP (or any other format). CImagehas 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 user27732

#include <windows.h>

LoadImage

加载图像

回答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 的库,它可能会满足您的需求。