近日在项目中,一直被一个问题搞得头大的很,美工要把按钮图片弄成不规则的,但是在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. 14.multi_match+most-fields策略

    主要知识点 most-fields策略的用法 most-fields策略和best-fields的比较         best-fields策略:将某一个field匹配尽可能多的关键词的doc优先返 ...

  2. 腾讯云,搭建LAMP服务

    lamp (Web应用软件) 编辑 Linux+Apache+Mysql/MariaDB+Perl/PHP/Python一组常用来搭建动态网站或者服务器的开源软件,本身都是各自独立的程序,但是因为常被 ...

  3. everyday two problems / 3.11 - 3.17

    7日共14题,因为3.14两题相同,所以实际总共13题 13道题分为难易两部分,没有按照时间顺序排列题目 A.易: 1.覆盖数字的数量 题意: 给出一段从A - B的区间S(A,B为整数) 这段区间内 ...

  4. sencha touch 2制作滑动DataView(无缝list)

    Ext.define('App.view.Sections', { extend: 'Ext.dataview.DataView', xtype: 'sectionslist', id: 'mainl ...

  5. K - Count the string kmp_Next数组应用

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

  6. 使用Visual Studio Code调试Electron主进程

    1.打开VS Code,使用文件->打开,打开程序目录 2.切换到调试选项卡 3.打开launch.json配置文件 4.在“附加到进程”节点上增加localhost配置 5.使用命令行启动el ...

  7. monitor cursor

    客户提出了一个需求,他们改进了自己的程序,想证明程序现在open cursor变少了,也就是说程序运行过程中 open cursor的峰值变小了. 我写了一个脚本来进行这个监控. oracle[aaa ...

  8. Tween公式

    Tween公式 4个参数 t:current time(当前时间) b:beginning value(初始值) c: change in value(变化量) d:duration(持续时间) re ...

  9. C语言编程入门——程序练习(上)

    大家能够敲写一下以下的练习代码.看下执行结果,都非常easy.关键要理解. if: # include <stdio.h> int main(void) { int i = 1; i = ...

  10. UVA LIVE 7146 Defeat the Enemy

    这个题跟codeforces 556 D Case of Fugitive思路一样 关于codeforces 556 D Case of Fugitive的做法的链接http://blog.csdn. ...