Quickest way to get dimensions of BITMAP file in C


What’s the quickest way to get the height and width of a bitmap file in C? I either need to get it from the HBITMAP or the file itself, either way will work. Here’s a section of my code:

    printf("Initializing textures...");
    HDC bp, memdc;
    HBITMAP bmp, hold;
    bp = GetDC(hwnd);
    memdc = CreateCompatibleDC(bp);
    bmp = (HBITMAP)LoadImage(NULL, tex_name, IMAGE_BITMAP, 0,0, LR_LOADFROMFILE);
    hold = (HBITMAP) SelectObject(memdc, bmp);
    for(int i = 0; i < 150; i++){
        for(int j = 0; j < 150; j++){
            COLORREF co = GetPixel(memdc, j, i);
            texturemap[i][j] = (COLORREF) (GetRValue(co)<<16)|(GetGValue(co)<<8)|GetBValue(co);
    SelectObject(memdc, hold);

It loads the pixel data from a file and then stores it into a 2d array. I would like to replace the 150 in each loop with the height followed by the width, but I have no clue how to get them.

(Also, yes I have already looked for existing answers.)

>Solution :

You could use GetObject to fetch the information, which is written to a BITMAP structure:

bmp = (HBITMAP)LoadImage(NULL, tex_name, IMAGE_BITMAP, 0,0, LR_LOADFROMFILE);

GetObject(bmp, (int) sizeof bm, &bm);

for(LONG i = 0; i < bm.bmHeight; i++){
    for(LONG j = 0; j < bm.bmWidth; j++){
        // ...

Leave a ReplyCancel reply