Click here to Skip to main content
11,710,472 members (86,770 online)
Click here to Skip to main content

CContourBitmap - A Region Creator

, 6 Jul 2004 CPOL 65.2K 3K 46
Rate this:
Please Sign up or sign in to vote.
A bitmap class with a "brute-force" region creator

Introduction

A few weeks ago I was looking for a way to create irregularly shaped windows from a bitmap with a user defined transparent color. The MFC samples, that I found so far, have their problems with enclosed transparent regions. So I decided to come up with my own solution.

Background

CContourBitmap uses a very simple but failsafe "brute-force" algorithm to calculate a region from a bitmap with a user defined transparent color. It starts with the rectangular region given by the bitmap's dimensions and then scans the bitmap row by row. Every "transparent" row section, that is found, is subtracted from the bitmap's region, giving a new bitmap's region.

Below there is a (reduced) screenshot of the demo application's main window. (Well, the copyright line has been added later.) But as you can see, CContourBitmap has no problems with convex or concave shapes or fully enclosed transparent regions.

Using the code

CContourBitmap offers two different methods to calculate a region from a bitmap:

HRGN CContourBitmap::CreateRegion(COLORREF colorTransp=CLR_DEFAULT)
Use this function to explicitly specify the transparent color. (Or use the default color, that is the color of the topleft pixel.)
HRGN CContourBitmap::CreateRegion(CPoint pntTransparentColor)
Use this function to extract the transparent color from any pixel within the image. (If the pixel is outside of the bitmap, the return value is NULL.)

Apart from these methods, there are two other helpful functions to ease bitmap handling:

CSize CContourBitmap::GetSize()
Returns the dimensions of the bitmap.
BOOL CContourBitmap::Paint(CDC* pDC, int nXDest=0, int nYDest=0)
Copies the bitmap with an optional offset to the given device context.

Points of Interest

CContourBitmap is derived from CBitmap. It adds no attributes to the base class. Thus, casting a CBitmap to CContourBitmap is save.

Known Bugs

Specifying a 24 bit transparent color on a Windows desktop with 16 bit color depth may not work correctly. Needs a conversion from the 24 bit color space to the 16 bit color space.

Legal Copyrights

The Pink Panther TM & (C) 1964 by Metro-Goldwyn-Mayer Studios, Inc.

History

  • 2004-07-07 Initial release

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Wolfgang Busch
Software Developer
Germany Germany
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionTHANK YOU Pin
nergel6-Oct-13 9:07
membernergel6-Oct-13 9:07 
SuggestionCan we make it more perfect Pin
wcccodeproject13-Oct-11 4:50
memberwcccodeproject13-Oct-11 4:50 
Questionhow to add copyright line Pin
aren37222-Aug-08 1:57
memberaren37222-Aug-08 1:57 
GeneralThanks Pin
CaveFox29-Oct-06 11:18
memberCaveFox29-Oct-06 11:18 
GeneralThanks for the CContourBitmap Pin
Aju.George27-Sep-06 4:23
memberAju.George27-Sep-06 4:23 
GeneralFrame Number Display and Mixing the Bitmap Data with the Frame Pin
badal_akr31-May-05 20:03
memberbadal_akr31-May-05 20:03 
GeneralWon't work with propertSheet Pin
amitguliani26-Oct-04 9:11
memberamitguliani26-Oct-04 9:11 
GeneralRe: Won't work with propertSheet Pin
Wolfgang Busch18-Nov-04 13:53
memberWolfgang Busch18-Nov-04 13:53 
GeneralRe: Won't work with propertSheet Pin
amitguliani7-Dec-04 12:02
memberamitguliani7-Dec-04 12:02 
GeneralUsing controls... Pin
JL0028-Jul-04 2:34
memberJL0028-Jul-04 2:34 
GeneralRe: Using controls... Pin
Wolfgang Busch8-Jul-04 4:34
memberWolfgang Busch8-Jul-04 4:34 
GeneralLegal Copyrights Pin
Paul Selormey7-Jul-04 14:34
memberPaul Selormey7-Jul-04 14:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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
Web03 | 2.8.150819.1 | Last Updated 7 Jul 2004
Article Copyright 2004 by Wolfgang Busch
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid