全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
using System.Drawing.Drawing2D;private void button3_Paint(object sender, System.Windows.Forms.PaintEventArgs e) this.button3.Cursor = Cursors.Hand; Bitmap bmpBob =(Bitmap)this.button3.Image; GraphicsPath graphicsPath = CalculateControlGraphicsPath(bmpBob); this.button3.Region = new Region(graphicsPath); private static GraphicsPath CalculateControlGraphicsPath(Bitmap bitmap) GraphicsPath graphicsPath = new GraphicsPath(); Color colorTransparent = bitmap.GetPixel(0, 0); int colOpaquePixel = 0; for(int row = 0; row bitmap.Height; row +) colOpaquePixel = 0; for(int col = 0; col bitmap.Width; col +) if(bitmap.GetPixel(col, row) != colorTransparent) colOpaquePixel = col; int colNext = col;/Html/chtml 上面的代码测试效果不好,放上一个老外写的类,下过好一点:using System;using System.Drawing;using System.Drawing.Drawing2D;using System.Windows.Forms;namespace BitmapRegionTest / / Summary description for BitmapRegion. / public class BitmapRegion public BitmapRegion() / / Create and apply the region on the supplied control / / The Control object to apply the region to / The Bitmap object to create the region from public static void CreateControlRegion(Control control, Bitmap bitmap) / Return if control and bitmap are null if(control = null | bitmap = null) return; / Set our controls size to be the same as the bitmap control.Width = bitmap.Width; control.Height = bitmap.Height; / Check if we are dealing with Form here if(control is System.Windows.Forms.Form) / Cast to a Form object Form form = (Form)control; / Set our forms size to be a little larger that the bitmap just / in case the forms border style is not set to none in the first place form.Width += 15; form.Height += 35; / No border form.FormBorderStyle = FormBorderStyle.None; / Set bitmap as the background image form.BackgroundImage = bitmap; / Calculate the graphics path based on the bitmap supplied GraphicsPath graphicsPath = CalculateControlGraphicsPath(bitmap); / Apply new region form.Region = new Region(graphicsPath); / Check if we are dealing with Button here else if(control is System.Windows.Forms.Button) / Cast to a button object Button button = (Button)control; / Do not show button text button.Text = ; / Change cursor to hand when over button button.Cursor = Cursors.Hand; / Set background image of button button.BackgroundImage = bitmap; / Calculate the graphics path based on the bitmap supplied GraphicsPath graphicsPath = CalculateControlGraphicsPath(bitmap); / Apply new region button.Region = new Region(graphicsPath); / / Calculate the graphics path that representing the figure in the bitmap / excluding the transparent color which is the top left pixel. / / The Bitmap object to calculate our graphics path from / Calculated graphics path private static GraphicsPath CalculateControlGraphicsPath(Bitmap bitmap) / Create GraphicsPath for our bitmap calculation GraphicsPath graphicsPath = new GraphicsPath(); / Use the top left pixel as our transparent color Color colorTransparent = bitmap.GetPixel(0, 0); / This is to store the column value where an opaque pixel is first found. / This value will determine where we start scanning for trailing opaque pixels. int colOpaquePixel = 0; / Go through all rows (Y axis) for(int row = 0; row bitmap.Height; row +) / Reset value colOpaquePixel = 0; / Go through all columns (X axis) for(int col = 0; col bitmap.Width; col +) / If this is an opaque pixel, mark it and search for anymore trailing behind if(bitmap.GetPixel(col, row) != colorTransparent) / Opaque pixel found, mark current position colOpaquePixel = col; / Create another variable to set the current pixel position int colNext = col; / Starting from current found opaque pixel, search for anymore opaque pixels / trailing behind, until a transparent pixel is found or minimum width is reached for(colNext = colOpaquePixel; colNext bitmap.Width; colNext +) if(bitmap.GetPixel(colNext, row) = colorTransparent) break; / Form a rectangle for line of opaque pixels found and add it to our graphics path graph
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025出国留学合同协议书
- 解除区域代理协议合同
- 西兰花菜购销合同范本
- 调味品销售合同协议书
- 2025年中医助理真题试卷及答案
- 基于教学实践的中学美术教师综合能力养成路径探究
- 基于效率与成本双目标的A公司冷链物流仓储中心布局深度优化研究
- 北交大机械考研真题及答案
- 基于改进布谷鸟算法的桁架结构优化:理论、实践与创新
- 基于改进RNN的风电功率短期预测算法:模型优化与实证研究
- GB/T 5072-2023耐火材料常温耐压强度试验方法
- 横断面计算Excel土方断面速算表
- 15D502 等电位联结安装
- 11《答谢中书书》知识点整理
- 创意的表达 课件-2023-2024学年高中通用技术地质版(2019)必修《技术与设计1 》
- 九年级数学期中考试质量分析【精选】
- 基于BIM基数的机电安装工程降本提质增效
- GB/T 10003-2008普通用途双向拉伸聚丙烯(BOPP)薄膜
- 高位自卸汽车设计计算说明书-毕业设计
- BOSA测试培训课件
- 【国标图集】13J404电梯自动扶梯自动人行道
评论
0/150
提交评论