CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    sunnysky Guest

    Why is CreateDIBSection() Failing in My Code?

    Hello, below is a block of my code. I don't understand why CreateDIBSection() returns NULL. Thanks a lot in advance.
    Code:
    pPackedDib = (BITMAPINFO *)(malloc (sizeof(BITMAPINFOHEADER) + 256*sizeof(WORD)));
    pPackedDib->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    pPackedDib->bmiHeader.biWidth = 300;
    pPackedDib->bmiHeader.biHeight = 450;
    pPackedDib->bmiHeader.biPlanes = 1;
    pPackedDib->bmiHeader.biBitCount = 8;          // max 256 colors supported
    pPackedDib->bmiHeader.biCompression = BI_RGB;
    pPackedDib->bmiHeader.biSizeImage = 0;
    pPackedDib->bmiHeader.biXPelsPerMeter = 0;
    pPackedDib->bmiHeader.biYPelsPerMeter = 0;
    pPackedDib->bmiHeader.biClrUsed = 0;
    pPackedDib->bmiHeader.biClrImportant = 0;
    pPackedDib->bmiColors[0].rgbRed = 204;
    pPackedDib->bmiColors[0].rgbGreen = 204;
    pPackedDib->bmiColors[0].rgbBlue = 204;
    pPackedDib->bmiColors[0].rgbReserved = 0;
    
    if (pPackedDib)
        {
        // Create the DIB section from the DIB
            hBitmap = CreateDIBSection (NULL,
                                                   pPackedDib, 
                                                   DIB_PAL_COLORS,
                                                   &pBits, 
                                                   NULL, 0);
            ...
    
            free(pPackedDib);
        }

  2. #2
    sunnysky Guest

    Re: Why is CreateDIBSection() Failing in My Code?

    Ok. I found the reason. It's the parameters for CreateDIBSection(). Since the first paramter is NULL, I should use DIB_RGB_COLORS for the third parameter. Otherwise, I should provide something like GetDC(hwnd) as the first parameter when using DIB_PAL_COLORS.

  3. #3
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Why is CreateDIBSection() Failing in My Code?

    no, it fails because you pass NULL, Period. You can't create a dib section without a DC.

    You should also correctly fill in biSizeImage. (in this particular case it would be width*height)

    ALso. You say you want a 256 PAL (16bit) colors paletter, but then appear to be initializing the RGB (RGBQuad/32bit) form of the palette.

  4. #4
    sunnysky Guest

    Re: Why is CreateDIBSection() Failing in My Code?

    Hi OReubens,

    According to Petzold's book, we can create a dib section without a DC as long as we use DIB_RGB_COLORS.

    I think biBitCount = 8 specifies maximum numbers of the image to be 256 colors, but each of this color can still be 32bit, right?

    Thanks,
    Brian

  5. #5
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Why is CreateDIBSection() Failing in My Code?

    Are you sure about that ? There's a difference between passing NULL as DC and passing the return value of GetDC(NULL) as DC. (or the return from CreateCompatibleDC(NULL))

    it isn't documented on MSDN at least. So anyone making claims otherwise may just have noticed it behaving that way on a particular version of Windows, but that doesn't make it the truth for all versions that microsoft has ever made (or will ever make in the future).

    It's easy enough to provide a DC, so playing safe is definately better...


    beside, even if it were true... you're using DIB_PAL_COLORS in yoru code, that most definately DOES need a DC.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured