Click here to Skip to main content
13,830,143 members
Click here to Skip to main content
Add your own
alternative version


96 bookmarked
Posted 14 Feb 2001

Memory bitmap class CMemBm

, 23 Feb 2006
Rate this:
Please Sign up or sign in to vote.
A simple class to make generic Windows bitmap operations easier.

An example of how to use this class for drawing transparent bitmap in a static control.


Recently, I uploaded to CodeProject an article with source code for an MFC extension library that deals with Device-Dependent Bitmaps (DDB) and Device-Independent Bitmaps (DIB). Then, I began refurbishing my old skin frame classes and soon found that some people were not happy with MFC as it causes the size of applications and libraries to grow. Moreover, while working with skins, etc., I reviewed some of the DDB and DIB class concepts, and that gave rise to the CMemBm class which seamlessly implements several irritating Windows GDI routines, and at the same time is easy to use.

The CMemBm itself

First of all, the class' name is no longer CMemBm, in the year 2005 it became a member of the charting library called MChart, so from now on it will be MChMemBm :) hope that won't ruin anything. In addition, when it was used as a drawing canvas for charting on desktop, as well as CE devices, there was a need to make it more portable and serializable.

Well, in a nutshell "what's new"... I've found that in some cases, to augment serialization, for instance, the DDB functionality had apparent drawbacks, so MChMemBm was made DDB\DIB depending on MCH_USE_DDB. If the latter is defined somewhere above MChMemBm, for example, in a precompiled header, the class transparently becomes a 32 bpp DIB section wrapper, i.e. the programming interface remains the same, and a few new methods become available.

The new serialization method is:

void GetAsDIB(MByteString& refDIB) const;

where MByteString is an alias for std::string, it does not always work correctly in the DDB mode, but it is OK in DIB. It stores the image data as binary chunk exactly as it is in a raw bitmap file. The image filtering feature is available in the DIB mode:

typedef signed short MChFilter[9];
void ApplyFilter( const MChFilter& crefFilter );

It performs simple 3x3 matrix image filtering, like, it may be used to blur the image or find edges or whatever.

The CE GDI functionality is quite limited compared to desktop, so several methods like stretch drawing, transparent drawing, alpha drawing got excluded with another #define: UNDER_CE.

#if defined(_WIN32_WCE) && !defined(UNDER_CE)
    #define UNDER_CE _WIN32_WCE

This is a very simple class. Behind the scenes, it contains the memory DC handle and the bitmap handle, the latter being selected into the former. Let's take a closer look at the public class methods:

    // initialization and cleanup
    // create memory bitmap from source bitmap.
    // !REMEMBER! hbmSrc MUST NOT be selected into 
    // any DC before calling this function
    // or following will just create copy of 
    // hDC contents, not the hbmSrc.
    bool Create(HDC hDC, HBITMAP hbmSrc, int iLeft, int iTop,
        int iWidth, int iHeight, DWORD dwRop = SRCCOPY );
    // create memory bitmap from explicit data 
    // (default is monochrome bitmap)
    bool Create( HDC hDC, int iWidth, int iHeight,
            UINT cPlanes = 1, UINT cBitsPerPel = 1, 
            const void *lpvBits = NULL );
    //copy creation
    bool Create( const MChMemBm& bmSrc, DWORD dwRop = SRCCOPY );
    //create from bitmap resource by name
    bool Create( HINSTANCE hInst, LPCTSTR pszName );
    //create from bitmap resource by ID
    bool Create( HINSTANCE hInst, UINT nID )
        { return Create( hInst, MAKEINTRESOURCE(nID) ); };

    // default constructor
    MChMemBm(){ Initialize(); };
    // inplace constructor
    MChMemBm(HDC hDC, HBITMAP hbmSrc, int iLeft, int iTop,
        int iWidth, int iHeight, DWORD dwRop = SRCCOPY )
        Create(hDC, hbmSrc, iLeft, iTop, iWidth, iHeight, dwRop);
    //copy constructor
    MChMemBm( const MChMemBm& bmSrc, DWORD dwRop = SRCCOPY )
        Create(bmSrc, dwRop);
    // create memory bitmap from explicit data 
    // (default is monochrome bitmap)
    MChMemBm( HDC hDC, int iWidth, int iHeight,
            UINT cPlanes = 1, UINT cBitsPerPel = 1, 
            const void *lpvBits = NULL )
        Create( hDC, iWidth, iHeight, cPlanes, 
                            cBitsPerPel, lpvBits );
    //construct from resource by name
    MChMemBm( HINSTANCE hInst, LPCTSTR pszName )
        Create(hInst, pszName);
    //construct from resource by ID
    MChMemBm( HINSTANCE hInst, UINT nID )
        Create(hInst, nID);
    virtual ~MChMemBm(){    Cleanup();    };

    //cleanup object data
    void Cleanup();
    //get bitmap width
    int GetWidth() const;
    //get bitmap height
    int GetHeight() const;
    // get bits per pixel for our bitmap
    int GetBpp() const;
    // get color planes for our bitmap
    int GetPlanes() const;
    //return our memory dc with bitmap selected in it
    //(Blt operations - ready)
    operator HDC() const { return m_hdcImg; };
    //boolean validation operator !
    bool operator !() const { return !m_bCreated; };
    //perform pattern fill, adjusting brush 
    //origin if neccessary
    void Fill( HDC hDC, int iLeft, int iTop, int iWidth, 
                int iHeight, bool bAdjustBrushOrg = false, 
                DWORD dwRop = PATCOPY );
    //do 1-to-1 draw
    void Draw( HDC hDC, int iLeft, int iTop, 
                            DWORD dwRop = SRCCOPY ) const;
#ifndef UNDER_CE
    //perform draw (simple or stretched)
    void Draw( HDC hDC, int iLeft, int iTop, int iWidth, 
                 int iHeight, DWORD dwRop = SRCCOPY, 
                 int iStretchMode =    COLORONCOLOR ) const;
    //do transparent drawing using WinAPI or 
    //program emulated code
    void DrawTrans( HDC hDC, int iLeft, int iTop, 
       int iWidth, int iHeight,COLORREF crTransparent ) const;
    //do alpha blending draw (works only 
    //when _WIN32_WINDOWS > 0x0400 else do nothing)
    void DrawAlpha( HDC hDC, int iLeft, int iTop, int iWidth, 
      int iHeight, const BLENDFUNCTION& blendFunc ) const;
    //get as DIB, stuffed into byte array
    void GetAsDIB(MByteString& refDIB) const;
    //pixel manipulation
    COLORREF GetPixel(int iX, int iY) const;
    void SetPixel( int iX, int iY, COLORREF clPixel );
    //special effects support
#ifndef MCH_USE_DDB
    typedef signed short MChFilter[9];
    void ApplyFilter( const MChFilter& crefFilter );

All these methods are straightforward. I need to make a couple of notes on conditional compilation #defines. DrawAlpha and DrawTrans methods wrap the GDI functions TransparentBlt and AlphaBlend which reside in msimg32.dll. As far as I know, this DLL is not a part of Win95, it is included in the MS OSs starting from Win98 and WinNT 4.0 (with some service pack, I am not sure which NT4 SP is needed for this). You may want to compile the project with <SPAN CLASS="cpp-preprocessor">#define _WIN32_WINDOWS 0x0410 (I have put this define in stdafx.h in the demo project). It means that msimg32 will be automatically linked to membm.obj. In this case, both DrawAlpha and DrawTrans use the Windows API functions. If you find that this library is absent in your system, just #define _WIN32_WINDOWS 0x0400 and DrawTrans will use the appropriate code to draw the transparent bitmap, while DrawAlpha will do nothing.

Note: As I said, MChMemBm is no longer a stand-alone class, and so several additional defines\typedefs are required. I have put a part of them into stdafx.h, while the GDI wrapper helpers are provided in a separate header file, as they are in the MChart library. The following stub defines should be placed somewhere if you are planning to re-use the MChMemBm class functionality without changing the class code:

//define M_ASSERT that way, or make it alias of some existing 
//ASSERT macro if any
#ifndef NDEBUG
    #define M_ASSERT(x)    assert(x)
    #define M_ASSERT(x)

//place it in percompiled header if you plan to make use of MChMemBm 
//class CE support
#if defined(_WIN32_WCE) && !defined(UNDER_CE)
    #define UNDER_CE _WIN32_WCE

//define this in precompiled header for CE compilation, as follows
#ifdef UNDER_CE
#if _WIN32_WCE < 0x500 && _MSC_VER > 1220 
// only needed for WM2003 builds under VS2005 
#pragma comment(lib, "ccrtrtti.lib") 

//undefine to use DDB functionality instead of DIB section
//#define MCH_USE_DDB

//the following are the necessary stubs 
//to make MChMemBm class stand-alone
//define stubs for MChart - specific types to compile standalone
#define MByteString    std::string
#define M_NUMBER_OF_ARRAY_ELEMENTS(a)    (sizeof(a)/sizeof(a[0]))
#define M_L(x)    _T(x)
//stringer macro
#define MCH_TO_STRING_(x)            # x
#define MCH_TO_STRING(x)    MCH_TO_STRING_(x)

struct MException
    MException( LPCTSTR strMsg ) : m_strMsg(strMsg) {}
    static void Throw( LPCTSTR strMsg ) { throw MException( strMsg ); }

    LPCTSTR GetMsg() const { return m_strMsg.c_str(); }

    typedef std::basic_string StdStringT;
    StdStringT m_strMsg;

//define this somewhere before including MChGDIWrappers.h
class MChNonCopyableConcept
    MChNonCopyableConcept() {}
    ~MChNonCopyableConcept() {}

    MChNonCopyableConcept( const MChNonCopyableConcept& );
    const MChNonCopyableConcept& operator=( const MChNonCopyableConcept& );

Brief comments on the demo code

The demo sample is just a generic Win32 Windows application. Here is the implementation of the Paint function:

void CALLBACK Paint( HWND hWnd, HDC hDC )
    //get window application instance
    HINSTANCE hInst = (HINSTANCE)GetWindowLong( hWnd, GWL_HINSTANCE );
    CMemBm bmMem8( hInst, IDB_BMP8 );    //8 bpp bitmap(256 colors)
    CMemBm bmMem4( hInst, IDB_BMP4 ); //4 bpp bitmap(16 colors)
    CMemBm bmMem24( hInst, IDB_BMP24 ); //24 bpp bitmap(16M colors)
    CMemBm bmExplicit( hDC, iXSize, iYSize, 1, 32, 
      abBits ); //explicit creation of 24 bit bitmap
    CMemBm bmFile( hInst, _T("avatar.bmp"),
      TRUE ); //bitmap loading from file
    bmMem8.Draw( hDC, 20, 20 );
    bmMem4.Draw( hDC, 20, 70 );
    bmMem24.Draw( hDC, 20, 120 );

    //draw them transparently (assuming blue color is transparent)
    bmMem8.DrawTrans( hDC, 140, 20, 60, 30, RGB(0,0,255) );
    bmMem16.DrawTrans( hDC, 140, 70, 60, 30, RGB(0,0,255) );
    bmMem24.DrawTrans( hDC, 140, 120, 60, 30, RGB(0,0,255) );

    //capture part of this window's dc
    CMemBm bmSnap( hDC, NULL, 40, 40, 80, 80 );

    //draw this snapshot
    bmSnap.Draw( hDC, 20, 200 );

    //get screen snapshot, draw red line on it, 
    //and draw it in window's dc
    //cleanup previous snapshot contents

    //capture screen part
    bmSnap.Create(NULL, NULL, 40, 40, 80, 80 );

    //create red pen
    HPEN hPen = CreatePen( PS_SOLID, 1, RGB(255,0,0) );
    //select it on snapshot's dc
    HANDLE hOldPen = SelectObject(bmSnap, hPen);

    //draw diagonal line in the snapshot
    MoveToEx( bmSnap, 0, 0, NULL );
    LineTo( bmSnap, 80, 80 );

    //restore snapshot's dc pen
    SelectObject(bmSnap, hOldPen);

    //cleanup pen

    //draw snapshot on window's dc
    bmSnap.Draw( hDC, 20, 300 );

    //window dc pattern filling with snapshot
    bmSnap.Fill( hDC, 200, 20, 200, 200, true );

    //test alpha blending: AC_SRC_OVER and 0 are mandatory so far,
    //128 = 50% opacity( 0 - 255), 0 - do not use bitmap's alpha channel
    BLENDFUNCTION blendFunc = {AC_SRC_OVER, 0, 128, 0};

    //do alpha blend drawing
    bmSnap.DrawAlpha( hDC, 200, 250, 80, 80, blendFunc );

