Thread: CreateDIBSection returns NULL.

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    25

    CreateDIBSection returns NULL.

    Hi,
    please help me to resolve the error of createDIBSection returns NULL.
    Given below the code before calling the
    createDIBSection .
    I tried to creates a DIB using CreateDIBSection but it returns NULL. GetLastError() returns 0. I could not find out what could be the error.
    I think the size of deviceContext is high.I am new to this DIBSection function and not sure how internal function works
    Code:
    
    CDC * pSourceDC = AfxGetMainWnd()->GetDC();
    
    
    BYTE * pDIBbuffer = new BYTE[sizeof(BITMAPINFOHEADER) + 256*sizeof(WORD)];
    if (pDIBbuffer == NULL) {  throw; }
    
    BITMAPINFO * pDIB = (BITMAPINFO *)pDIBbuffer;
    
    
    pDIB->bmiHeader.biSize= sizeof(BITMAPINFOHEADER);
    pDIB->bmiHeader.biWidth= m_totalWidth;
    pDIB->bmiHeader.biHeight= m_totalHeight;
    pDIB->bmiHeader.biPlanes= 1;
    pDIB->bmiHeader.biBitCount= 24;          // MUST be 24-bit DIB for JPEG 
    pDIB->bmiHeader.biCompression= BI_RGB;
    pDIB->bmiHeader.biSizeImage= 0;
    pDIB->bmiHeader.biXPelsPerMeter= 0;
    pDIB->bmiHeader.biYPelsPerMeter= 0;
    pDIB->bmiHeader.biClrUsed= 0;
    pDIB->bmiHeader.biClrImportant= 0;
    WORD* color = (WORD*) pDIB->bmiColors;
    
    for (i = 0; i < 256; i++)
    {
    *(color++) = i;
    }
        ColorPalette* colorPal = ColorPalette::getInstance();
    CPalette* pOldPal = pSourceDC->SelectPalette( &(colorPal->GetWindowPalette()), FALSE);
    pSourceDC->RealizePalette();
       HBITMAP hBitmap = CreateDIBSection(pSourceDC->GetSafeHdc(),
    pDIB,
    DIB_PAL_COLORS,
    (void **)&pvBits,// Here's where we put the image
    NULL,
    0);
    

    Thank you



    Last edited by SAP_7; 08-21-2017 at 11:14 PM.

  2. #2
    Registered User
    Join Date
    Nov 2016
    Posts
    25
    Could any of you reply to this since i need to complete this quickly. Despite of being tried for 2 days , I could not understand/find out where is the error

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    24-bit bitmaps don't have a color table.
    Either set biBitCount to 8 or get rid of the color table (set bmiColors to NULL).

  4. #4
    Registered User
    Join Date
    Nov 2016
    Posts
    25
    Quote Originally Posted by algorism View Post
    24-bit bitmaps don't have a color table.
    Either set biBitCount to 8 or get rid of the color table (set bmiColors to NULL).
    Thank you algorism.
    I attempted the both ways.It still returns the NULL.

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    "Attempted the both ways" suggests you're not really thinking.
    You have to decide which way you want it.
    Let's assume you want a 24-bit bitmap, which is the common case.
    So get rid of all of the palette crap.

    You also need to change DIB_PAL_COLORS to DIB_RGB_COLORS

    You haven't bothered to show how you define pvBits. I wager it's wrong, too, since the rows need to be a multiple of 4.

    Try something like this. (I haven't tested it since I don't run Windows anymore.)
    Code:
    CDC *pSourceDC = AfxGetMainWnd()->GetDC();
    
    BITMAPINFO *pDIB = new BITMAPINFO;
    ZeroMemory(pDIB, sizeof(BITMAPINFO));
    pDIB->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    pDIB->bmiHeader.biWidth = m_totalWidth;
    pDIB->bmiHeader.biHeight = m_totalHeight;
    pDIB->bmiHeader.biPlanes = 1;
    pDIB->bmiHeader.biBitCount = 24;
    pDIB->bmiHeader.biCompression = BI_RGB;
    
    int stride = 3 * m_totalWidth; // 3 bytes per pixel (24-bits)
    stride = (stride + 3) & ~3;    // rows are rounded to a multiple of 4
    
    BYTE* bits = new BYTE[stride * m_totalHeight];
    
    HBITMAP hBitmap = CreateDIBSection(
        pSourceDC->GetSafeHdc(),
        pDIB,
        DIB_RGB_COLORS,
        (VOID**)&bits,
        NULL,
        0);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strstr() returns null
    By Jedd Lim in forum C Programming
    Replies: 6
    Last Post: 09-05-2015, 05:55 AM
  2. fgets always returns null
    By ido in forum C Programming
    Replies: 0
    Last Post: 04-25-2015, 06:29 AM
  3. mysql_use_result() returns NULL
    By mirekgn in forum C Programming
    Replies: 5
    Last Post: 03-28-2010, 01:05 PM
  4. CreateDIBSection() returning NULL
    By arjunajay in forum Windows Programming
    Replies: 5
    Last Post: 07-26-2006, 09:54 AM
  5. Function returns null value instead of zero
    By drdepoy in forum C Programming
    Replies: 3
    Last Post: 10-23-2005, 03:51 PM

Tags for this Thread