近日在项目中,一直被一个问题搞得头大的很,美工要把按钮图片弄成不规则的,但是在winform里实现又不仅仅是使用简单的png图片而已。在网上找到一些方法,稍微改了一点加工成项目所需。

贴出解决方案,以供日后使用:

 public class BitmapRegion
{
//创建支持位图区域的控件(目前有button,form,imagebutton)
public static void CreateControlRegion(Control control, Bitmap bitmap)
{
//判断控件是否存在
if (control == null )//|| bitmap == null
return; //控件大小设置为位图大小
control.Width = bitmap.Width;
control.Height = bitmap.Height; // 档控件为form时
if (control is System.Windows.Forms.Form)
{
//强制转化为form
Form form = (Form)control; //当FORM的边界FormBorderStyle不为NONE时,应将FORM的大小设置成比位图大小稍大一点
form.Width += ;
form.Height += ;
//设置form为没有边界
form.FormBorderStyle = FormBorderStyle.None; //将位图设置为控件背景图
form.BackgroundImage = bitmap; //计算位图中不透明的部分
GraphicsPath graphicsPath = CalculateControlGraphicsPath(bitmap); //应用新的区域
form.Region = new Region(graphicsPath);
}
//当控件是panel时
else if (control is System.Windows.Forms.Panel)
{
//强制转化为panel
Panel form = control as Panel; //当FORM的边界FormBorderStyle不为NONE时,应将FORM的大小设置成比位图大小稍大一点
//form.Width += 15;
//form.Height += 35;
//form.Width += 15;
//form.Height += 35;
// 设置panel为没有边界
form.BorderStyle = BorderStyle.None; //将位图设置为控件背景图
form.BackgroundImage = bitmap; //计算位图中不透明的部分
GraphicsPath graphicsPath = CalculateControlGraphicsPath(bitmap); //应用新的区域
form.Region = new Region(graphicsPath);
}
//当控件是button时
else if (control is System.Windows.Forms.Button)
{ // 强制转化为button
Button button = (Button)control; // 不显示button文字
button.Text = ""; // 改变cursor的style
button.Cursor = Cursors.Hand; // 设置button的背景图片
button.BackgroundImage = bitmap; // 计算图中不透明部分
GraphicsPath graphicsPath = CalculateControlGraphicsPath(bitmap); // 应用新的区域
button.Region = new Region(graphicsPath);
}
//当控件是imagebutton时
else if (control is M3Host.view.utils.ImageButton)
{
M3Host.view.utils.ImageButton button = control as M3Host.view.utils.ImageButton; // 不显示文字
button.Text = ""; // 改变模式和设置正常状态下的图片为空
button.Cursor = Cursors.Hand;
button.NormalImage = null;
// 设置位图为背景图片
button.BackgroundImage = bitmap; // 计算图中不透明部分
GraphicsPath graphicsPath = CalculateControlGraphicsPath(bitmap); // 应用新的区域
button.Region = new Region(graphicsPath);
}
} // 计算位图中不透明部分
private static GraphicsPath CalculateControlGraphicsPath(Bitmap bitmap)
{
// 创建graphicsPath
GraphicsPath graphicsPath = new GraphicsPath(); // 取得左上角的第一个点作为透明点
Color colorTransparent = bitmap.GetPixel(, ); // 第一个找到的点
int colOpaquePixel = ; // 遍历所有Y方向的点
for (int row = ; row < bitmap.Height; row++)
{
// 重设
colOpaquePixel = ; // 遍历X方向的所有点
for (int col = ; 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; // 将不透明点加到graphics path
graphicsPath.AddRectangle(new Rectangle(colOpaquePixel,
row, colNext - colOpaquePixel, ));
//覆盖前一个点
col = colNext;
}
}
} return graphicsPath;
}
}

使用方法:Bitmap bmp = new Bitmap("images\\move1.bmp");
BitmapRegion.CreateControlRegion(控件名, bmp);

再附上将控件制成bitmap的方法

Bitmap bmp = new Bitmap(Width, Height);
Rectangle r = new Rectangle(0, 0, Width,Height);//width和height均为控件的宽和高
控件名.DrawToBitmap(bmp, r);

  记录以上。。

bitmap实现背景透明的更多相关文章

  1. 使IE6下PNG背景透明的七种方法任你选

    原文地址:http://blog.csdn.net/mosliang/article/details/6760028 相信如何解决png在ie6下透明的问题困扰了很多人.为了追求更好的页面效果,很多人 ...

  2. C# WinForm 自定义控件,DataGridView背景透明,TabControl背景透明

     注意: 以下代码,属性直接赋值的语法糖要vs2015以上才支持.   using System.ComponentModel; using System.Drawing; using System. ...

  3. 【原】CSS实现背景透明,文字不透明,兼容所有浏览器

    11.11是公司成立的日子,16岁啦,我呢3岁半,感谢公司给了这样一个平台,让我得以学习和成长,这里祝愿公司发展越来越好~ 进入主题,每年11月11号是光棍节,产生于校园,本来只是一流传于年轻人的娱乐 ...

  4. CSS实现背景透明,文字不透明(兼容各浏览器)

    在 FF/Chrome 等较新的浏览器中可以使用css属性background- color的rgba轻松实现背景透明,而文字保持不透明.而IE6/7/8浏览器不支持rgba,只有使用IE的专属滤镜f ...

  5. CSS实现背景透明,文字不透明,兼容所有浏览器

    11.11是公司成立的日子,16岁啦,我呢3岁半,感谢公司给了这样一个平台,让我得以学习和成长,这里祝愿公司发展越来越好~ 进入主题,每年11月11号是光棍节,产生于校园,本来只是一流传于年轻人的娱乐 ...

  6. css 背景透明文字(内容)不透明三种实现方法

    好久没写博客了.以前还想着最少一个月抽空写几篇.结果没做到O(∩_∩)O~~.好吧.现在努力,继续坚持. 看着以前写的东西,感觉自己在逐渐成长. 先上图: 本文主要记录如上图一样的.文字或内容不透明, ...

  7. VC++ CStatic控件背景透明且改变其文本时,文字重叠解决方法

    最近在项目中将CStatic控件设置为背景透明且在一个定时器函数改变其文本,结果CStatic的文字重叠了.解决该问题的方案是:从CStatic类派生自己的静态文本控件. 其实设置背景透明,也就是在C ...

  8. 用Photoshop处理图片使背景透明

    用Photoshop处理图片使背景透明 打开一张图片 双击背景或者右键背景图层,新建一个图层, 选择魔棒工具,单击图片, 会自动选择颜色相近的范围 按下键盘的delete键,就可以删除魔棒所选择的区域 ...

  9. android 自定义Dialog背景透明及显示位置设置

    先贴一下显示效果图,仅作参考: 代码如下: 1.自定义Dialog public class SelectDialog extends AlertDialog{ public SelectDialog ...

随机推荐

  1. 【Linq】标准查询操作符

    A.1 聚合 聚合操作符(见表A-1),所有的结果只有一个值而不是一个序列. Average 和 Sum 针对数值 (任何内置数值类型)序列或使用委托从元素值转换为内置数值类型的元素序列. Min 和 ...

  2. Java 初学者

    在有C++和C#基础之下开始学习Java,主要记录了一些和C++C#不同的或不知到的点 栈对象必须初始化,否则会报错.(其他的则有默认值) byte占用8位,char占用16位 接口默认为public ...

  3. PAT 1096. Consecutive Factors

    Among all the factors of a positive integer N, there may exist several consecutive numbers. For exam ...

  4. linux学习3-简单磁盘管理

    简单的磁盘管理 下面涉及的命令具有一定的危险性,操作不当可能会丢失你的个人数据,初学者建议在虚拟环境中进行操作 通常情况下,这一小节应该直接将如何挂载卸载磁盘,如何格式化磁盘,如何分区,但如你所见,我 ...

  5. check_nrpe: ERROR - could not complete SSL handshake

    情景描述: 发现的问题是 在监控端执行 ./check_nrpe -H 被监控端ip 正常返回nrpe版本 在被监控端执行 ./check_nrpe -H 监控端ip 报错 check_nrpe: E ...

  6. CDOJ 888 Absurdistan Roads

    Absurdistan Roads Time Limit: 5678/3456MS (Java/Others)     Memory Limit: 65432/65432KB (Java/Others ...

  7. SCU Censor

    Censor frog is now a editor to censor so-called sensitive words (敏感词). She has a long text p . Her j ...

  8. Spring MVC-表单(Form)标签-隐藏字段(Hidden Field)示例(转载实践)

    以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_hidden.htm 说明:示例基于Spring MVC 4.1.6. 以下示例显 ...

  9. PHP array_fill()

    定义和用法 array_fill() 函数用给定的值填充数组,返回的数组有 number 个元素,值为 value.返回的数组使用数字索引,从 start 位置开始并递增.如果 number 为 0 ...

  10. CF #329 C

    C题我还以为是拉格朗日插值... 其实可以想象到,必须有这样一个函数,经过某一点时,其它圆相关的函数要为0. 于是,可以构造这样的一个函数,对于x有 (x/2)*(1-abs(t-i)+abs(1-a ...