Click here to Skip to main content
Click here to Skip to main content

Bitmap usage extension library

By , 3 Apr 2001
 

Fake Screenshot :))

Introduction

Needless to say that a bitmap is the gateway between different raster image formats and GDI image representation. Device Independent Bitmaps or DIBs are important objects that allow us to save, load and draw images without thinking of display mode in device context - independent way. OS will do its best to output them as close to original, as it is able, because DIBs contain all necessary color usage, color depth, and image resolution information. There are many, or better say, numerous classes that support DIB operations through wrapping Windows API functions. This MFC extension DLL contains such class and a bit more to make bitmap usage easier and more flexible.

Classes included

All class declarations are included in the bmpext.h header.

Device dependent bitmap (DDB) extension class CDDBDrawEx:

This class is intended for on-stack allocation and usage within the single scope, as it has no default constructor and no "real" data members, only pointers to externally defined ones.

It exposes public methods as follows:

// Inplace constructor. pDC - pointer to drawing context, pbmSrc - pointer to DDB to render
// on pDC, pbmBack - optional pointer to background bitmap, that pbmSrc to blend with during transparent draw.
CDDBDrawEx(CDC* pDC, CBitmap* pbmSrc, CBitmap* pbmBack = NULL);

// Wraps around source DDB filling on pDC within rDest rectangle.
void Fill(CRect& rDest);

// Wrapper around BitBlt API function. Blitting source bitmap to pDC into rDest rectangle.
void Draw(CRect& rDest, CPoint& pntSrc);

// Perform transparent DDB draw, assuming crMask color stands for transparent pixels
// in source DDB. It uses modified routine taken from flicker-free drawing example on
// <A HREF="http://www.codeguru.com">www.codeguru.com</A>. It uses optional pbmBack if supplied in constructor to do this flicker - free drawing.
void DrawTransparent(CRect& rDest, CPoint& pntSrc, COLORREF crMask);

// Constructs complex region from source bitmap using cTransparentColor and cTolerance
// for defining transparent (hollow) region areas. Internally, it uses modified region
// builder code by Jean-Edouard Lachand-Robert <A HREF="http://www.geocities.com/Paris/LeftBank/1160/resume.htm">http://www.geocities.com/Paris/LeftBank/1160/resume.htm</A>).
// It's extremely useful when playing with ::SetWindowRgn function to create windows with
// non-rectangular shape.
HRGN MakeRgn(COLORREF cTransparentColor = 0, COLORREF cTolerance = 0x101010);

DIBitmap encapsulation class CDib:

This class and the Frankenstein monster have much in common :) Part of its code was taken from samples shipped with MFC 4.0 book, partially from CodeGuru and other sources. I have modified it, put it together, and said "It's alive... ALIVE!" :)

It exposes public methods as follows:

<CODE>// Default constructor.
CDib();

// Several self-explanatory methods.
DWORD Width();
DWORD Height();
WORD  NumColors(  BITMAPINFOHEADER& bmiHeader );

// Object validation checking method.
BOOL  IsValid();

// Frees internal DIB member data, making object invalid. (IsValid returns false)
void Invalidate();

// Drawing method, automatically invokes DIB stretching.
BOOL  Draw(CDC*, CRect& rectDC, CRect& rectDIB);

// Direct DIB pixel access methods.
void SetPixel( int iX, int iY, RGBQUAD& rgbPixel );
RGBQUAD GetPixel(int iX, int iY);

// Save DIB to disk file.
DWORD Save(CFile& file);

// Read DIB from file, support resource reading. If bFromResource = TRUE,
// then it just skips DIB file marker read phase.
DWORD Read(CFile& file, BOOL bFromResource = FALSE );

// Reads DIB from resource, basing on its nResID. Internally invokes previous Read method.
DWORD ReadFromResource(UINT nResID);

// Next two methods create DDB from DIB object.
HBITMAP CreateDDBitmap(CDC* pDC);
HBITMAP CreateDDBitmap( HDC hDC );

// Tries to compress DIB first making it DDB, then changing compression attribute and
// converting it back to DIB.
BOOL Compress(CDC* pDC, BOOL bCompress );

// Assignment operator
CDib& operator = (CDib& dib);</CODE>

Extension Classes Usage

CDDBDrawEx class usage:

<CODE>{
....
// CDC* pDC, CBitmap* bmImg, CRect rDest, and COLORREF crImgMask are declared and initialized elsewhere
....
// inplace construction
CDDBDrawEx ddbDrawEx(pDC, bmImg);

// Do transparent drawing
ddbDrawEx.DrawTransparent(rDest, CPoint(0), crImgMask );

// Do bitmapped fill
ddbDrawEx.Fill( rDest )
}

// Region creation. CBitmap bmSkin is declared and initialized elsewhere
{
....
CDDBDrawEx ddbDrawEx(NULL, &m_bmSkin );

return ddbDrawEx.MakeRgn( m_crTransparent, m_crTolerance );
}</CODE>

CDib class usage:

<CODE>CDib dibTemp;
CDC* pDC = CDC::FromHandle( ::GetDC( NULL ) );

....
// nResID is declared and initialized elsewhere
dibTemp.ReadFromResource( nResID );
....
}</CODE>

Demo project implementing these extensions also includes class template for customizing dialog frame. I'm going to describe this template in detailed review which I plan to publish in a week. It will cover the window frames customization technique and will be supplemented with couple of helper template class. That's all so far, but I will supply several useful control and animation class libs packaged as MFC extension DLL in the nearest future. Additions, corrections and suggestions are always welcomed, as I often haven't enough time to optimize my source or make it more elegant. Mail me at: gromov@eti.co.ru.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Vsevolod Gromov
Russian Federation Russian Federation
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Generalgeneral: Windows with buttonsmembersekolaphakoe4 Feb '07 - 8:56 
i am particularly new to c++, all my programs are black background elementary programs. please show me how to create windows with buttons that u can actually press and with spaces that you can write to. (generall, i'd like to know about real-world applications and animation) can anyone please help
 
s. phakoe
GeneralRe: general: Windows with buttonsstaffChristian Graus4 Feb '07 - 12:26 
As I told you, you're asking in a forum dedicated to a specific article. You're also asking questions that you can answer yourself, with google. If you're this new, you should buy a book and work through it. Then, you should take your questions to the Visual C++ forum, not in the forum for articles not related to your question.

 
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog

QuestionHow to create images in c++membersekolaphakoe4 Feb '07 - 8:47 
can anyone please show me how to create, display, and use images in c++Unsure | :~
AnswerRe: How to create images in c++staffChristian Graus4 Feb '07 - 9:03 
You should ask this in the C++ forum, after you use google a bit to find your own info. CBitmap is the class in MFC for an image. If you google that, you should find heaps of info.
 
You'd also need to define 'use'. Do you want to draw on an image ? Process it somehow ?

 
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog

GeneralRe: How to create images in c++membersekolaphakoe4 Feb '07 - 9:15 
Thanks. by use, i just mean generall to do anything doable to images, (move them, create, etc) anything
GeneralRe: How to create images in c++staffChristian Graus4 Feb '07 - 12:25 
OK, you're well into the world of 'google it first'. You're asking broad and easy to answer questions.

 
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog

Generalmorphing codingsussCaroline Tan JC4 Jan '04 - 7:07 
is anyone out there knows the c++ coding for image morphing?thanks alot
GeneralRe: morphing codingmemberTugak24 May '04 - 22:29 
do you pics?;)
GeneralFaster Set/GetPixel methodmemberAngelina Cheng22 Dec '03 - 7:00 
Hello,
 
I am displaying a grayscale image by calling:
 
SetDIBitsToDevice( CloneScrDC.GetSafeHdc(),
ptDst.x, ptDst.y,
m_param->m_cx, m_param->m_cy,
m_lScrollX, 0,
m_lScrollY, m_param->m_cy,
topbits,
m_param->m_bmi,
m_param->m_coloruse);
 
Where m_param store pixel info.
 
But now I would like to display white pixel in Red and Black pixels in Blue, I used CloneScrDC get/setpixel to display Red and Blue color but it is way too slow. Anybody has some great ideas?
 
Thanks in advance.
 
