Solved

From DIB to Bitmap, FAST

Posted on 2000-03-30
7
878 Views
Last Modified: 2013-11-20
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.
0
Comment
Question by:gaggio
  • 3
  • 3
7 Comments
 
LVL 8

Expert Comment

by:gelbert
ID: 2670851
See "Bit2Mono.exe Changes Color Bitmaps to Monochrome"
MSDN ID: Q77282
It has sample of convertion from DIB to bitmap
0
 
LVL 23

Expert Comment

by:chensu
ID: 2671543
0
 
LVL 1

Author Comment

by:gaggio
ID: 2673400
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?
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.

 
LVL 23

Expert Comment

by:chensu
ID: 2673827
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?
0
 
LVL 1

Author Comment

by:gaggio
ID: 2673835
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.
0
 
LVL 23

Accepted Solution

by:
chensu earned 60 total points
ID: 2673870
If you look into the DIBToDDB function, you can see the following line at the beginning of the function.

lpbi = (LPBITMAPINFOHEADER)hDIB;

It means that the function supposes hDIB is a pointer instead of a memory handle. You use GlobalAlloc to allocate a memory object and it returns a handle. In order to use the memory, you use GlobalLock to lock the memory object and it returns a pointer to the first byte of the object's memory block. After using, you use the GlobalUnlock function to unlock it. You may also specify the GMEM_FIXED or GPTR flags when using GlobalAlloc so that it returns a pointer directly.
0
 
LVL 1

Author Comment

by:gaggio
ID: 2673918
Adjusted points from 50 to 60
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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 video discusses moving either the default database or any database to a new volume.

759 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

20 Experts available now in Live!

Get 1:1 Help Now