Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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);
    DeleteObject(bmp);
    DeleteDC(bp);
    system("cls");

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.)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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);

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

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading