Solved

From DIB to Bitmap, FAST

Posted on 2000-03-30
7
886 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sameEnds challenge 3 161
isEverywhere  challenge 19 78
building a service to accessed via telnet client, want colors ? 4 89
Work with App store 7 53
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.

803 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