Search code examples
c++bmp

How to put bmp file into array c++ c


how can I put the file.bmp to array using standard lib c++ or c whithout windows.h ect

edit I want to get bmp colors to array change the array and put to bmp using standard lib


Solution

  • Use a binary stream:

    #include <fstream>
    
    char buffer[100];
    ifstream myFile ("myImage.bmp", ios::in | ios::binary);
    myFile.read (buffer, 100);
    
    myFile.close();
    

    However, parsing it will be a bit trickier if you insist on doing it that way. I suggest you look into a generic C++ image library, such as the boost GIL (doesn't support bmp) or this open source bmp library.

    If you are unable to use any third party or OS-specific libraries, you will have to parse the data yourself. The C++ standard libraries don't include anything to do that for you. You'll have to start by familiarising yourself with the BMP structure. Wikipedia's article has a good description of it.