Click here to Skip to main content
Email Password   helpLost your password?

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

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
Generalhow to add copyright line
aren372
2:57 22 Aug '08  
hi,
CContourBitmap is really cool, thanks for your sharing.
I want to know how to add text just like your copyright line, the demo I downloaded doesn't include this.
I would appreciate if you could tell me or send your demo modified to my email:aren372@hotmail.com.
GeneralThanks
CaveFox
12:18 29 Oct '06  
This helped me lots!! Big Grin


Artificial Intelligence is no match for Natural Stupidity

GeneralThanks for the CContourBitmap
Aju.George
5:23 27 Sep '06  

CContourBitmap - Nice and neat class. Great work.
Thank ya Big Grin



-Silver-

GeneralFrame Number Display and Mixing the Bitmap Data with the Frame
badal_akr
21:03 31 May '05  
I am working on one assignment which should create a BItmap Corrosponding to one number(1,2,3,4,5,6....) and the data has to be Dumped in to the Video Grabbed Frame(RGB24 Bit data) so that if we see the Frame we should be able to see the grabbed frame too.
Can any body help me
Regards
Smile

Anil Kumar
GeneralWon't work with propertSheet
amitguliani
10:11 26 Oct '04  
I have propertysheet and then property pages inside it. When I tired to use BitmapContour class it shows the complete dialog box.

AGuliani
GeneralRe: Won't work with propertSheet
Wolfgang Busch
14:53 18 Nov '04  
I don't see any reason why CBitmapContour shouldn't work with property sheets. All you have to do is to subclass CPropertySheet and to set its window region in the OnCreate handler.

Have you noticed the "Known Bugs" section of my article? The problems that you have might be caused by the color depth of your desktop. To be independent from the color depth use HRGN CContourBitmap::CreateRegion(CPoint pntTransparentColor) and specify a point within the bitmap to pick the transparent color rather than to specify its color value.

HTH
Wolfgang


GeneralRe: Won't work with propertSheet
amitguliani
13:02 7 Dec '04  
It is working with Property sheet. Do you know any way to display 24 bit Bitmap that works with this project? I am able to display 256 color bitmap but not 24 bit bitmap.

Aguliani
GeneralUsing controls...
JL002
3:34 8 Jul '04  
Hi!

If you put any control on the form it gets flashing all the time, how to avoid it?

I am looking for some code to make an OSD, showing just a transparent background static control. Is it possible with this code?

Thanx for sharing!!
GeneralRe: Using controls...
Wolfgang Busch
5:34 8 Jul '04  
JL002 wrote: If you put any control on the form it gets flashing all the time, how to avoid it?
It is flashing because the WS_CLIPCHILDREN style is not set for the dialog. When you go to the resource editor and set the checkmark for "Clip children" the flashing is history Smile

JL002 wrote: I am looking for some code to make an OSD, showing just a transparent background static control. Is it possible with this code?
Yes, it is.

JL002 wrote: Thanx for sharing!!
You are welcome.
GeneralLegal Copyrights
Paul Selormey
15:34 7 Jul '04  
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.
GeneralCan you help....
Judd
2:01 17 Nov '04  
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
GeneralRe: Can you help....
Wolfgang Busch
16:24 18 Nov '04  
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....
Judd
0:16 19 Nov '04  
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...


Last Updated 7 Jul 2004 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010