Click here to Skip to main content
11,922,320 members (40,121 online)
Click here to Skip to main content
Add your own
alternative version


62 bookmarked

Bitmap usage extension library

, 3 Apr 2001
Rate this:
Please Sign up or sign in to vote.
MFC extension DLL including advanced bitmap usage features
<!-- Link to source file download --> <!-- Add the rest of your HTML here -->

Fake Screenshot :))


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=""></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=""></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 Smile | :) 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!" Smile | :)

It exposes public methods as follows:

<CODE>// Default constructor.

// 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:

// 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 );

CDib class usage:

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

// nResID is declared and initialized elsewhere
dibTemp.ReadFromResource( nResID );

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:


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
No Biography provided

You may also be interested in...

Comments and Discussions

Generalgeneral: Windows with buttons Pin
sekolaphakoe4-Feb-07 9:56
membersekolaphakoe4-Feb-07 9:56 
GeneralRe: general: Windows with buttons Pin
Christian Graus4-Feb-07 13:26
staffChristian Graus4-Feb-07 13:26 
QuestionHow to create images in c++ Pin
sekolaphakoe4-Feb-07 9:47
membersekolaphakoe4-Feb-07 9:47 
AnswerRe: How to create images in c++ Pin
Christian Graus4-Feb-07 10:03
staffChristian Graus4-Feb-07 10:03 
GeneralRe: How to create images in c++ Pin
sekolaphakoe4-Feb-07 10:15
membersekolaphakoe4-Feb-07 10:15 
GeneralRe: How to create images in c++ Pin
Christian Graus4-Feb-07 13:25
staffChristian Graus4-Feb-07 13:25 
Generalmorphing coding Pin
Caroline Tan JC4-Jan-04 8:07
sussCaroline Tan JC4-Jan-04 8:07 
GeneralRe: morphing coding Pin
Tugak24-May-04 23:29
memberTugak24-May-04 23:29 
GeneralFaster Set/GetPixel method Pin
Angelina Cheng22-Dec-03 8:00
memberAngelina Cheng22-Dec-03 8:00 
QuestionHow to get the data info of image? Pin
jinglebelll6-Nov-03 1:15
memberjinglebelll6-Nov-03 1:15 
QuestionHow To Set picture bacground of scrolling window Pin
khurram Mir7-Oct-03 20:39
memberkhurram Mir7-Oct-03 20:39 
GeneralBitmap editor Pin
codexxxxx22-Nov-02 20:44
membercodexxxxx22-Nov-02 20:44 
GeneralRe: Bitmap editor Pin
Anonymous2-Nov-03 13:13
sussAnonymous2-Nov-03 13:13 
GeneralGetPixel throws an exception Pin
Brendan Hack27-Jun-02 16:15
memberBrendan Hack27-Jun-02 16:15 
GeneralRotate a CDC object's some portion by given degrees Pin
Saima Shafiq18-Jun-02 0:32
memberSaima Shafiq18-Jun-02 0:32 
GeneralRotating a CDC object Pin
Saima Shafiq17-Jun-02 23:44
memberSaima Shafiq17-Jun-02 23:44 
GeneralRe: Rotating a CDC object Pin
Christian Graus18-Jun-02 0:33
memberChristian Graus18-Jun-02 0:33 
GeneralError when using CDIB please help!! Pin
yair2428-Apr-02 2:19
memberyair2428-Apr-02 2:19 
GeneralRe: Error when using CDIB please help!! Pin
Anonymous28-Apr-02 2:39
memberAnonymous28-Apr-02 2:39 
QuestionHow to get / load CDib from CStatic Control (capture) Pin
babyc++27-Apr-02 3:41
memberbabyc++27-Apr-02 3:41 
Generalgrayscale bitmap, etc Pin
Zemin28-Jan-02 18:05
memberZemin28-Jan-02 18:05 
GeneralRe: grayscale bitmap, etc Pin
Christian Graus18-Jun-02 0:34
memberChristian Graus18-Jun-02 0:34 
Generalbitmapinfoheader of CDib Pin
ersin3-Aug-01 3:23
memberersin3-Aug-01 3:23 
GeneralRe: bitmapinfoheader of CDib Pin
Vsevolod7-Aug-01 22:11
memberVsevolod7-Aug-01 22:11 
Generala new user Pin
Anonymous3-Jul-01 6:06
memberAnonymous3-Jul-01 6:06 
GeneralRe: a new user Pin
Vsevolod4-Jul-01 1:08
memberVsevolod4-Jul-01 1:08 
GeneralRe: a new user Pin
Anonymous10-Oct-01 6:59
memberAnonymous10-Oct-01 6:59 
GeneralDynamically load a bitmap from a file into CToolbar Pin
Anonymous29-May-01 0:57
memberAnonymous29-May-01 0:57 
GeneralRe: Dynamically load a bitmap from a file into CToolbar Pin
Vsevolod29-May-01 5:36
memberVsevolod29-May-01 5:36 
GeneralSave a wrong .bmp file with colors more than 16M. Pin
loujiing16-May-01 0:45
memberloujiing16-May-01 0:45 
GeneralRe: Save a wrong .bmp file with colors more than 16M. Pin
Vsevolod16-May-01 1:12
memberVsevolod16-May-01 1:12 
QuestionMLoibl :I always fail to connect to ?is it correct? Pin
benben12-May-01 0:12
memberbenben12-May-01 0:12 
AnswerRe: MLoibl :I always fail to connect to ?is it correct? Pin
MLoibl4-Jun-01 21:02
memberMLoibl4-Jun-01 21:02 
GeneralFree image library Pin
MLoibl8-Apr-01 22:31
memberMLoibl8-Apr-01 22:31 
GeneralRe: Free image library Pin
MLoibl4-Jun-01 9:47
memberMLoibl4-Jun-01 9:47 
GeneralRe: Free image library Pin
Mike Pelley22-Oct-03 3:44
memberMike Pelley22-Oct-03 3:44 
GeneralPNG Pin
John Wood4-Apr-01 6:42
memberJohn Wood4-Apr-01 6:42 
GeneralRe: PNG Pin
Christian Graus4-Apr-01 13:43
memberChristian Graus4-Apr-01 13:43 
GeneralRe: PNG Pin
John Wood5-Apr-01 11:38
memberJohn Wood5-Apr-01 11:38 
GeneralThanks!! Pin
Marat Bedretdinov24-Jan-01 10:49
memberMarat Bedretdinov24-Jan-01 10:49 
GeneralDemo Project Pin
Anonymous17-Jan-01 0:25
memberAnonymous17-Jan-01 0:25 
GeneralRe: Demo Project Pin
Vsevolod22-Jan-01 5:10
memberVsevolod22-Jan-01 5:10 
GeneralRe: Demo Project Pin
Vsevolod24-Jan-01 3:35
memberVsevolod24-Jan-01 3:35 
GeneralRe: Demo Project Pin
jamal18-Mar-01 3:16
memberjamal18-Mar-01 3:16 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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