Please reply to LeoAngelReliable@yahoo.com.
 

 
LeoAngelReliable@yahoo.com
QuestionHow to get the data info of image?memberjinglebelll6 Nov '03 - 0:15 
If I use the class CBitmap?
QuestionHow To Set picture bacground of scrolling windowmemberkhurram Mir7 Oct '03 - 19:39 
How To set picture as background of a scrolling window using MFC
GeneralBitmap editormembercodexxxxx22 Nov '02 - 19:44 
Please help to find a bitmap editor like used in MFC
my mail is alex_kuleshov@hotmail.com
GeneralRe: Bitmap editorsussAnonymous2 Nov '03 - 12:13 
Cry | :(( Unsure | :~ Dead | X| Rose | [Rose] D'Oh! | :doh: Sigh | :sigh: Eek! | :eek:
GeneralGetPixel throws an exceptionmemberBrendan Hack27 Jun '02 - 15:15 
Hi All,
 
GetPixel(iX,iY) does not work correctly. If iY == 0 you raise an exception, if not you get the pixel from the row below the one you're looking for. Below is the solution.
 
Change this code in dib.cpp:
 
-- begin code change
RGBQUAD CDib::GetPixel(int iX, int iY)
{
     RGBQUAD rgbResult;
     WORD wDummy;
    
     //takeinto account that DIBit raws are reversed vertically
     iY = m_pBMI->bmiHeader.biHeight - iY;
    
     //assert pixel position
 

To this:
 
RGBQUAD CDib::GetPixel(int iX, int iY)
{
     RGBQUAD rgbResult;
     WORD wDummy;
    
     //takeinto account that DIBit raws are reversed vertically
     iY = m_pBMI->bmiHeader.biHeight - iY - 1;
    
     //assert pixel position
-- end code change
 
I can't believe this hasn't been picked up before.
 


GeneralRotate a CDC object's some portion by given degreesmemberSaima Shafiq17 Jun '02 - 23:32 
Hello,
i need to get some portion of pDC(displayed on the single document) from gove x,y coordinates and rotate that portion of pDC and then display the same image on the single document's screen i.e. on pDC;
Could any bdy please help me outFrown | :(
GeneralRotating a CDC objectmemberSaima Shafiq17 Jun '02 - 22:44 
Hello,i have to rotate a CDC object getting it from pDC and displaying it back on the pDC i used SetWorldTransform from msdn but its not working can i send u the code and will u please check this Smile | :)
GeneralRe: Rotating a CDC objectmemberChristian Graus17 Jun '02 - 23:33 
There is code on CP to do this already.

 
Christian
 
I am completely intolerant of stupidity. Stupidity is, of course, anything that doesn't conform to my way of thinking. - Jamie Hale - 29/05/2002
 
Half the reason people switch away from VB is to find out what actually goes on.. and then like me they find out that they weren't quite as good as they thought - they've been nannied. - Alex, 13 June 2002
GeneralError when using CDIB please help!!memberyair2428 Apr '02 - 1:19 
I keep getting this error when trying to use the CDIB class:
 
dib.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) const CDib::`vftable'" (__imp_??_7CDib@@6B@)
 
I belive its related to a DLL file
what can I do??
please help for it is very urgent!!!
 

 
thanks
Yair
GeneralRe: Error when using CDIB please help!!memberAnonymous28 Apr '02 - 1:39 
Don't use a dll, use Chris Maunders DIBSection wrapper instead.

QuestionHow to get / load CDib from CStatic Control (capture)memberbabyc++27 Apr '02 - 2:41 
Hi all
I am trying to capture the live video from CStatic control. I do not want to directly access by directX video memory.
 
Is it possible to get CDib object directly from CStatic Control
Confused | :confused:
Generalgrayscale bitmap, etcmemberZemin28 Jan '02 - 17:05 
It seems pretty late to join this party.Blush | :O Is it over yet?Laugh | :laugh:
Well if somebody is still using the code, could you share with me some of your experience?
First of all, I want to use greyscale bitmap, somehow it turned to be blue-ish and negative, I used debugging and found out the palette was not set correctly for bmiHeader.biBitCount=16, how should I do it?
Secondly, I found this code could read and display the sample bmp file(Elvis.bmp) correctly, but not with the bmp files created by other applications, then I found out that there three (or more?) versions of bitmap info headers: BITMAPCOREHEADER,BITMAPINFOHEADER,BITMAPV4HEADER. It's really confusing.
Could anybody there kindly fix these and let's re-use the code? This part of code is meant to be re-usable anyway, it's a shame to throw good chunk of code awayFrown | :( Frown | :( . Java almost re-use all the code been writen and that's why it become so powerful, because it's a result of collective intelligence.

GeneralRe: grayscale bitmap, etcmemberChristian Graus17 Jun '02 - 23:34 
Zemin wrote:
Java almost re-use all the code been writen and that's why it become so powerful, because it's a result of collective intelligence.
 
HAHAHAHAHAHAHAHAHAHAHAHA !!!!!
 
That is the funniest thing I've heard yet.
 

 
Christian
 
I am completely intolerant of stupidity. Stupidity is, of course, anything that doesn't conform to my way of thinking. - Jamie Hale - 29/05/2002
 
Half the reason people switch away from VB is to find out what actually goes on.. and then like me they find out that they weren't quite as good as they thought - they've been nannied. - Alex, 13 June 2002
Generalbitmapinfoheader of CDibmemberersin3 Aug '01 - 2:23 

i cannot access m_pBMI of CDib even if i can draw the bitmap on the screen. in order to blit onto the screen stretchdibits uses m_pBMI, however when i make explicit calls to m_pBMI it returns NULL. what can be the reason for this?
 
Confused | :confused:
 
ersin
GeneralRe: bitmapinfoheader of CDibmemberVsevolod7 Aug '01 - 21:11 
Hello, ersin!
First, pardon me for such a long silence, you know, vacations, etc.
Second, could you be more specific in the problem decription? The code snippet may be of great use here. So far, I ought to say that m_pBMI is protected member of CDib, so it isn't accessible from outside the class. Furthermore, since you successfully read the Dib, m_pBMI mustn't be NULL. BTW, if your code manage to draw it, then it sure isn't NULL, moreover, Width and Height methods directly extract bitmap sizes from m_pBMI, so it cannot be NULL either. I wonder how can you make direct calls to protected member m_pBMI from outside the CDib class, without having compiler errors? It must be some compiler issue or something. Provide some code example on your problem pls.
Best regards, Vsevolod
 
Vsevolod Gromov
Generala new usermemberAnonymous3 Jul '01 - 5:06 
I am a new user, i can not read a bmp file from the disk, i can not set a pixel. please give me an exampleCry | :((

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 4 Apr 2001
Article Copyright 2001 by Vsevolod Gromov
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid