public class BitmapRegion{//创建支持位图区域的控件(目前有button,form,imagebutton)public static void CreateControlRegion(Control control, Bitmap bitmap){//判断控件是否存在if (control == null )//|| bitmap == nullreturn;//控件大小设置为位图大小control.Width = bitmap.Width;control.Height = bitmap.Height;// 档控件为form时if (control is System.Windows.Forms.Form){//强制转化为formForm form = (Form)control;//当FORM的边界FormBorderStyle不为NONE时,应将FORM的大小设置成比位图大小稍大一点form.Width += 15;form.Height += 35;//设置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){//强制转化为panelPanel 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){// 强制转化为buttonButton    button = (Button)control;// 不显示button文字button.Text = "";// 改变cursor的stylebutton.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){// 创建graphicsPathGraphicsPath graphicsPath = new GraphicsPath();// 取得左上角的第一个点作为透明点Color colorTransparent = bitmap.GetPixel(0, 0);// 第一个找到的点int colOpaquePixel = 0;// 遍历所有Y方向的点for (int row = 0; row < bitmap.Height; row++){// 重设colOpaquePixel = 0;// 遍历X方向的所有点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;// 将不透明点加到graphics pathgraphicsPath.AddRectangle(new Rectangle(colOpaquePixel,row, colNext - colOpaquePixel, 1));//覆盖前一个点col = colNext;}}}return graphicsPath;}}

bitmap实现背景透明相关推荐

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

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

  2. VC设置CEdit控件背景透明、文字背景也透明

    开发环境:VC6, a dialog based MFC application. 主界面: 为对话框添加WM_CTLCOLOR消息响应函数: HBRUSH CDDlg::OnCtlColor(CDC ...

  3. VC 控件背景透明、文字背景透明

    Visual C++  开发实战宝典 功能:位图上显示文字,背景透明. void CP400Dlg::OnPaint()  { .... CBitmap bmp; bmp.LoadBitmap(IDB ...

  4. 利用GDI+实现gif图像背景透明

    要实现将GIF图像进行透明处理,需要调用三个函数, MakeTransparent函数为将图像数据进行透明处理的函数,GetEncoderClsid为获取clsid,StreamToByte为将流转换 ...

  5. CSS 背景(background)(背景颜色color、背景图片image、背景平铺repeat、背景位置position、背景附着、背景简写、背景透明、链接导航栏综合案例)

    1. 背景颜色(color) background-color:颜色值; 默认的值是 transparent 透明的 示例代码: <!DOCTYPE html> <html lang ...

  6. MFC 加入背景图片并让控件背景透明

    /*加入背景图片*/ BOOL CTOOLDlg::OnEraseBkgnd(CDC* pDC) {  // TODO: 在此加入消息处理程序代码和/或调用默认值  CDialog::OnEraseB ...

  7. 让Flash背景透明兼容Firefox、IE 6和IE 7的代码

    添加代码: <param name="wmode" value="transparent" > 到 <object>-</obje ...

  8. Sublime如何设置背景透明

    Sublime如何设置背景透明 下载sublime 透明背景插件 我用的是git下载插件: git clone https://github.com/vhanla/SublimeTextTrans.g ...

  9. 关于png、背景透明疑难杂症综合帖

    前言 在web重构中,为了追求视觉效果,会经常使用标签背景透明.透明的png图片等,可惜ie6未死,所以经常会有这样那样的问题出现,下面我总结一下ie6下各种怪症和解决方法. 标签背景透明 常规方法 ...

最新文章

  1. static关键字和内存使用
  2. 世界欠他一个图灵奖! LSTM之父的深度学习“奇迹之年”
  3. 什么是Mac上的JDK路径? [重复]
  4. “ px”,“ dip”,“ dp”和“ sp”有什么区别?
  5. Javascript JSON 序列化和反序列化
  6. sqlite中常见的问题总结
  7. Java的transient关键字
  8. CSU 1112 机器人的指令
  9. 95-080-054-源码-启动-启动TaskManager
  10. 吴韧谈异构智能芯片:比谷歌能耗低,比华为寒武纪计算力高
  11. SpringBoot整合Mail之设置发件人昵称
  12. 破解完全入门篇 第七章-寻找软件的注册码
  13. IEEE年度薪酬报告发布!美国程序员薪资中位数7年来首次下降2.4万
  14. Matrix Concatenate 矩阵串联
  15. 系统文件IO与标准文件IO
  16. C#-Excel导入工资条群发邮箱
  17. latex出现File ended while scanning use of \frame.错误
  18. 基带传输与频带传输(关系与区别)
  19. 京东数科前端岗位面历
  20. ubuntu系统查询硬盘的品牌、型号、序列号

热门文章

  1. 登录功能通用测试用例设计
  2. mac电脑配置adb
  3. 十大排序算法(演示动画)
  4. JavaScript的三种弹出提示框(alert、confirm、prompt)
  5. 联合书单 | 6本书,李诞带你感受数学之美
  6. 走进GBase 8a之简介
  7. jsp mysql下拉框联动_phpajax下拉框动态联动问题-爱问知识人
  8. 六键无冲和全键无冲哪个好_机械键盘6键无冲突和全无冲突的差别
  9. UITextView文字怎样居上
  10. Vue的双向数据绑定原理是什么?(前端面试题)