That's all. I hope you enjoyed this little class. In addition, in "downloads" section, I've placed a demo project that shows the possible use of this class in a custom-draw static control (inspired by Danny Shimony).


  • 13th Feb, 2006
    • major class update, class renamed (DDB|DIB wrapper made by conditionally compiling with #define MCH_USE_DDB, ported to CE), demo projects updated (To VS 2005).
  • 24th Nov, 2004
    • added minor changes allowing the bitmap to be loaded from a file. Updated the demo project and source.
  • 1st Sep, 2003
    • added demo project that explains how to dynamically load bitmap from a resource to the CMemBm class and use it as an image preview or window background.
  • 13th Jan, 2002
    • updated the source code, demo project and the demo EXE based on the recent bug fix.


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

Software Developer (Senior) Eko-Sfera
Russian Federation Russian Federation
Born in 05.20.1971, in Moscow.
Graduated from Moscow Physical Engineering Institute in 1993.
Gained PhD. in Phys. Math. sciences in 1998.
Programmer experience over 8 years.
Assembler(s), Pascal, VBasic, JScript, ANSI C, C++.
Microcontrollers, Serial communication, MSJet DB, MFC, ATL, COM.
MSDev Studio, Borland CBuilder.
Russian, English.

Married, with one child.

You may also be interested in...

Comments and Discussions

QuestionCan I Get Your MFC Version Code? Pin
pegasuu17-Sep-08 21:54
memberpegasuu17-Sep-08 21:54 
AnswerRe: Can I Get Your MFC Version Code? Pin
Vsevolod17-Sep-08 23:10
professionalVsevolod17-Sep-08 23:10 
GeneralRe: Can I Get Your MFC Version Code? Pin
pegasuu18-Sep-08 15:26
memberpegasuu18-Sep-08 15:26 
GeneralRe: Can I Get Your MFC Version Code? [modified] Pin
Vsevolod18-Sep-08 19:51
professionalVsevolod18-Sep-08 19:51 
GeneralRe: Can I Get Your MFC Version Code? Pin
pegasuu21-Sep-08 17:29
memberpegasuu21-Sep-08 17:29 
GeneralRe: Can I Get Your MFC Version Code? Pin
Vsevolod21-Sep-08 20:01
professionalVsevolod21-Sep-08 20:01 
QuestionVC6 project Pin
Sir.Costy8-Jun-07 2:58
memberSir.Costy8-Jun-07 2:58 
AnswerRe: VC6 project Pin
Vsevolod8-Jun-07 3:23
professionalVsevolod8-Jun-07 3:23 
GeneralRe: VC6 project Pin
Sir.Costy8-Jun-07 3:28
memberSir.Costy8-Jun-07 3:28 
GeneralRe: VC6 project Pin
Sir.Costy8-Jun-07 3:57
memberSir.Costy8-Jun-07 3:57 
GeneralRe: VC6 project Pin
Vsevolod18-Jun-07 22:22
professionalVsevolod18-Jun-07 22:22 
GeneralRe: VC6 project Pin
Sir.Costy18-Jun-07 22:41
memberSir.Costy18-Jun-07 22:41 
GeneralBinary data to Bitmap picture Pin
kitty511-Aug-06 5:17
memberkitty511-Aug-06 5:17 
GeneralI hope I can use it in Windows CE... Pin
s.he12-Feb-06 20:00
members.he12-Feb-06 20:00 
GeneralRe: I hope I can use it in Windows CE... Pin
Vsevolod12-Feb-06 23:04
professionalVsevolod12-Feb-06 23:04 
GeneralRe: I hope I can use it in Windows CE... Pin
s.he13-Feb-06 12:39
members.he13-Feb-06 12:39 
GeneralRe: I hope I can use it in Windows CE... Pin
Vsevolod23-Feb-06 7:20
professionalVsevolod23-Feb-06 7:20 
GeneralRe: I hope I can use it in Windows CE... Pin
s.he23-Feb-06 9:55
members.he23-Feb-06 9:55 
GeneralIt works! Pin
mmress19-Jan-06 21:39
membermmress19-Jan-06 21:39 
Generalbitmaps are drawn mirrored Pin
Bernd1013-Dec-05 4:06
memberBernd1013-Dec-05 4:06 
GeneralRe: bitmaps are drawn mirrored Pin
Vsevolod13-Dec-05 23:57
professionalVsevolod13-Dec-05 23:57 
GeneralRe: bitmaps are drawn mirrored Pin
Bernd1014-Dec-05 2:36
memberBernd1014-Dec-05 2:36 
QuestionCan CMemBm be used with CButton Pin
Do Manh Hung18-May-05 5:08
memberDo Manh Hung18-May-05 5:08 
AnswerRe: Can CMemBm be used with CButton Pin
Vsevolod19-May-05 0:08
professionalVsevolod19-May-05 0:08 
Questionhow can i get image of desktop to memory ? Pin
Jagdish Vasani25-Feb-05 2:35
memberJagdish Vasani25-Feb-05 2:35 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190114.1 | Last Updated 23 Feb 2006
Article Copyright 2001 by Vsevolod
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid