Click here to Skip to main content
Click here to Skip to main content

CContourBitmap - A Region Creator

By , 6 Jul 2004
 

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

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.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralLegal CopyrightsmemberPaul Selormey7 Jul '04 - 14:34 
This article seems to carry a copyright older than Windows itself Frown | :-(
 
The Pink Panther TM & (C) 1964 by Metro-Goldwyn-Mayer Studios, Inc.
 
Best regards,
Paul.

 
Jesus Christ is LOVE! Please tell somebody.
QuestionCan you help....memberJudd17 Nov '04 - 1:01 
Wolfgang,
 
I like this region implementation, very neat and clean!
 
I was wondering if perhaps you could give me some pointers for an effect I'm trying to achieve...
 
In an MDI MFC application I have a screen on which the user can place any number of bitmaps, loaded from files. The bitmaps can be moved and resized as required. I have implemented transparency, and flip and mirror functions.
 
The bit I struggling with is as follows - when the user points at the bitmap with the mouse cursor, I'd like to highlight it using FrameRgn to draw a frame around the non-transparent part of the bitmap (also I'd like to only highllight when pointing at non-transparent regions).
 
I have a feeling ContourBitmap might make this quite easy, but can't figure it out (my FrameRgn always seems to draw a rectangle around the source bitmap).
 
Any pointers?
 
Thanks
AnswerRe: Can you help....memberWolfgang Busch18 Nov '04 - 15:24 
Well, it's hard to tell what's going wrong in your application without seeing the code. But perhaps it might help, if you clone the region handle returned by CBitmapContour to use the duplicate when calling FrameRgn.
 
When you pass a region handle to SetWindowRgn it seems that the handle becomes invalid. Obviously you have to clone that handle to use it in different places.
 
You can use CombineRgn to clone a region handle:
// 1st step: initialize the target region
HRGN hrgnTarget = CreateRectRegn(0,0,0,0);
// 2nd step: call CombineRgn to produce a clone
CombineRgn(hrgnTarget, hrgnSource, NULL, RGN_COPY);
HTH
Wolfgang
GeneralRe: Can you help....memberJudd18 Nov '04 - 23:16 
Thanks for you response. I managed to get the frame to appear, but I think it was more by accident than anything else! I use a mask (Contour) bitmap to create the transparency effect, and while preparing the mask I store the region. I then use this region to draw the frame, which seems to work (but, again, I think by accident).
 
I tried to use PtInRegion to check when my mouse pointer is hovering over the non-transparent parts of the bitmap, but it doesn't work.
 
I also think I need to do some extra processing to 'undraw' the frame (or redraw the bitmap without the frame).
 
I'll have to keep trying...

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

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