Click here to Skip to main content
Licence CPOL
First Posted 6 Jul 2004
Views 57,552
Bookmarked 43 times

CContourBitmap - A Region Creator

By | 6 Jul 2004 | Article
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)

About the Author

Wolfgang Busch

Software Developer

Germany Germany

Member



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. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
SuggestionCan we make it more perfect Pinmemberwcccodeproject4:50 13 Oct '11  
Questionhow to add copyright line Pinmemberaren3721:57 22 Aug '08  
GeneralThanks PinmemberCaveFox11:18 29 Oct '06  
This helped me lots!! Big Grin | :-D
 

Artificial Intelligence is no match for Natural Stupidity

GeneralThanks for the CContourBitmap PinmemberAju.George4:23 27 Sep '06  
GeneralFrame Number Display and Mixing the Bitmap Data with the Frame Pinmemberbadal_akr20:03 31 May '05  
GeneralWon't work with propertSheet Pinmemberamitguliani9:11 26 Oct '04  
GeneralRe: Won't work with propertSheet PinmemberWolfgang Busch13:53 18 Nov '04  
GeneralRe: Won't work with propertSheet Pinmemberamitguliani12:02 7 Dec '04  
GeneralUsing controls... PinmemberJL0022:34 8 Jul '04  
GeneralRe: Using controls... PinmemberWolfgang Busch4:34 8 Jul '04  
GeneralLegal Copyrights PinmemberPaul Selormey14:34 7 Jul '04  
QuestionCan you help.... PinmemberJudd1:01 17 Nov '04  
AnswerRe: Can you help.... PinmemberWolfgang Busch15:24 18 Nov '04  
GeneralRe: Can you help.... PinmemberJudd23:16 18 Nov '04  

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.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120529.1 | Last Updated 7 Jul 2004
Article Copyright 2004 by Wolfgang Busch
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid