(七十七)c#Winform自定义控件-采样控件-HZHControls
官网
前提
入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。
GitHub:https://github.com/kwwwvagaa/NetWinformControl
码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
如果觉得写的还行,请点个 star 支持一下吧
欢迎前来交流探讨: 企鹅群568015492 
来都来了,点个【推荐】再走吧,谢谢
NuGet
Install-Package HZH_Controls
目录
https://www.cnblogs.com/bfyx/p/11364884.html
用处及效果
注意观察各个控件交叠的地方,是不是发现他们没有遮挡?这就是这个控件的妙处了。

准备工作
先说明一下这个控件的作用,很多时候我们需要一个图片类型的控件,但是有需要密集的放在一起,如果单纯的设置背景图或image的话 交叠在一起的部分就会存在遮挡现象,所有就有了这个控件。
该控件可以根据设置的采样图片来裁剪有用的绘图区域,这样的好处就是在交叠的时候,无用区域不会遮挡。
这个用GDI+画的,另外也用到了一点三角函数,不明白的话 可以先百度下
开始
添加一个类UCSampling ,继承UserControl
添加属性
/// <summary>
/// The sampling imag
/// </summary>
private Bitmap samplingImag = null;
/// <summary>
/// Gets or sets the sampling imag.
/// </summary>
/// <value>The sampling imag.</value>
[Browsable(true), Category("自定义属性"), Description("采样图片"), Localizable(true)]
public Bitmap SamplingImag
{
get { return samplingImag; }
set
{
samplingImag = value;
ResetBorderPath();
Invalidate();
}
} /// <summary>
/// The transparent
/// </summary>
private Color? transparent = null; /// <summary>
/// Gets or sets the transparent.
/// </summary>
/// <value>The transparent.</value>
[Browsable(true), Category("自定义属性"), Description("透明色,如果为空,则使用0,0坐标处的颜色"), Localizable(true)]
public Color? Transparent
{
get { return transparent; }
set
{
transparent = value;
ResetBorderPath();
Invalidate();
}
} /// <summary>
/// The alpha
/// </summary>
private int alpha = ; /// <summary>
/// Gets or sets the alpha.
/// </summary>
/// <value>The alpha.</value>
[Browsable(true), Category("自定义属性"), Description("当作透明色的透明度,小于此透明度的颜色将被认定为透明,0-255"), Localizable(true)]
public int Alpha
{
get { return alpha; }
set
{
if (value < || value > )
return;
alpha = value;
ResetBorderPath();
Invalidate();
}
} /// <summary>
/// The color threshold
/// </summary>
private int colorThreshold = ; /// <summary>
/// Gets or sets the color threshold.
/// </summary>
/// <value>The color threshold.</value>
[Browsable(true), Category("自定义属性"), Description("透明色颜色阀值"), Localizable(true)]
public int ColorThreshold
{
get { return colorThreshold; }
set
{
colorThreshold = value;
ResetBorderPath();
Invalidate();
}
} /// <summary>
/// The bit cache
/// </summary>
private Bitmap _bitCache;
在大小改变或图片改变时重新计算边界
/// <summary>
/// The m border path
/// </summary>
GraphicsPath m_borderPath = new GraphicsPath(); /// <summary>
/// Handles the SizeChanged event of the UCSampling control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
void UCSampling_SizeChanged(object sender, EventArgs e)
{
ResetBorderPath();
} /// <summary>
/// Resets the border path.
/// </summary>
private void ResetBorderPath()
{
if (samplingImag == null)
{
m_borderPath = this.ClientRectangle.CreateRoundedRectanglePath();
}
else
{
var bit = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);
using (var bitg = Graphics.FromImage(bit))
{
bitg.DrawImage(samplingImag, this.ClientRectangle, , , samplingImag.Width, samplingImag.Height, GraphicsUnit.Pixel);
}
_bitCache = bit;
m_borderPath = new GraphicsPath();
List<PointF> lstPoints = GetBorderPoints(bit, transparent ?? samplingImag.GetPixel(, ));
m_borderPath.AddLines(lstPoints.ToArray());
m_borderPath.CloseAllFigures();
}
} /// <summary>
/// Gets the border points.
/// </summary>
/// <param name="bit">The bit.</param>
/// <param name="transparent">The transparent.</param>
/// <returns>List<PointF>.</returns>
private List<PointF> GetBorderPoints(Bitmap bit, Color transparent)
{
float diameter = (float)Math.Sqrt(bit.Width * bit.Width + bit.Height * bit.Height);
int intSplit = ;
intSplit = (int)( - (diameter - ) / );
if (intSplit < )
intSplit = ;
List<PointF> lstPoint = new List<PointF>();
for (int i = ; i < ; i += intSplit)
{
for (int j = (int)diameter / ; j > ; j--)
{
Point p = GetPointByAngle(i, j, new PointF(bit.Width / , bit.Height / ));
if (p.X < || p.Y < || p.X >= bit.Width || p.Y >= bit.Height)
continue;
Color _color = bit.GetPixel(p.X, p.Y);
if (!(((int)_color.A) <= alpha || IsLikeColor(_color, transparent)))
{
if (!lstPoint.Contains(p))
{
lstPoint.Add(p);
}
break;
}
}
}
return lstPoint;
} /// <summary>
/// Determines whether [is like color] [the specified color1].
/// </summary>
/// <param name="color1">The color1.</param>
/// <param name="color2">The color2.</param>
/// <returns><c>true</c> if [is like color] [the specified color1]; otherwise, <c>false</c>.</returns>
private bool IsLikeColor(Color color1, Color color2)
{
var cv = Math.Sqrt(Math.Pow((color1.R - color2.R), ) + Math.Pow((color1.G - color2.G), ) + Math.Pow((color1.B - color2.B), ));
if (cv <= colorThreshold)
return true;
else
return false;
}
#region 根据角度得到坐标 English:Get coordinates from angles
/// <summary>
/// 功能描述:根据角度得到坐标 English:Get coordinates from angles
/// 作 者:HZH
/// 创建日期:2019-09-28 11:56:25
/// 任务编号:POS
/// </summary>
/// <param name="angle">angle</param>
/// <param name="radius">radius</param>
/// <param name="origin">origin</param>
/// <returns>返回值</returns>
private Point GetPointByAngle(float angle, float radius, PointF origin)
{
float y = origin.Y + (float)Math.Sin(Math.PI * (angle / 180.00F)) * radius;
float x = origin.X + (float)Math.Cos(Math.PI * (angle / 180.00F)) * radius;
return new Point((int)x, (int)y);
}
#endregion
取边界的思路如下:
1,以控件中心为原点,按照一定的角度顺时针依次进行旋转,
2、每次旋转后,按照此角度从外向内,找到第一个不是透明的点记录下来,这就是外边界点
这个取边界算法感觉并不是太好,如果哪位小伙伴有更好的算法,希望可以探讨一下
重绘
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.SetGDIHigh(); this.Region = new System.Drawing.Region(m_borderPath); if (_bitCache != null)
e.Graphics.DrawImage(_bitCache, , ); }
最后的话
如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧
(七十七)c#Winform自定义控件-采样控件-HZHControls的更多相关文章
- (三十六)c#Winform自定义控件-步骤控件-HZHControls
官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...
- (三十三)c#Winform自定义控件-日期控件
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (十二)c#Winform自定义控件-分页控件
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- DevExpress winform XtraEditor常用控件
最近在公司里面开始使用DevExpress winform的第三方控件进行开发和维护,这里整理一些常用控件的资料以便于后续查看 ComboBoxEdit 这个控件和winform自带的控件差不多,使用 ...
- WinForm容器内控件批量效验是否允许为空?设置是否只读?设置是否可用等方法分享
WinForm容器内控件批量效验是否允许为空?设置是否只读?设置是否可用等方法分享 在WinForm程序中,我们有时需要对某容器内的所有控件做批量操作.如批量判断是否允许为空?批量设置为只读.批量设置 ...
- Winform中checklistbox控件的常用方法
Winform中checklistbox控件的常用方法最近用到checklistbox控件,在使用其过程中,收集了其相关的代码段1.添加项checkedListBox1.Items.Add(" ...
- {VS2010C#}{WinForm}{ActiveX}VS2010C#开发基于WinForm的ActiveX控件
在VS2010中使用C#开发基于WinForm的ActiveX控件 常见的一些ActiveX大部分是使用VB.Delphi.C++开发,使用C#开发ActiveX要解决下面三个问题: 使.NET组件可 ...
- Atitit. .net c# web 跟客户端winform 的ui控件结构比较
Atitit. .net c# web 跟客户端winform 的ui控件结构比较 .net 4.5 webform Winform 命名空间 System.Web.UI.WebControls ...
- WinForm窗体及其控件的自适应
3步骤: 1.在需要自适应的Form中实例化全局变量 AutoSizeFormClass.cs源码在下方 AutoSizeFormClass asc = new AutoSizeFormClass ...
随机推荐
- Linux中docker的使用二
容器下安装jdk和tomcat:通过挂载文件的方法,把宿主机上的文件挂载到容器中,然后解压到opt目录下:tar -zxvf 包名 -C /opt//opt目录下drwxr-xr-x 8 10 143 ...
- svn+apache搭建版本控制服务器
Centos7(linux)搭建版本控制服务器(svn+apache) 1.简介: 版本控制服务器: 版本控制(Revision control)是一种软体工程技巧,籍以在开发的过程中,确保由不同人所 ...
- 华为云ModelArts2.0来袭
[摘要] modelarts自发布以来,不断地更新增加新的功能来为AI工程师们带来新的服务,在这次的全联接大会上EI服务产品部总经理贾永利宣布--华为云AI重装升级,并重磅发布一站式AI开发管理平台M ...
- 关于Python中的错误与异常,你是否了解的够仔细?
每次版本结束都描述这着同样的错误,相似的问题,但始终没见解决.所以今天,我就来总结下Python的错误与异常! 异常与错误 错误 语法错误 可以通过IDE或者解释器给出提示的错误 opentxt('a ...
- 基于webpack实现多html页面开发框架三 图片等文件路径替换、并输出到打包目录
一.解决什么问题 1.图片路径替换.并输出到打包目录 2.输出目录清理 二.需要安装的包 file-loader:html.css中图片路径替换,图片输出到打包目录:命令:npm ...
- java.security.NoSuchProviderException: no such provider: BC 的问题解决
第一种方式 1.修改以下两个文件 %JDK_Home%\jre\lib\security\java.security %JRE_Home%\jre\lib\security\java.security ...
- swift实现单例的四种方式
单例模式 单例模式是设计模式中最简单的一种,甚至有些模式大师都不称其为模式,称其为一种实现技巧,因为设计模式讲究对象之间的关系的抽象,而单例模式只有自己一个对象. 当你只需要一个实例的时候需要使用单例 ...
- libcurl库浅析
先放上libcurl官方文档:链接 第一步:全局初始化 #include <curl/curl.h> CURLcode curl_global_init(long flags ); 在使用 ...
- Java修炼——冒泡排序
核心思想: 1)如有一个数列有 N(5)个元素,则至多需要 N-1(4)趟循环 才能保证数列有序 2) 每一趟循环都从数列的第一个元素开始比较,依次比较 相邻的两个元素,比较到数列的最后 3) 如果前 ...
- 【Nodejs】392- 基于阿里云的 Node.js 稳定性实践
前言 如果你看过 2018 Node.js 的用户报告,你会发现 Node.js 的使用有了进一步的增长,同时也出现了一些新的趋势. Node.js 的开发者更多的开始使用容器并积极的拥抱 Serve ...