Solved

How to Create DDB with 32bit color DIB ?

Posted on 1998-08-30
4
566 Views
Last Modified: 2013-11-19
Dear experts

I'm using VC++5.0.
I'd like to create a DDB with a 32 bit color DIB.
I tried as followings :

/////
BITMAPINFOHEADER      BmpInfoHdr;

BmpInfoHdr.biSize = sizeof(BITMAPINFOHEADER);
BmpInfoHdr.biWidth = 720;
BmpInfoHdr.biHeight = (-1)*486;
BmpInfoHdr.biPlanes = 1;
BmpInfoHdr.biBitCount = 32;
BmpInfoHdr.biCompression = BI_RGB;
BmpInfoHdr.biSizeImage = 0;
BmpInfoHdr.biXPelsPerMeter = 0;
BmpInfoHdr.biYPelsPerMeter = 0;
BmpInfoHdr.biClrUsed = 0;
BmpInfoHdr.biClrImportant = 0;

CClientDC dc(this);
HDC      hDC = dc.GetSafeHdc();
HBITMAP hBitmap = CreateDIBitmap(hDC,
(LPBITMAPINFOHEADER) &BmpInfoHdr, (LONG) CBM_INIT,
(LPSTR) pFrameDIB, (BITMAPINFO *) &BmpInfoHdr, DIB_RGB_COLORS);
CBitmap* pFrameBmp = new CBitmap;
pFrameBmp->->CreateCompatibleBitmap(&dc, 720, 486);
pFrameBmp->FromHandle(hBitmap);

CDC* pMemDC = new CDC;
pMemDC->CreateCompatibleDC(&dc);
CBitmap* pOldBitmap = pMemDC->SelectObject(pFrameBmp);

dc.SetStretchBltMode(COLORONCOLOR);
dc.BitBlt(0, 0, 720, 486, pMemDC, 0, 0, SRCCOPY);
////////////
Above sample code doesn't display pFrameDIB which is 32 bit color image data. Only black is displayed.

Can anyone let me know how to solve this problem ?
i.e. how to create or set bitmap bits of DDB with 32 bit color image data ?

Best regards
0
Comment
Question by:okg
  • 2
4 Comments
 

Author Comment

by:okg
Comment Utility
Edited text of question
0
 

Author Comment

by:okg
Comment Utility
Edited text of question
0
 
LVL 23

Expert Comment

by:chensu
Comment Utility
The problem of your code is that there is no color palette created. Therefore, the color information is lost if the destination DC is not in 32-bit color mode. Look into the following KB article which I think is useful for you.

PARSEBIT.EXE Directly Accesses the Bits of a DIB Section
http://support.microsoft.com/support/kb/articles/q159/6/49.asp
0
 
LVL 1

Accepted Solution

by:
elf_k earned 200 total points
Comment Utility
The problem is in your code, some of lines is wrong.
Look at these lines:
// you create new bitmap object
CBitmap* pFrameBmp = new CBitmap;
// you create new bitmap handle 720x486 initialized to black.
pFrameBmp->CreateCompatibleBitmap(&dc, 720, 486);
// error resides here
pFrameBmp->FromHandle(hBitmap);

The problem is in understanding FromHandle() mechanism.
CBitmap::FromHandle() is static function of CBitmap. It creates new CBitmap object based on hBitmap you pass in it. So correct syntax is:
CBitmap* pNewBitmap=CBitmap::FromHandle(hBitmap).

So finally try to replace these three lines with:
CBitmap* pFrameBmp=CBitmap::FromHandle(hBitmap);

.and you will get a badly colored bitmap on the screenm if you have <=256 colors on the screen.

But haftoning, selection of correct palette and decreasing color depth is a subject of another question.
                     Good luck. Elf.



0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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…
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.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

772 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

13 Experts available now in Live!

Get 1:1 Help Now