Accessing bytes of a .bmp image

Hi! I want to get an unsigned char array of bytes (and if possible, width and height)from a Bitmap image using "Windows.h" functions, like FindResource, LoadResource, etc.

I have successfully managed to load text files from these functions, they give me the bytes of the file. But I am having difficulty with Bitmaps, I have search and went to many forums but was unsuccessful. This is what I am trying to accomplish:

unsigned char* data = nullptr;

auto bitmap = LoadBitmap(...) //I have no idea what each of the arguments are..
data = //some function to get the bytes of the bitmap.
Do you have actual bitmap image files that you want to read, or are you trying to get images out of compiled windows resource binaries?
I am having difficulty with Bitmaps

What kind of BMP? How many bits per pixel? 1, 4, 8, or 24? With colortable?
Maybe you first apply https://www.irfanview.com as batch converter to transform your BMPs to a known, easy to handle format. Otherwise your routine has to be prepared for all ifs and buts.
I do have an actual bitmap which is 2 x 2 pixels. What kind of BMP? I have no idea, I just made a simple texture in paint.net and exported it as a BMP file.
You could maybe do something along these lines. I can't test this since I don't have windows at the moment. Maybe someone else can fix this up.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <vector>
#include <windows.h>

int main() {

    // Load the bitmap from a file
    HANDLE hbm = LoadImage(NULL, "filename", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);

    // Get it's size
    BITMAP bm; 
    GetObject(hbm, sizeof(BITMAP), &bm);
    int columns = bm.bmWidth;
    int rows = bm.bmHeight;

    // Select it into an hdc
    HDC hdc = CreateCompatibleDC(NULL);  // create dc compatible with screen
    HANDLE hbmOld = SelectObject(hdc, hbm);

    // Copy pixels
    std::vector<std::vector<COLORREF>> data(rows, std::vector<COLORREF>(columns));
    for (int row = 0; row < rows; ++row)
        for (int col = 0; col < columns; ++col)
            data[row][col] = GetPixel(hdc, col, row);

    SelectObject(hdc, hbmOld);
    DeleteDC(hdc);


    // ...


    DeleteObject(hbm);
}

Last edited on
What kind of BMP? I have no idea

Your 2x2 BMP of 24 bit colour depth has the size of 70 bytes? The pixel take 2x2 * 24/8 = 12 bytes. Look at it with a hex editor of your choice and find out what the rest could be, the legend you find here: https://en.wikipedia.org/wiki/BMP_file_format
Thank you so much dutch! I will be sure to check out the website MikeStgt :)
One more word. I just had a look to the source of Emu42, https://hp.giesselink.com/emu42.htm -- file FILES.C in E42S122.zip there, it is C not C++ but may give an impression of all the ifs and buts to respect when using an image as faceplate for an emulator. Even without optional GIF and PNG the different BMP formats are some kind of drudgery.

So I would suggest to use a lib you trust like the one Ganado mentioned or copy what is free to use before you code&debug home-made denouements.
Topic archived. No new replies allowed.