Solved

From DIB to Bitmap, FAST

Posted on 2000-03-30
7
888 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
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.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to create frequencies of a variable from SAS dataset? 10 133
Include multiple hostnames in this command? 3 55
WinWaitActive parameters 12 31
viewing source code from eclipse 13 110
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.

860 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