A tool to convert bitmaps to region files

Environment: VC++ 4.2

Since the arcticle Transparent Window by Franz Polzer helped me a lot, I would like to contribute a little tool that converts bitmaps to region-files suitable to use as a window-mask (SetWindowRgn).

Why does anybody need this tool?

  • its flexible; its possible to adjust the key-color
  • its fast (not the tool, but your app); loading a region is better than creating it online
  • its free; source is included

How to use it?

    enter a source bitmap path (or press browse)
  • enter a key color (or click onto any pixel of the source bitmap)
  • press "calc !" wait and see the result
  • enter a destination path (or press browse)
  • press "write"

To use those region-files in your own project, you need to do the following: Import the *.RGN-file into your project-resources. The type of that resource needs to be set to "Data".

You can also import it manually by editing your *.rc file. Just insert something like the following:

IDR_SAMPLE_REGION    RCDATA  DISCARDABLE     "res\\sample.rgn"

Below is some example code that loads and activates a region, it should be called from something like "OnInitDialog".


//nRegionID - resource ID of the region data
BOOL CTransparentWnd::InitRegion (unsigned short nRegionID)
{
	const char *MSGCAPTION="Init Region";
	HRSRC hrsrc;		//handle of the resource	
	HGLOBAL hmem;		//handle to resource memory
	DWORD dwSize;		//size of our resource
	RGNDATA *rd;		//region data	
	CRgn rgn;			//the region itself	

	//try to locate the resource
	if ((hrsrc=FindResource(NULL,
	                        (LPCSTR)nRegionID,
	                         RT_RCDATA)) == NULL)
	{
		MessageBox ("failed to find region",MSGCAPTION,MB_OK);
		return FALSE;
	}	
	//load the resource
	if ((hmem=LoadResource (NULL,hrsrc)) == NULL)
	{
		MessageBox ("failed to load region",MSGCAPTION,MB_OK);
		return FALSE;
	}
	//get the size of the region-data structure (= header + data)
	if ((dwSize=SizeofResource (NULL,hrsrc)) == 0)
	{
		MessageBox ("region has an invalid size",MSGCAPTION,MB_OK);
		return FALSE;
	}
	//lock the resource to get a pointer for it
	if ((rd=(RGNDATA *)LockResource (hmem)) == NULL)
	{
		MessageBox ("failed to lock resource memory",MSGCAPTION,MB_OK);
		return FALSE;
	}
	//create a region from our data
	if (!rgn.CreateFromData (NULL,dwSize,rd))
	{
		MessageBox ("failed to create a region from that data",MSGCAPTION,MB_OK);
		return FALSE;
	}
	//setup
	if (!SetWindowRgn (GetSafeHwnd(),(HRGN)rgn,TRUE))
	{
		MessageBox ("failed to set the region for that window",MSGCAPTION,MB_OK);
		return FALSE;
	}
	//NOTE: win32-based apps do not need to unlock or free loaded resources (not manually)
	return TRUE;
}

Download project files - 34 KB



