c# 制作出任意不规则按钮! (原理根据背景图绘制button)
using system.drawing.drawing2d;
private void button3_paint(object sender, system.windows.forms.painteventargs e)
{ this.button3.cursor = cursors.hand;
bitmap bmpbob =(bitmap)this.button3.image;
graphicspath graphicspath = calculatecontrolgraphicspath(bmpbob);
this.button3.region = new region(graphicspath);
}
private static graphicspath calculatecontrolgraphicspath(bitmap bitmap)
{
graphicspath graphicspath = new graphicspath();
color colortransparent = bitmap.getpixel(0, 0);
int colopaquepixel = 0;
for(int row = 0; row < bitmap.height; row ++)
{
colopaquepixel = 0;
for(int col = 0; col < bitmap.width; col ++)
{
if(bitmap.getpixel(col, row) != colortransparent)
{
colopaquepixel = col;
int colnext = col;
for(colnext=colopaquepixel; colnext<bitmap.width; colnext++)
if(bitmap.getpixel(colnext, row) == colortransparent)
break;
graphicspath.addrectangle(new rectangle(colopaquepixel,
row, colnext - colopaquepixel, 1));
col = colnext;
}
}
}
return graphicspath;
}
新闻热点
疑难解答