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

后端 未结 7 1633
囚心锁ツ
囚心锁ツ 2020-12-03 12:16

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++ b

相关标签:
7条回答
  • 2020-12-03 12:16

    The CBitmap class does BMP I/O.

    0 讨论(0)
  • 2020-12-03 12:17

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

    0 讨论(0)
  • 2020-12-03 12:18

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

    0 讨论(0)
  • 2020-12-03 12:20

    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.

    0 讨论(0)
  • 2020-12-03 12:22

    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.

    What was easy was 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");
    }
    
    0 讨论(0)
  • 2020-12-03 12:27

    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.

    0 讨论(0)
提交回复
热议问题