七十七c#Winform自定义控件-采样控件

前提

入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。

GitHub:https://github.com/kwwwvagaa/NetWinformControl

码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

如果觉得写的还行,请点个 star 支持一下吧

欢迎前来交流探讨: 企鹅群568015492 

企鹅群568015492

来都来了,点个【推荐】再走吧,谢谢

NuGet

Install-Package HZH_Controls

目录

https://www.cnblogs.com/bfyx/p/11364884.html

用处及效果

注意观察各个控件交叠的地方,是不是发现他们没有遮挡?这就是这个控件的妙处了。

分享图片

准备工作

先说明一下这个控件的作用,很多时候我们需要一个图片类型的控件,但是有需要密集的放在一起,如果单纯的设置背景图或image的话  交叠在一起的部分就会存在遮挡现象,所有就有了这个控件。

该控件可以根据设置的采样图片来裁剪有用的绘图区域,这样的好处就是在交叠的时候,无用区域不会遮挡。

这个用GDI+画的,另外也用到了一点三角函数,不明白的话 可以先百度下

开始

添加一个类UCSampling ,继承UserControl

添加属性

 1   /// <summary>
 2         /// The sampling imag
 3         /// </summary>
 4         private Bitmap samplingImag = null;
 5         /// <summary>
 6         /// Gets or sets the sampling imag.
 7         /// </summary>
 8         /// <value>The sampling imag.</value>
 9         [Browsable(true),Category("自定义属性"),Description("采样图片"),Localizable(true)]
10         public Bitmap SamplingImag
11         {
12             get { return samplingImag; }
13             set
14             {
15                 samplingImag = value;
16                 ResetBorderPath();
17                 Invalidate();
18             }
19         }
20 
21         /// <summary>
22         /// The transparent
23         /// </summary>
24         private Color? transparent = null;
25 
26         /// <summary>
27         /// Gets or sets the transparent.
28         /// </summary>
29         /// <value>The transparent.</value>
30         [Browsable(true),Description("透明色,如果为空,则使用0,0坐标处的颜色"),Localizable(true)]
31         public Color? Transparent
32         {
33             get { return transparent; }
34             set
35             {
36                 transparent = value;
37                 ResetBorderPath();
38                 Invalidate();
39             }
40         }
41 
42         /// <summary>
43         /// The alpha
44         /// </summary>
45         private int alpha = 50;
46 
47         /// <summary>
48         /// Gets or sets the alpha.
49         /// </summary>
50         /// <value>The alpha.</value>
51         [Browsable(true),Description("当作透明色的透明度,小于此透明度的颜色将被认定为透明,0-255"),Localizable(true)]
52         public int Alpha
53         {
54             get { return alpha; }
55             set
56             {
57                 if (value < 0 || value > 255)
58                     return;
59                 alpha = value;
60                 ResetBorderPath();
61                 Invalidate();
62             }
63         }
64 
65         /// <summary>
66         /// The color threshold
67         /// </summary>
68         private int colorThreshold = 10;
69 
70         /// <summary>
71         /// Gets or sets the color threshold.
72         /// </summary>
73         /// <value>The color threshold.</value>
74         [Browsable(true),Description("透明色颜色阀值"),Localizable(true)]
75         public int ColorThreshold
76         {
77             get { return colorThreshold; }
78             set
79             {
80                 colorThreshold = value;
81                 ResetBorderPath();
82                 Invalidate();
83             }
84         }
85 
86         /// <summary>
87         /// The bit cache
88         /// </summary>
89         private Bitmap _bitCache;

在大小改变或图片改变时重新计算边界

 1  /// <summary>
 2         /// The m border path
 3         /// </summary>
 4         GraphicsPath m_borderPath = new GraphicsPath();
 5 
 6         /// <summary>
 7         /// Handles the SizeChanged event of the UCSampling control.
 8         /// </summary>
 9         /// <param name="sender">The source of the event.</param>
10         /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
11         void UCSampling_SizeChanged(object sender,EventArgs e)
12         {
13             ResetBorderPath();
14         }
15 
16         /// <summary>
17         /// Resets the border path.
18         /// </summary>
19         private void ResetBorderPath()
20         {
21             if (samplingImag == null)
22             {
23                 m_borderPath = this.ClientRectangle.CreateRoundedRectanglePath(5);
24             }
25             else
26             {
27                 var bit = new Bitmap(this.ClientRectangle.Width,this.ClientRectangle.Height);
28                 using (var bitg = Graphics.FromImage(bit))
29                 {
30                     bitg.DrawImage(samplingImag,this.ClientRectangle,0,0,samplingImag.Width,samplingImag.Height,GraphicsUnit.Pixel);
31                 }
32                 _bitCache = bit;
33                 m_borderPath = new GraphicsPath();
34                 List<PointF> lstPoints = GetBorderPoints(bit,transparent ?? samplingImag.GetPixel(0,0));
35                 m_borderPath.AddLines(lstPoints.ToArray());
36                 m_borderPath.CloseAllFigures();
37             }
38         }
39 
40         /// <summary>
41         /// Gets the border points.
42         /// </summary>
43         /// <param name="bit">The bit.</param>
44         /// <param name="transparent">The transparent.</param>
45         /// <returns>List&lt;PointF&gt;.</returns>
46         private List<PointF> GetBorderPoints(Bitmap bit,Color transparent)
47         {
48             float diameter = (float)Math.Sqrt(bit.Width * bit.Width + bit.Height * bit.Height);
49             int intSplit = 0;
50             intSplit = (int)(7 - (diameter - 200) / 100);
51             if (intSplit < 1)
52                 intSplit = 1;
53             List<PointF> lstPoint = new List<PointF>();
54             for (int i = 0; i < 360; i += intSplit)
55             {
56                 for (int j = (int)diameter / 2; j > 5; j--)
57                 {
58                     Point p = GetPointByAngle(i,j,new PointF(bit.Width / 2,bit.Height / 2));
59                     if (p.X < 0 || p.Y < 0 || p.X >= bit.Width || p.Y >= bit.Height)
60                         continue;
61                     Color _color = bit.GetPixel(p.X,p.Y);
62                     if (!(((int)_color.A) <= alpha || IsLikeColor(_color,transparent)))
63                     {
64                         if (!lstPoint.Contains(p))
65                         {
66                             lstPoint.Add(p);
67                         }
68                         break;
69                     }
70                 }
71             }
72             return lstPoint;
73         }
74 
75         /// <summary>
76         /// Determines whether [is like color] [the specified color1].
77         /// </summary>
78         /// <param name="color1">The color1.</param>
79         /// <param name="color2">The color2.</param>
80         /// <returns><c>true</c> if [is like color] [the specified color1]; otherwise,<c>false</c>.</returns>
81         private bool IsLikeColor(Color color1,Color color2)
82         {
83             var cv = Math.Sqrt(Math.Pow((color1.R - color2.R),2) + Math.Pow((color1.G - color2.G),2) + Math.Pow((color1.B - color2.B),2));
84             if (cv <= colorThreshold)
85                 return true;
86             else
87                 return false;
88         }
 1  #region 根据角度得到坐标    English:Get coordinates from angles
 2         /// <summary>
 3         /// 功能描述:根据角度得到坐标    English:Get coordinates from angles
 4         /// 作  者:HZH
 5         /// 创建日期:2019-09-28 11:56:25
 6         /// 任务编号:POS
 7         /// </summary>
 8         /// <param name="angle">angle</param>
 9         /// <param name="radius">radius</param>
10         /// <param name="origin">origin</param>
11         /// <returns>返回值</returns>
12         private Point GetPointByAngle(float angle,float radius,PointF origin)
13         {
14             float y = origin.Y + (float)Math.Sin(Math.PI * (angle / 180.00F)) * radius;
15             float x = origin.X + (float)Math.Cos(Math.PI * (angle / 180.00F)) * radius;
16             return new Point((int)x,(int)y);
17         }
18         #endregion

取边界的思路如下:

1,以控件中心为原点,按照一定的角度顺时针依次进行旋转,

2、每次旋转后,按照此角度从外向内,找到第一个不是透明的点记录下来,这就是外边界点

这个取边界算法感觉并不是太好,如果哪位小伙伴有更好的算法,希望可以探讨一下

重绘

 1   protected override void OnPaint(PaintEventArgs e)
 2         {
 3             base.OnPaint(e);
 4             e.Graphics.SetGDIHigh();
 5 
 6             this.Region = new System.Drawing.Region(m_borderPath);
 7            
 8             if (_bitCache != null)
 9                 e.Graphics.DrawImage(_bitCache,0);
10            
11         }

 

最后的话

如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


