Link to home
Start Free TrialLog in
Avatar of gaggio
gaggio

asked on

From DIB to Bitmap, FAST

My problem is very clear: I have an HDIB handle to a device independent bitmap.I want to be able to get an HBITMAP handle to a BITMAP object from it.
Until now, my only solution was to do the following:
- create a Memory DC compatible with the display                 memDC.CreateCompatibleDC(NULL);
- select a bitmap object that has the properties we want
      memDC.SelectObject(anOkBitmap);
- create a bitmap object compatible with the current one:
                 bitmap.CreateCompatibleBitmap(&memDC,xsize,ysize);
- select it into memDC
       memDC.SelectObject(&bitmap);
- copy PIXEL by PIXEL the DIB object from my DIB class (which has a GetPixel() function):
      for(int j=0;j<ysize;j++)
            for(int i=0;i<xsize;i++)
            {
                  COLORREF value;
                  mydibimage.GetPixel(i,j,value);
                  memDC.SetPixel(i,j,value);
            }

Well, you can see that it is not very good, especially not FAST.
I'd like to get the the bitmap filled with the DIB bits directly, with some
kind of bitblt function.

SO: how do I get a bitmap from an HDIB?

I need code because I'm late in my project.
Thanks a lot.
Avatar of gelbert
gelbert

See "Bit2Mono.exe Changes Color Bitmaps to Monochrome"
MSDN ID: Q77282
It has sample of convertion from DIB to bitmap
Avatar of gaggio

ASKER

I'm sorry, but this function does not work for me, Chensu. Or maybe that I don't understand what I should do...
Here's what I tried:

1)HBITMAP hb = DIBToDDB(mydibimage.m_hDib);
 CBitmap* TheBitmap = CBitmap::FromHandle(hb);

2)CBitmap b1;
    b1.LoadBitmap(IDB_BITMAP1);
    CBitmap* TheBitmap = &b1;

and then:

CClientDC dc(this); // I want to display on the dialog
CDC memDC;
memDC.CreateCompatibleDC(NULL);
memDC.SelectObject(TheBitmap);
memDC.TextOut(0,0,"TEST");
dc.BitBlt(0,0,dibim.Width(),dibim.Height(),&memDC,0,0,SRCCOPY);


.... and in the first case I see nothing, whereas in the second case I see what I should (the bitmap and the text)

What's wrong?
Is hb a valid bitmap handle? Is your mydibimage.m_hDib a memory handle that the DIBToDDB function expects? Have you tried to debug into the DIBToDDB function?
Avatar of gaggio

ASKER

My bad.
I had to use a GlobalLock() on the m_hDib of my image class.
If you can you explain me the purpose of GlobalLock(), you'll get 10 extra points.
If you don't want to, just post a comment anyway and you'll get those 50.
ASKER CERTIFIED SOLUTION
Avatar of chensu
chensu
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gaggio

ASKER

Adjusted points from 50 to 60