• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 727
  • Last Modified:

GetDibBits doesn't work!!!

Hi :
  actually the GetDibBits() function work, but after i get the bits, the window lost its thread, the memory seem has been moved and the control can't get back to the calling function, why ? the addresses are all ??? ??? ???, can anybody tell me why ?
  • 6
  • 5
  • 3
1 Solution
Sounds like you are overwriting the stack frame or the process data ...
Where do you store the DibBits? Could you give a short code sample of what you're doing?
eugenengAuthor Commented:
first of all, snoegler, thanx for your response,
I create a DIB hBitmap as below function ;

BOOL CreateMapDIB(HBITMAPA hBitmap,RGBQUAD *pColorTable[65])
   if (hBitmap){
       hBitmap= NULL;



      lpbi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
      lpbi->bmiHeader.biWidth = 80;
      lpbi->bmiHeader.biHeight = 80;
      lpbi->bmiHeader.biPlanes = 1;
      lpbi->bmiHeader.biBitCount = 8;
      lpbi->bmiHeader.biCompression = BI_RGB;
      lpbi->bmiHeader.biSizeImage = 0;
      lpbi->bmiHeader.biXPelsPerMeter = 0;
      lpbi->bmiHeader.biYPelsPerMeter = 0;
      lpbi->bmiHeader.biClrUsed = 65;
      lpbi->bmiHeader.biClrImportant = 0;

      int buf=0;
      //fill bk palette color
      CDC *ScreenDC = GetDC(NULL);

      hBitmap =CreateDIBSection(ScreenDC->m_hDC,
      delete [](BYTE *)lpbi;

      if (hBitmap == NULL)
            return FALSE;

      //initialize bitmap bits
      BYTE * pByte = new BYTE[80*80];
      delete [] (BYTE*) pByte;

return TRUE;

ok, i've created the hBitmap successfully, at sometime, i want to change a particular part of bit data in the hBitmap, but there is not function like SetDIBitsToDevice() where i can specify a rectangle and update the bits data, so i have to use GetDIBits() to get the entire bits data from hBitmap and update the entire scanline at a time, so i do like :

void MyClass::PenTile(CPoint pnt)
    //assume size of hBitmap is 80*80
   // if size is huge, this statement might take cause delay.
     BYTE *pBuf= new BYTE[80*80];

    int h,w;
    BITMAPINFO bmpInfo;
    bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bmpInfo.bmiHeader.biWidth = 80;
    bmpInfo.bmiHeader.biHeight = 80;
    bmpInfo.bmiHeader.biPlanes = 1;
    bmpInfo.bmiHeader.biBitCount = 8;
    bmpInfo.bmiHeader.biCompression = BI_RGB;

    CDC *pDC = GetDC();
    int h;
    int x,y;

    x = pnt.x; //i do this becaue i don't know why after
    y = pnt.y;//executed GetDIBits, value of pnt will be modified

    h = 80-pnt.y; //because bitmap will be drawn bottom-up right?
      err = GetDIBits(NULL,hBitmap,

      if (!err)
            err = GetLastError();
        pnt.x = x;
        pnt.y = y;


        //set the bits back to hBitmap
      err = SetDIBits(pDC->GetSafeHdc(),hBitmap,

      if (!err)
            err = GetLastError();

      delete [] (BYTE*) lpbi;
      delete [] (BYTE*) pBuf;

ok. this is the function that i want to update bits data of dib bitmap. My problem is :

    1. how can i update a certain part of bitmap without have to get the entire bits data of bitmap ? like SetDIBitsToDevice where i can specify a rectangle that need to be updated. Because when the bitmap grows to huge size, it will take some time to allocate memory for pBuf that will be used to receive the bits data.
   2. Don't know why in the above code, the value of function argument "pnt" will be modified after executed GetDIBits();

  Thanx for you help.

I know some ways to do this, but neither of them is really convenient :)
First of all, you could deal with the DIB low-level, meaning to keep all data in your application,
and creating the real DIB only if needed (passing the pDibBits parameter to CreateDIBitmap).
As CreateDIBitmap uses your memory without modification, this could solve (1).

Another possibility is to use DirectX using memory surfaces. But i think this isn't a solution for

Then, there is a CImage class which allows to modify the bits directly. You can download it
at www.visualc.com. It has several high-level and low-level methods to work with bitmap data.
Maybe you take a look at it.
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

BTW: 'pnt' is overwritten due a stack frame overflow. The size of the array [80*80] is too small.
Maybe you're using 16- or 24 Bit DIBs? If yes, then the size would be 80*80*3 ...
Fact is, the array is too small - the memory 'behind' the array is overwritten, and this is why
your application will produce errors. When the 'return pointer' is overwritten, then the whole
application will crash. Try increasing the size of the array.
eugenengAuthor Commented:
thanx a lot man, i haven't try yet, but it seems like helpful. i'll get back latter. But isn't there a way can modify the dib bits directly ?
I don't know any ;)
Yes, you can modify the bits directly.
When you call CreateDibSection, fill in the 4th parameter and you'll have a pointer to a pointer to the bits.  Then you can change them whenever you like.
You don't need GetDIBits or SetDIBits.
Does this help?

eugenengAuthor Commented:
but, after i modified the bits, do i need to rebuild the HBITMAP ?
Not at all.  A Dib Section bitmap, unlike a regular bitmap from CreateBitmap, you can change by writing directly to the bits.  The change won't manifest itself on screen until you do a BitBlt from a memory DC that has the hBitmap selected into it.  Try it, you'll see.

From the documentation at msdn.microsoft.com:
Windows NT: You need to guarantee that the GDI subsystem has completed any drawing to a bitmap created by CreateDIBSection
before you draw to the bitmap yourself. Access to the bitmap must be synchronized. Do this by calling the GdiFlush

eugenengAuthor Commented:
thanx a lots zyqwert, you are my lifesaver man, I've tested it and it works perfectly, thanx a lots
You're welcome eugeneng -- can we close this case?
eugenengAuthor Commented:
yape, the has been closed.
eugeneng, how about giving the points to zyqwert?
I think he solved your problem - so you should award him his
points :)
eugenengAuthor Commented:
oops..sorry, i've forgotten, thanx for reminding me.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 6
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now