Click here to Skip to main content
6,595,444 members and growing! (16,477 online)
Email Password   helpLost your password?
Multimedia » General Graphics » General     Intermediate License: The Code Project Open License (CPOL)

CContourBitmap - A Region Creator

By Wolfgang Busch

A bitmap class with a "brute-force" region creator
VC6, VC7, VC7.1, Windows, MFC, VS.NET2003, Dev
Posted:6 Jul 2004
Views:45,248
Bookmarked:35 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
22 votes for this article.
Popularity: 5.99 Rating: 4.47 out of 5
2 votes, 9.1%
1

2
1 vote, 4.5%
3
3 votes, 13.6%
4
16 votes, 72.7%
5

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


Member

Occupation: Software Developer
Location: Germany Germany

Other popular General Graphics articles:

  • A flexible charting library for .NET
    Looking for a way to draw 2D line graphs with C#? Here's yet another charting class library with a high degree of configurability, that is also easy to use.
  • CxImage
    CxImage is a C++ class to load, save, display, transform BMP, JPEG, GIF, PNG, TIFF, MNG, ICO, PCX, TGA, WMF, WBMP, JBG, J2K images.
  • 3D Pie Chart
    A class library for drawing 3D pie charts.
  • Barcode Image Generation Library
    This library was designed to give an easy class for developers to use when they need to generate barcode images from a string of data.
  • ImageStone
    An article on a library for image manipulation.
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 13 of 13 (Total in Forum: 13) (Refresh)FirstPrevNext
Generalhow to add copyright line Pinmemberaren3722:57 22 Aug '08  
GeneralThanks PinmemberCaveFox12:18 29 Oct '06  
GeneralThanks for the CContourBitmap PinmemberAju.George5:23 27 Sep '06  
GeneralFrame Number Display and Mixing the Bitmap Data with the Frame Pinmemberbadal_akr21:03 31 May '05  
GeneralWon't work with propertSheet Pinmemberamitguliani10:11 26 Oct '04  
GeneralRe: Won't work with propertSheet PinmemberWolfgang Busch14:53 18 Nov '04  
GeneralRe: Won't work with propertSheet Pinmemberamitguliani13:02 7 Dec '04  
GeneralUsing controls... PinmemberJL0023:34 8 Jul '04  
GeneralRe: Using controls... PinmemberWolfgang Busch5:34 8 Jul '04  
GeneralLegal Copyrights PinsupporterPaul Selormey15:34 7 Jul '04  
GeneralCan you help.... PinmemberJudd2:01 17 Nov '04  
GeneralRe: Can you help.... PinmemberWolfgang Busch16:24 18 Nov '04  
GeneralRe: Can you help.... PinmemberJudd0:16 19 Nov '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 6 Jul 2004
Editor: Nishant Sivakumar
Copyright 2004 by Wolfgang Busch
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project