Comments

  • Problems with RCDATA in Visual Studio .NET ?

    Posted by Legacy on 01/01/2004 12:00am

    Originally posted by: Till Toenshoff

    I experienced strange problems when including custom resources (RCDATA) into the application .rc-file.

    The .NET resource editor kept removing the "RCDATA"-expression which lead to a syntax error. That smart .NET resource compiler returns errors for lines that are just fine - mean trap.

    It took me a lot some time to find out that the best place for keeping such resource includes is within the .rc2-file. Once included in that file (and not within the .rc anymore), this resource is not listed or touched by the resource editor.

    Hope that helps!

    cheers,
    till

    Reply
  • Small Fix for memory leak and load diferent type bitmaps

    Posted by Legacy on 03/30/2001 12:00am

    Originally posted by: Plamen Grigorov

       In the begining it is cool sample. I want thank You for solution!
    
    Have a small memory leak in member m_pRgn an CDlgPreview. It must be destroyed before destroing CDlgPreview object. For example CDlgPreview must have destructor with simple code:

    CDlgPreview::~CDlgPreview()
    {
    if (m_pRgn)
    delete m_pRgn;
    m_pRgn=NULL;
    }

    Next small problem is in reading bitmaps with another 24 bits color map . Up in comments i see one successful patch for that problem. Another variant for solution is change code in member function LoadBitmap an CBmp2RgnDlg. Change in line 307 of Bmp2RgnDlg.cpp :
    // original code
    f.Read(lpbmi->bmiColors,bmih.biClrUsed*sizeof(RGBQUAD));
    // that is my change
    f.Read(lpbmi->bmiColors,(1<<bmih.biBitCount)*sizeof(RGBQUAD));
    It is because in bmih.biClrUsed for simple bitmaps with color table stand zero and real count colors must be calculate. Have a bitmaps with biClrUsed different from zero. May be in this case must be checked and that value for zero. I think that it is work.

    Reply
  • A Wrote a MUCH Better Tool for Regions

    Posted by Legacy on 02/07/2001 12:00am

    Originally posted by: Bill SerGio

    I wrote a program that will let you create a region from image file format like .jpg, .bmp, .gif, .mpg, .ico, etc. FASTER than any of the code posted on CodeGuru or any other site on the Internet. You can read my article on creating regions and download the FREE sample project at:
    http://www.codearchive.com/~sergio/slickskins.htm

    Best regards,
    Bill SerGio
    sergio@codearchive.com

    Reply
  • problem loading 256 bit bitmaps

    Posted by Legacy on 10/31/2000 12:00am

    Originally posted by: Shaun Wilde

    I had a problem loading a 256 colour bitmap however I fixed it by doing the following
    
    


    in 'void CBmp2RgnDlg::OnChangeSrc()' change

    LoadBitmap (LPCSTR(m_strSrc),&m_pBitmap);

    to

    m_pBitmap = CBitmap::FromHandle((HBITMAP)LoadImage(NULL,m_strSrc,IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION|LR_DEFAULTSIZE|LR_LOADFROMFILE));

    I suspect the problem came from the LoadBitmap code (see other code guru articles on bitmap loading)

    Reply
  • More info about region file please

    Posted by Legacy on 03/29/2000 12:00am

    Originally posted by: Robert Christiaanse

    Hello,

    I have created a program in Delphi which does exactly the same thing. It is funny to see that it is almost the same way implemented. The difference is, I create a list of the points of the polygon (region) and show it to the user.
    (12,13),
    (17,15),
    (11,3),
    (14,17)

    A programmer can define an array of points for his region an than use the Windows Api function to create it.

    You create a region file. Can you tell me how I should read this file? What is the format? (Maybe I can find it in your code, but he, I am a (not so clever) Delphi programmer.

    Thanx. Good job!

    Reply
  • A Similar Tool

    Posted by Legacy on 05/14/1999 12:00am

    Originally posted by: Richard de Oude

    I have written a similar tool to this and posted it in the Tool section.

    Reply
  • Solution to NT and Write failure problems

    Posted by Legacy on 04/14/1999 12:00am

    Originally posted by: Paul Martinsen

    I think I have found the problem that has causes the assertion failure and the empty region files. In
    CDlgPreview::Setup(...) it is necessary to create a copy of the region for the SetWindowRegion function. This
    is because SetWindowRegion takes ownership of the region passed to it. The documentation recommends that you
    don't use the handle passed to SetWindowRegion afterward. To fix this problem make the following change in
    CDlgPreview::Setup:
    
    

    Change
    if (CreateRegion (m_pRgn,m_pBitmap,col))
    { if (!SetWindowRgn ((HRGN)*m_pRgn,TRUE))
    MessageBox ("failed to set the region for"
    " this window",MSGCAPTION,MB_OK);
    }

    to:

    if (CreateRegion (m_pRgn,m_pBitmap,col))
    { CRgn rgnCopy;
    rgnCopy.CreateRectRgn(0,0,10,10);
    rgnCopy.CopyRgn(m_pRgn);

    if (!SetWindowRgn ((HRGN)rgnCopy.Detach(),TRUE))
    MessageBox ("failed to set the region for"
    " this window",MSGCAPTION,MB_OK);
    }

    HTH
    Kind Regards,
    Paul Martinsen.

    Reply
  • This tool doesn't work under windows NT

    Posted by Legacy on 03/02/1999 12:00am

    Originally posted by: N. Albers

    Hello,

    It's a nice tool, but is doesn't work under Windows NT.
    It gives an Assertion falure on:

    Bmp2RgnDlg.cpp line 232:

    nSize=pRgn->GetRegionData (NULL,0);
    ASSERT (nSize);

    Something goes wrong here under NT.

    Regards,

    N. Albers

    Reply
  • Write Failure

    Posted by Legacy on 01/28/1999 12:00am

    Originally posted by: Travis Miller

    When attempting to write the region file, I gave
    
    it a file name, but when the file is written there
    is no data contained in the file. Bug?

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • IBM Worklight is a mobile application development platform that lets you extend your business to mobile devices. It is designed to provide an open, comprehensive platform to build, run and manage HTML5, hybrid and native mobile apps.

  • On-demand Event Event Date: October 23, 2014 Despite the current "virtualize everything" mentality, there are advantages to utilizing physical hardware for certain tasks. This is especially true for backups. In many cases, it is clearly in an organization's best interest to make use of physical, purpose-built backup appliances rather than relying on virtual backup software (VBA - Virtual Backup Appliances). Join us for this webcast to learn why physical appliances are preferable to virtual backup appliances, …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds