近日在项目中,一直被一个问题搞得头大的很,美工要把按钮图片弄成不规则的,但是在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. js 随机数范围

    Math.floor(Math.random()*(high-low+1) +low)

  2. 使用python的几个小经验(查看文档)

    好久没有水博客了,未来再过20天不到的时间又得参加软考,今天终于得好好水一发帖子 关于Python,很多人包括我之前都不知道怎么找文档,现在有一个好办法,就是在命令行模式下调用pydoc –p xxx ...

  3. BZOJ 4285 使者 (CDQ分治+dfs序)

    题目传送门 题目大意:给你一棵树,有三种操作,在两个点之间连一个传送门,拆毁一个已有的传送门,询问两个点之间的合法路径数量.一条合法路径满足 1.经过且仅经过一个传送门 2.不经过起点终点简单路径上的 ...

  4. python文件头的含义

    一.指定解释器及其路径 在Linux\Mac上,可以用./文件路径直接运行.py文件 这时,需要在python文件开头指定解释器及其路径 #!/usr/bin/python 这样系统就直接按pytho ...

  5. Python中的可迭代对象/迭代器/For循环工作机制/生成器

    本文分成6个部分: 1.iterable iterator区别 2.iterable的工作机制 3.iterator的工作机制 4.for循环的工作机制 5.generator的原理 6.总结 1.i ...

  6. 曾经遇过的sql问题

    曾经遇过的sql问题 问题一: 语句1: select SUM(level) from Comment 语句2: ELSE SUM(level) END as totalLevel from Comm ...

  7. java中的redis工具类

    1.redis基础类 package com.qlchat.component.redis.template; import javax.annotation.PostConstruct; impor ...

  8. 3、ceph-deploy之配置使用文件系统

    我们在admin节点执行下述操作,来配置使用ceph集群的文件系统 必备条件 1.在ceph-client节点安装ceph ceph-deploy install ceph-client 2.确认ce ...

  9. mybatis源码阅读-执行器Executor(四)

    说明 前面二看到了 sqlSession最终是找到MapperStatement然后委托给Executer执行的 Executer到底做了什么 接口定义 public interface Execut ...

  10. 大逃亡(escape.*)

    给出数字N(1<=N<=10000),X(1<=x<=1000),Y(1<=Y<=1000),代表有N个敌人分布一个X行Y列的矩阵上,矩形的行号从0到X-1,列号从 ...