开发者

why CreateDIBSection() fails with certian BITMAPINFO?

I am trying to use CreateDIBSection.

Problem:

In Windows XP, I tried to call CreateDIBSection, it returns NULL and GetLastError = 0

When I try to change the screen resolution, for example to 2048 x 1536, it returns correct value.

I have tested this function has some relationship with nMemSize (not necessarily small number).

Question:

Is there any guarantee way to ensure CreateDIBSection returns correct value?

nScreenWidth = 1024;
nScreenHeight= 768;
 = nScreenWidth*nScreenHeight*3*开发者_如何学C7
HDC hdc = ::GetDC(hWnd);
m_hBmpMapFile = ::CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, nMemSize, NULL);

BITMAPINFO bmpInfo = {0};
bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfo.bmiHeader.biWidth = nScreenWidth;
bmpInfo.bmiHeader.biHeight = nScreenHeight;
bmpInfo.bmiHeader.biPlanes = 1;
bmpInfo.bmiHeader.biBitCount = 24;
bmpInfo.bmiHeader.biCompression = 0;
bmpInfo.bmiHeader.biSizeImage = nMemSize;
bmpInfo.bmiHeader.biXPelsPerMeter = 0;
bmpInfo.bmiHeader.biYPelsPerMeter = 0;
bmpInfo.bmiHeader.biClrUsed = 0;
bmpInfo.bmiHeader.biClrImportant = 0;
bmpInfo.bmiColors[0].rgbBlue = 204;
bmpInfo.bmiColors[0].rgbGreen = 204;
bmpInfo.bmiColors[0].rgbRed = 204;
bmpInfo.bmiColors[0].rgbReserved = 0;
PVOID pvBits;

m_hBmpAllWstDskWallpaper = ::CreateDIBSection(hdc, &bmpInfo, DIB_RGB_COLORS, &pvBits, m_hBmpMapFile, 0); 


I suspect the problem might be contained in the section of code that you did not include (in the elipses ...). So I recommend:

  • Check your Device Context is valid
  • ZeroMemory
  • Add the structure size
  • And bitmap dimensions
  • Move the GetLastError calls to ensure that the Device Context is valid (perhaps an earlier API call fails)

The code below seems to work after I added the recommendations above, I hope it helps:

        HDC hdc = ::GetDC(hWnd); 
        int nScreenWidth = 1024; 
        int nScreenHeight= 768; 
        int nMemSize = nScreenWidth*nScreenHeight*3*7;
        HANDLE m_hBmpMapFile = ::CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, nMemSize, NULL); 
        BITMAPINFO bmpInfo; 
        //clear the memory
        ZeroMemory(&bmpInfo.bmiHeader, sizeof(BITMAPINFOHEADER));
        //struct size
        bmpInfo.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
        //dimensions
        bmpInfo.bmiHeader.biWidth = nScreenWidth;
        bmpInfo.bmiHeader.biHeight = nScreenHeight;
        bmpInfo.bmiHeader.biPlanes = 1; 
        bmpInfo.bmiHeader.biBitCount = 32; 
        bmpInfo.bmiHeader.biSizeImage=nMemSize; 
        void *pvBits = NULL;
        HANDLE m_hBmpAllWstDskWallpaper = ::CreateDIBSection(hdc, &bmpInfo, DIB_RGB_COLORS, &pvBits, m_hBmpMapFile, 0);
        int nError = ::GetLastError();      
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