C#_SRC:Shaped Form from Bitmap
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
public class ShapedForm : Form
{
#region Bob Powell's GDI+ FAQ Banner
/******************************************************************************** * This code is part of Bob Powell's GDI+ FAQ
* You may use this code free from royalties in a commercial project on condition
* that you buy yourself a book or DVD from the following link to Amazon or make
* a donation of your choice.
*
* http://www.amazon.com/exec/obidos/redirect-home/bobpowelnet
*
* You can make a donation to the upkeep of the GDI+ FAQ by using
* PayPal from the following link.
*
* https://www.paypal.com/xclick/business=bob%40bobpowell.net&item_name=GDI%
2B+FAQ&item_number=1&no_note=1&tax=0¤cy_code=USD
*
* If this code helped you, just imagine what I can do for you as a part of
* your team.
*
* My consulting rates start at just $30 an hour and I sell blocks of hours
* to provide you with projects, architecture, programming solutions and mentoring
*
* http://www.bobpowell.net
* ********************************************************************************
* License:
* This code is provided with no warranty and is not guaranteed to be free from defects
* or fit for merchantability or for any other purpose. By incorporating this code
* into your own projects you do so at your own risk and agree to hold Robert W. Powell
* free from responsibility for all damages that may arise from such use
* If you redistribute this source code or any portion of it in any human readable form
* you must include the full text of this information panel and license.
* ********************************************************************************/
#endregion
private Region GetRegion(Bitmap _img, Color color)
{
Color _matchColor=Color.FromArgb(color.R,color.G,color.B);
System.Drawing.Region rgn= new Region();
rgn.MakeEmpty();
Rectangle rc=new Rectangle(0,0,0,0);
bool inimage=false;
for(int y=0; y<_img.Height;y++)
{
for(int x=0;x<_img.Width;x++)
{
if(!inimage)
{
if(_img.GetPixel(x,y)!=_matchColor)
{
inimage=true;
rc.X=x;
rc.Y=y;
rc.Height=1;
}
}
else
{
if(_img.GetPixel(x,y)==_matchColor)
{
inimage=false;
rc.Width=x-rc.X;
rgn.Union(rc);
}
}
}
if(inimage)
{
inimage=false;
rc.Width=_img.Width-rc.X;
rgn.Union(rc);
}
}
return rgn;
}
public ShapedForm(Bitmap background, Color transparentColor)
{
this.FormBorderStyle = FormBorderStyle.None;
this.BackgroundImage = background;
this.Region = this.GetRegion(background, transparentColor);
}
}
Friday, August 04, 2006