bitmap实现背景透明
近日在项目中,一直被一个问题搞得头大的很,美工要把按钮图片弄成不规则的,但是在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实现背景透明的更多相关文章
- 使IE6下PNG背景透明的七种方法任你选
原文地址:http://blog.csdn.net/mosliang/article/details/6760028 相信如何解决png在ie6下透明的问题困扰了很多人.为了追求更好的页面效果,很多人 ...
- C# WinForm 自定义控件,DataGridView背景透明,TabControl背景透明
注意: 以下代码,属性直接赋值的语法糖要vs2015以上才支持. using System.ComponentModel; using System.Drawing; using System. ...
- 【原】CSS实现背景透明,文字不透明,兼容所有浏览器
11.11是公司成立的日子,16岁啦,我呢3岁半,感谢公司给了这样一个平台,让我得以学习和成长,这里祝愿公司发展越来越好~ 进入主题,每年11月11号是光棍节,产生于校园,本来只是一流传于年轻人的娱乐 ...
- CSS实现背景透明,文字不透明(兼容各浏览器)
在 FF/Chrome 等较新的浏览器中可以使用css属性background- color的rgba轻松实现背景透明,而文字保持不透明.而IE6/7/8浏览器不支持rgba,只有使用IE的专属滤镜f ...
- CSS实现背景透明,文字不透明,兼容所有浏览器
11.11是公司成立的日子,16岁啦,我呢3岁半,感谢公司给了这样一个平台,让我得以学习和成长,这里祝愿公司发展越来越好~ 进入主题,每年11月11号是光棍节,产生于校园,本来只是一流传于年轻人的娱乐 ...
- css 背景透明文字(内容)不透明三种实现方法
好久没写博客了.以前还想着最少一个月抽空写几篇.结果没做到O(∩_∩)O~~.好吧.现在努力,继续坚持. 看着以前写的东西,感觉自己在逐渐成长. 先上图: 本文主要记录如上图一样的.文字或内容不透明, ...
- VC++ CStatic控件背景透明且改变其文本时,文字重叠解决方法
最近在项目中将CStatic控件设置为背景透明且在一个定时器函数改变其文本,结果CStatic的文字重叠了.解决该问题的方案是:从CStatic类派生自己的静态文本控件. 其实设置背景透明,也就是在C ...
- 用Photoshop处理图片使背景透明
用Photoshop处理图片使背景透明 打开一张图片 双击背景或者右键背景图层,新建一个图层, 选择魔棒工具,单击图片, 会自动选择颜色相近的范围 按下键盘的delete键,就可以删除魔棒所选择的区域 ...
- android 自定义Dialog背景透明及显示位置设置
先贴一下显示效果图,仅作参考: 代码如下: 1.自定义Dialog public class SelectDialog extends AlertDialog{ public SelectDialog ...
随机推荐
- json 添加 和删除两种方法
<script> var test = { name: "name", age: "12" }; var countrys = { "ne ...
- (JS,JAVA,MySql)去除小数后多余的0
分别通过JS,JAVA和MySql实现去除小数后多余的0 1. JS方法 /** *去除小数点后多余的0 */ function cutZero(old) { //拷贝一份 返回去掉零的新串 old ...
- 关于MySQL中自增的理解和设置
show create table t10;--查看表的创建结果 show create table t10\G;--竖列查看 --设置自增为20 ); insert into t2(name) va ...
- MySql 日志查看与设置
错误日志log-errol 开启方式:在my.ini的[mysqld]选项下:添加代码:log-error=E:\log-error.txt 记录内容:主要是记录启动.运行或停止mysqld时出现的致 ...
- 338. Counting Bits(动态规划)
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the ...
- 手写DAO框架(二)-开发前的最后准备
-------前篇:手写DAO框架(一)-从“1”开始 --------- 前言:前篇主要介绍了写此框架的动机,把主要功能点大致介绍了一下.此篇文章主要介绍开发前最后的一些准备.主要包括一些基础知识点 ...
- 用Twebbrowser做可控编辑器与MSHTML
首先要明白mshtml的属性方法: {IHTMLDocument2 方法:} write //写入 writeln //写入并换行 open //打开一个流,以收集 document.write 或 ...
- Jzzhu and Numbers
Jzzhu and Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- 清北学堂模拟赛d6t3 反击数
分析:显然是一道数位dp题,不过需要一些奇怪的姿势.常规的数位dp能统计出一个区间内满足条件的数的个数,可是我们要求第k个,怎么办呢?转化为经典的二分问题,我们二分当前数的大小,看它是第几大的,就可以 ...
- UVa - 12661 - Funny Car Racing
先上题目: 12661 Funny Car RacingThere is a funny car racing in a city with n junctions and m directed ro ...