项目中经常遇到CSV文件的读写需求,其中的难点主要是CSV文件的解析。本文会介绍CsvHelper、TextFieldParser、正则表达式三种解析CSV文件的方法,顺带也会介绍一下CSV文件的写方法。 CSV文件标准 在介绍CSV文件的读写方法前,我们需要了解一下CSV文件的格式。 文件示例 一
简介 本文的初衷是希望帮助那些有其它平台视觉算法开发经验的人能快速转入Halcon平台下,通过文中的示例开发者能快速了解一个Halcon项目开发的基本步骤,让开发者能把精力完全集中到算法的开发上面。 首先,你需要安装Halcon,HALCON 18.11.0.1的安装包会放在文章末尾。安装包分开发和
这篇文章主要简单记录一下C#项目的dll文件管理方法,以便后期使用。 设置dll路径 参考C#开发奇技淫巧三:把dll放在不同的目录让你的程序更整洁中间的 方法一:配置App.config文件的privatePath : &lt;runtime&gt; &lt;assemblyBinding xml
在C#中的使用JSON序列化及反序列化时,推荐使用Json.NET——NET的流行高性能JSON框架,当然也可以使用.NET自带的 System.Text.Json(.NET5)、DataContractJsonSerializer、JavaScriptSerializer(不推荐)。
事件总线是对发布-订阅模式的一种实现,是一种集中式事件处理机制,允许不同的组件之间进行彼此通信而又不需要相互依赖,达到一种解耦的目的。&#xA;EventBus维护一个事件的字典,发布者、订阅者在事件总线中获取事件实例并执行发布、订阅操作,事件实例负责维护、执行事件处理程序。
通用翻译API的HTTPS 地址为https://fanyi-api.baidu.com/api/trans/vip/translate,使用方法参考通用翻译API接入文档 。&#xA;请求方式可使用 GET 或 POST 方式(Content-Type 请指定为:application/x-www-for
词云”由美国西北大学新闻学副教授、新媒体专业主任里奇·戈登(Rich Gordon)于2006年最先使用,是通过形成“关键词云层”或“关键词渲染”,对文本中出现频率较高的“关键词”的视觉上的突出。词云图过滤掉大量的文本信息,使浏览者只要一眼扫过文本就可以领略文本的主旨。&#xA;网上大部分文章介绍的是使用P
微软在.NET中对串口通讯进行了封装,我们可以在.net2.0及以上版本开发时直接使用SerialPort类对串口进行读写操作。&#xA;为操作方便,本文对SerialPort类做了一些封装,暂时取名为**SerialPortClient**。
简介 管道为进程间通信提供了平台, 管道分为两种类型:匿名管道、命名管道,具体内容参考.NET 中的管道操作。简单来说,匿名管道只能用于本机的父子进程或线程之间,命名管道可用于远程主机或本地的任意两个进程,本文主要介绍命名管道的用法。 匿名管道在本地计算机上提供进程间通信。 与命名管道相比,虽然匿名
目录自定义日志类NLog版本的日志类Serilog版本的日志类 上个月换工作,新项目又要重新搭建基础框架,把日志实现部分单独记录下来方便以后参考。 自定义日志类 代码大部分使用ChatGPT生成,人工进行了测试和优化,主要特点: 线程安全,日志异步写入文件不影响业务逻辑 支持过期文件自动清理,也可自
[TOC] # 原理简介 本文参考[C#/WPF/WinForm/程序实现软件开机自动启动的两种常用方法](https://blog.csdn.net/weixin_42288432/article/details/120059296),将里面中的第一种方法做了封装成**AutoStart**类,使
简介 FTP是FileTransferProtocol(文件传输协议)的英文简称,而中文简称为“文传协议”。用于Internet上的控制文件的双向传输。同时,它也是一个应用程序(Application)。基于不同的操作系统有不同的FTP应用程序,而所有这些应用程序都遵守同一种协议以传输文件。 FTP
使用特性,可以有效地将元数据或声明性信息与代码(程序集、类型、方法、属性等)相关联。 将特性与程序实体相关联后,可以在运行时使用反射这项技术查询特性。&#xA;在 C# 中,通过用方括号 ([]) 将特性名称括起来,并置于应用该特性的实体的声明上方以指定特性。
# 简介 主流的识别库主要有ZXing.NET和ZBar,OpenCV 4.0后加入了QR码检测和解码功能。本文使用的是ZBar,同等条件下ZBar识别率更高,图片和部分代码参考[在C#中使用ZBar识别条形码](https://www.cnblogs.com/w2206/p/7755656.htm
C#中Description特性主要用于枚举和属性,方法比较简单,记录一下以便后期使用。 扩展类DescriptionExtension代码如下: using System; using System.ComponentModel; using System.Reflection; /// &lt;
本文实现一个简单的配置类,原理比较简单,适用于一些小型项目。主要实现以下功能:保存配置到json文件、从文件或实例加载配置类的属性值、数据绑定到界面控件。&#xA;一般情况下,项目都会提供配置的设置界面,很少手动更改配置文件,所以选择以json文件保存配置数据。
前几天用SerialPort类写一个串口的测试程序,关闭串口的时候会让界面卡死。网上大多数方法都是定义2个bool类型的标记Listening和Closing,关闭串口和接受数据前先判断一下。我的方法是DataReceived事件处理程序用this.BeginInvoke()更新界面,不等待UI线程
约束告知编译器类型参数必须具备的功能。 在没有任何约束的情况下,类型参数可以是任何类型。 编译器只能假定 System.Object 的成员,它是任何 .NET 类型的最终基类。 如果客户端代码使用不满足约束的类型,编译器将发出错误。 通过使用 where 上下文关键字指定约束。&#xA;最常用的泛型约束为
protobuf-net是用于.NET代码的基于契约的序列化程序,它以Google设计的“protocol buffers”序列化格式写入数据,适用于大多数编写标准类型并可以使用属性的.NET语言。&#xA;protobuf-net可通过NuGet安装程序包,也可直接访问github下载源码:https:/
工作中经常遇到需要实现TCP客户端或服务端的时候,如果每次都自己写会很麻烦且无聊,使用SuperSocket库又太大了。这时候就可以使用SimpleTCP了,当然仅限于C#语言。&#xA;SimpleTCP是一个简单且非常有用的 .NET 库,用于处理启动和使用 TCP 套接字(客户端和服务器)的重复性任务