The article Drawing Transparent Bitmap with ease with on the fly masks in MFC demonstrates how to draw a
CBitmap transparently. The article doesn't include a demo project. Here you'll find a complete implementation including a demo project. This implementation brings the transparency technique into a larger context than
CBitmap, as it's able to read and draw several file formats, including file formats with built-in transparency (PNG and GIF).
There are many bits and pieces on painting and manipulating bitmaps in this Code Project section, but we are in dire need of a complete DIB class. (ed. For a DIBSection class, see A DIBSection wrapper for Win32 and WinCE) Windows doesn't help out much. The bitmap API is rudimentary. If you only want to paint bitmaps of various formats,
OleLoadPicturePath looks like it. Only it doesn't support PNG! Nor animated GIF. Obviously Internet Explorer doesn't use this API. Perhaps they're using
CImage was created back in 1996 by Julian Smart, who adopted code from Alejandro Aguilar Sierra. Julian Smarts implementation uses MFC for no apparant reason (so we all do from time to time), and is somewhat cluttered, but a great first step on the way. He seems to have lost interest in the project on July 18th 1998, since when it wasn't maintained. Recently Davide Pizzolato adopted the class, renaming it
CxImage. He added several file formats and fixed up the code but did nothing about the structure of the code. Most of the clutter of 1998 is still there, and then some. I've undertaken to fix up the
CxImage code as well as adding value, especially transparent painting of bitmaps. How were we supposed to do without? To some readers the decoupling of the
CImage class from MFC might be of interest, as it makes it a breeze to take
CImage to compilers other than Microsoft Visual C++. This is in accordance with the image reader libraries (JPEG, TIFF, PNG) on which this is based. They're totally cross-platform. For the MFC programmers I'm providing a derived class,
CImageObject. It's a neat MFC wrapper around
Please consider this contribution one step towards making production code out of
virtual BOOL DrawImplementation(HDC hdc, int xDst, int yDst...
class CImage : public CImageBase
virtual BOOL DrawImplementation(HDC hdc...
BOOL CreateFromFile(LPCSTR filename, enum cximage_type);
BOOL Save(LPCSTR filename, enum cximage_type);
virtual BOOL Read (FILE*, enum cximage_type);
virtual BOOL Write(FILE*, enum cximage_type);
BOOL Rotate(double angle);
BOOL Exchange(COLORREF, COLORREF);
BOOL WalkColors(COLORREF(*manipulate)(BYTE r, BYTE g, BYTE b, LPVOID),
LPVOID lpUser = NULL);
class CImageObject : public CImage, public CObject
virtual void Serialize(CArchive&);
virtual void Close(void);
BOOL Draw(CDC*, int xDst = 0, int yDst = 0, int cxDst = -1, int cyDst = -1,
int xSrc = 0, int ySrc = 0, int cxSrc = -1, int cySrc = -1);
EXTERN_C BOOL WINAPI DrawTransparentBitmap(HDC hdcDst, HDC hdcImage,
Many people are confused by the specialized classes derived from
CImageGIF. Actually these specialized classes are to be considered implementation details: The
CImageGIF isn't really a GIF class, but merely an import/export class that knows about the specific graphics format's file layout. It's all one class, so to speak (
There's no GIF class proper.
CImage converts all file formats into DIB.
CImage is a Windows DIB class, it can hold only a Windows DIB image (and will not easily port to other platforms).
In the table below, you can observe how the library's implemented in layers, with the Demo application sitting on the top of it all. The Demo uses only class
CImage and considers it a blackbox, and so can you.
|Demo application - C++ code |
CImage - C++ code
|MFC + |
C Runtime Library
|Windows API + |
C Runtime Library
|C Runtime Library||PNG|
|Windows API + C Runtime Library|
|C Runtime Library||C Runtime Library||C Runtime Library|
Notes on code quality:
- I consider the ZLIB, JPEG, TIFF and PNG libraries production code.
CImage class still have some way to go.
- The Demo application as provided is a 'clean' MFC application, more so than the earlier attempts, and is now a fine boilerplate MFC application, I think.
CImage Project Settings
The actual library is project imagedll and imagelib, for DLL and lib builds. Only Debug and Release targets are needed. Unicode build is not needed, as there isn't much string handling left in
CImage imagedll/lib contains all the actual bitmap code, including the jpeg, png and tiff code. zlib is kept out, as it isn't image code as such.
Remember to build (some of) these targets before attempting to build the Demo application, it needs to link with imagedll/lib.
Demo Project Settings
Here you see combinations of the _MBCS, _UNICODE and _IMAGEDLL compiler options. Pick and choose. (All are _AFXDLL.)
Author's points of view as exercised on CImage:
- Don't implement it using MFC if it's just as easy to implement without
- Don't implement it using STL if it's just as easy to implement using MFC
- Don't implement it in C++ if it's just as easy to implement in C.
- I like all of them (except STL)
char usage on a minimum, make the code Unicode-compatible.
- Keep it simple.
- Clean up Copy/Transfer/Detach/Ghost/constructor mess
- Implement reference counting a la
- Will somebody provide a WTL Demo application?
DrawTransparentBitmap fails on the printer on 'large' images. Any suggestions?
- The code generates loads of warnings, mostly because of BYTE variable. Should use int.
CImage 1.4.1: Added transparent draw.
fclose clutter removed. Unicode enabled. Cleaned up the project workspace. Added Unicode and DLL targets. Removed remaining MFC dependencies.
CImage 1.4: Julian Smart supported JPEG, PNG, BMP and GIF - 256 colors only.
CxImage 1.1: Some support for multi-image files. Davide used some of my code and forgot leaving credits. In the Demo application, I contributed with printing support,
CMemDC usage, hatched background, entire
CDemoView::OnDraw. I also wrote
CxImageGIF::Decode, adding (rudimentary) support of animated GIF.
CxImage 1.0: Davide Pizzolato added much value, including support for ICON and TIFF and true color. He removed most MFC dependencies, but didn't go all the way for some reason.
The MFC Demo application included with
CxImage are coded in a way rather insensitive to MFC paradigms, ignoring printing, print preview and abusing CScrollView. The Demo application supplied here remedies that.
CImage change log:
Version 1.4.1 - Aug. 29th 2001 (Troels K.)
- Merged with
Changes in project file:
- Utilizing Project|Dependencies
- Proper output directory structure (LIB, Release\Unicode etc.)
CImage and derived classes:
- Implemented transparency on screen and printer (
- Unicode-enabled ("const char* filename" -> FILE* )
- MFC-style CreateFromFile/Close member functions (ReadFile/SaveFile)
- Removed silly MFC-dependency (#include afxwin.h)
- 'Bug' fix in TIFF: jconfig.vc -> jconfig.h
- Created tif_c.c
- Bug fix in
CImage::LoadResource (tmpfile never closed)
- Bug fix in CImagePNG::SaveFile (assert i delete )
- Bug fix in CImageGIF::Open (bad pack)
- Bug fix in CImageGIF::SaveFile(ignored transparency)
- Added WalkColors/Greyscale method
- Enabled handling of animated gif's (CImageGIF::Open).
- Enabled reading of RLE-compressed bitmaps
- Bug fix: Rotate didn't handle transparency (
- Bug fix: Memory corruption in
- Bug fix: GDI resource leakage in
Changes in Demo application:
- Now utilizing CMemDC+CScrollView+CPreviewView+CArchive
- Added Unicode build
- Added serializing (CImageObject)
- Added hatched background
- Reorganized resource bitmaps
- Gif resource bitmap transparent to demonstrate transparent draw
Version 220.127.116.11 - Aug. 22th 2001 (Troels K.)
- Merged with
Version 1.4 - July 18th 1998 (Julian Smart):
Fixed code creating a CBitmap from a
Fixed MakeBitmap (Brett Levine).
Fixed memory leak in imajpg.cpp; added PNG 1.0.1 updates. Fixes by Konstantin Mihailov.
Fixed problem saving JPEG files, from Lars Bullwinkel.
Version 1.3 - June 28th 1997 (Julian Smart):
Fixed code creating a CBitmap from a
CImage (I hope).
Added contributed GIF writing code from Randy Spann.
Version 1.2 - January 18th 1997 (Julian Smart):
Fixed code creating a
CImage from a CBitmap, and added test to demo (under File menu).
Version 1.1 - January 9th 1997 (Julian Smart):
Fixed a problem with compiling the JPEG library (defining FAR) and changed the library directories to Debug and Release, instead of lib.
Version 1.0 - December 23rd 1996 (Julian Smart):
First release, based on the wxWindows wxImage class and many other bits and pieces.
Q: How do I create transparent bitmaps?
A: Save the file in formats PNG or GIF, using Paint Shop Pro or the like
Q: I don't want to use either format, Unisys owns GIF and PNG is too esoteric for me, what to do?
A: Save as regular Windows BMP, and in your application, set the transparent color of your choice using
CImage::SetBkColor. (It only works for palette bitmaps.)
Q: ...but I'm not ready for
CImage. I want to use
A: Then use
IPicture, and bring in only trans.c. It's universal.