CreateDIBSection returns NULL
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: CreateDIBSection returns NULL

  1. #1
    Join Date
    Jun 2017
    Posts
    7

    Question 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.

    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);
    Here hBitmap 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
    I am capturing the application screenshot in different page.In one page,it returns NULL.Please help me on this.

    Thank you
    Last edited by 2kaud; August 22nd, 2017 at 02:40 AM. Reason: Added code tags

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    18,956

    Re: CreateDIBSection returns NULL

    What is the siye of a bitmap?
    What do the GetDC(), SelectPalette() return?
    Victor Nijegorodov

Tags for this Thread

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)