0

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*7
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); 
laurent
  • 88,262
  • 77
  • 290
  • 428
yyy
  • 13
  • 2
  • 5
  • Well, it's not clear why it's failing. I think your question should really be, "why is CreateDIBSection failing?". If you can get an answer to that then you should be able to solve your real problem. – David Heffernan Jan 18 '11 at 09:59
  • ok, I have changed it. I think the reail problem is really CreateDIBSection fails when nMemSize change to certian number. sometimes it fails with say 10000, but when u change nMemsize to a larger 20000 it succesed. – yyy Jan 18 '11 at 10:15
  • @yyy: Post all fields of `bmpinfo.bmiHeader`. What values you assign to them? – Nawaz Jan 18 '11 at 10:21
  • Please provide the complete code. It would be much easier for us to tell (or guess) where the problem might be. And why is it `* 7` in the `nMemSize` equation? – detunized Jan 18 '11 at 10:23
  • sorry i just edited. `*3` for RGB bytes, `*7` is for 7 times the screen size. i am making a large horizontal panorama screen. – yyy Jan 18 '11 at 10:46
  • Works fine with my windows 7 machine. Could it actually be another API function failing? Have you tried calling GetLastError after other API calls? – MW_dev Jan 18 '11 at 11:32
  • Is `nScreenWidth` always evenly divisible by 4? – Mark Ransom Feb 20 '12 at 13:37

1 Answers1

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();      
MW_dev
  • 2,146
  • 1
  • 26
  • 40