最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++ - Display of a bitmap image, GetObject appears to incorrectly size some parameters in BITMAP structure - Stack Overflow

programmeradmin0浏览0评论

My question relates to a task I originally thought would be simple. I now find there are many details that were not first considered. My original goal was to draw an image of a flag in a dialog window. For simplicity I started just to display it in the main window. I started by finding a image, resizing it and storing it with my intended color depth. Bitmap / .BMP file seemed simple, that is what I intended to use. In my case four bit/sixteen color was selected to reduce the file size. The actual size in the file is 600 wide by 300 high. This gives a file with 90K pixel data plus the header which in my case is 118 bytes. As I am just learning windows API, I decided to do some initial tests. First I load the image using the following sequence... I removed the error checking for clarity:

hFlag = LoadImage(GetModuleHandle(NULL), wcImageFile, 
        IMAGE_BITMAP, 600, 300, LR_DEFAULTCOLOR | LR_LOADFROMFILE);

GetObject(hFlag, sizeof(BITMAP), &bitmap);

The content of bitmap is not what I expect after running this short sequence. The result for bmType, bmWidth, bmHeight, bmWidthBytes, bmPlanes are all as I expect. The bmBitsPixel however are 0x20 or 32. bmBits is a null pointer.

Something isn't right... LoadImage returns a handle to my bitmap without error. GetObject returns an int of 0x18 which is the size of the BITMAP structure it is filling. All as expected. Yet the bits per pixel which I expect to be 4 returns as 32. In a case of 32 bits/pixel the array size of pixel data will be short by eight times. This would likely be discovered and the null pointer generated.

Interesting also that if I later run:

SelectObject(hdcMem, hFlag);
BitBlt(hdc, 0, 0, 600, 300, hdcMem, 0, 0, SRCCOPY);

The flag is then displayed. I hope someone can help me make sense of this. I am working through Petzold however it's a big book.

Minimal example code...

case WM_CREATE:
    {
        const wchar_t   *wImageFile;
        wImageFile =    L"C:\\Users\\Tom\\Pictures\\Flag\\Flag.bmp";
        DWORD           dwError = 0;

        hFlag = (HBITMAP) LoadImage(GetModuleHandle(NULL), wImageFile, IMAGE_BITMAP, 600, 300, LR_DEFAULTCOLOR | LR_LOADFROMFILE | LR_CREATEDIBSECTION);
        if(hFlag == 0)
            dwError = GetLastError();
        int nWhatsit = GetObject(hFlag, sizeof(BITMAP), &bitmap);
        return (INT_PTR)FALSE;
    }

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论