views:

5480

answers:

5

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?

+1  A: 

I've not used Magick++, but Windows has a library called the Windows Imaging Component which would likely suit your needs.

+2  A: 

The CBitmap class does BMP I/O.

Mr Fooz
+4  A: 

When developing just for Windows I usually just use the ATL CImage class

Gerald
I think CImage is part of the shared MFC/ATL classes - which may be a bit easier to include then the CBitmap class, which requires MFC.
Aardvark
Yes, you just need to include atlimage.h
Gerald
what if you're not targeting only windows?
JohnIdol
I have a custom bitmap class that I use when I am developing apps for other platforms or where I need more flexibility. The BMP format is relatively simple. There is also an open source library (BSD license) called EasyBMP on SourceForge if you need something cross-platform.
Gerald
+2  A: 
#include <windows.h>

LoadImage

oh no, i mean to say include (sharp) "windows.h"but part of my comment is missing
I fixed your post and added link. Hope you don't mind.
eed3si9n
+2  A: 

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.

Chris Becke