Solved

GetDibBits doesn't work!!!

Posted on 1998-12-17
14
623 Views
Last Modified: 2013-11-20
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 ?
0
Comment
Question by:eugeneng
  • 6
  • 5
  • 3
14 Comments
 
LVL 6

Expert Comment

by:snoegler
Comment Utility
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?
0
 

Author Comment

by:eugeneng
Comment Utility
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){
       DeleteObject(hBitmap);
       hBitmap= NULL;
   }

            
LPBITMAPINFO lpbi;

       lpbi = (LPBITMAPINFO) new BYTE[sizeof(BITMAPINFOHEADER)+
               (65*sizeof(RGBQUAD))];

      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
      memcpy(&lpbi->bmiColors,pColorTable,sizeof(RGBQUAD)*65);
      CDC *ScreenDC = GetDC(NULL);

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

      if (hBitmap == NULL)
            return FALSE;

      //initialize bitmap bits
      BYTE * pByte = new BYTE[80*80];
      memset(pByte,0,80*80);
      SetBitmapBits(hBitmap,80*80,pByte);
      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,
                        h,8,&pBuf,
                       (LPBITMAPINFO)&bmpInfo,
                        DIB_RGB_COLORS);

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

      MyUpdateBits(pBuf,pnt.y);

        //set the bits back to hBitmap
      err = SetDIBits(pDC->GetSafeHdc(),hBitmap,
                        h,8,pBuf,(LPBITMAPINFO)lpbi,
                        DIB_RGB_COLORS);

      if (!err)
            err = GetLastError();


      ReleaseDC(pDC);
      delete [] (BYTE*) lpbi;
      delete [] (BYTE*) pBuf;
        return;
}

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.


0
 
LVL 6

Expert Comment

by:snoegler
Comment Utility
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
you.

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.
0
 
LVL 6

Expert Comment

by:snoegler
Comment Utility
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.
0
 

Author Comment

by:eugeneng
Comment Utility
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 ?
0
 
LVL 6

Expert Comment

by:snoegler
Comment Utility
I don't know any ;)
0
 
LVL 2

Expert Comment

by:zyqwert
Comment Utility
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?


0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:eugeneng
Comment Utility
but, after i modified the bits, do i need to rebuild the HBITMAP ?
0
 
LVL 2

Expert Comment

by:zyqwert
Comment Utility
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
function.

0
 

Author Comment

by:eugeneng
Comment Utility
thanx a lots zyqwert, you are my lifesaver man, I've tested it and it works perfectly, thanx a lots
0
 
LVL 2

Accepted Solution

by:
zyqwert earned 200 total points
Comment Utility
You're welcome eugeneng -- can we close this case?
0
 

Author Comment

by:eugeneng
Comment Utility
yape, the has been closed.
0
 
LVL 6

Expert Comment

by:snoegler
Comment Utility
eugeneng, how about giving the points to zyqwert?
I think he solved your problem - so you should award him his
points :)
0
 

Author Comment

by:eugeneng
Comment Utility
oops..sorry, i've forgotten, thanx for reminding me.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Excel Use VBA to get user's Mac Address for their computer 5 81
countEvens challenge 2 55
tripleUp challenge 7 65
Path of Workbook 3 44
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now