UCSampling.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 2019-09-28
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCSampling.cs">
  7. // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
  8. // </copyright>
  9. //
  10. // Blog: https://www.cnblogs.com/bfyx
  11. // GitHub:https://github.com/kwwwvagaa/NetWinformControl
  12. // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
  13. //
  14. // If you use this code, please keep this note.
  15. // ***********************************************************************
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Linq;
  19. using System.Text;
  20. using System.Windows.Forms;
  21. using System.Drawing;
  22. using System.Drawing.Drawing2D;
  23. using System.ComponentModel;
  24. namespace HZH_Controls.Controls
  25. {
  26. /// <summary>
  27. /// Class UCSampling.
  28. /// Implements the <see cref="System.Windows.Forms.UserControl" />
  29. /// </summary>
  30. /// <seealso cref="System.Windows.Forms.UserControl" />
  31. public class UCSampling : UserControl
  32. {
  33. /// <summary>
  34. /// The sampling imag
  35. /// </summary>
  36. private Bitmap samplingImag = null;
  37. /// <summary>
  38. /// Gets or sets the sampling imag.
  39. /// </summary>
  40. /// <value>The sampling imag.</value>
  41. [Browsable(true), Category("自定义属性"), Description("采样图片"), Localizable(true)]
  42. public Bitmap SamplingImag
  43. {
  44. get { return samplingImag; }
  45. set
  46. {
  47. samplingImag = value;
  48. ResetBorderPath();
  49. Invalidate();
  50. }
  51. }
  52. /// <summary>
  53. /// The transparent
  54. /// </summary>
  55. private Color? transparent = null;
  56. /// <summary>
  57. /// Gets or sets the transparent.
  58. /// </summary>
  59. /// <value>The transparent.</value>
  60. [Browsable(true), Category("自定义属性"), Description("透明色,如果为空,则使用0,0坐标处的颜色"), Localizable(true)]
  61. public Color? Transparent
  62. {
  63. get { return transparent; }
  64. set
  65. {
  66. transparent = value;
  67. ResetBorderPath();
  68. Invalidate();
  69. }
  70. }
  71. /// <summary>
  72. /// The alpha
  73. /// </summary>
  74. private int alpha = 50;
  75. /// <summary>
  76. /// Gets or sets the alpha.
  77. /// </summary>
  78. /// <value>The alpha.</value>
  79. [Browsable(true), Category("自定义属性"), Description("当作透明色的透明度,小于此透明度的颜色将被认定为透明,0-255"), Localizable(true)]
  80. public int Alpha
  81. {
  82. get { return alpha; }
  83. set
  84. {
  85. if (value < 0 || value > 255)
  86. return;
  87. alpha = value;
  88. ResetBorderPath();
  89. Invalidate();
  90. }
  91. }
  92. /// <summary>
  93. /// The color threshold
  94. /// </summary>
  95. private int colorThreshold = 10;
  96. /// <summary>
  97. /// Gets or sets the color threshold.
  98. /// </summary>
  99. /// <value>The color threshold.</value>
  100. [Browsable(true), Category("自定义属性"), Description("透明色颜色阀值"), Localizable(true)]
  101. public int ColorThreshold
  102. {
  103. get { return colorThreshold; }
  104. set
  105. {
  106. colorThreshold = value;
  107. ResetBorderPath();
  108. Invalidate();
  109. }
  110. }
  111. /// <summary>
  112. /// The bit cache
  113. /// </summary>
  114. private Bitmap _bitCache;
  115. /// <summary>
  116. /// Initializes a new instance of the <see cref="UCSampling"/> class.
  117. /// </summary>
  118. public UCSampling()
  119. {
  120. this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  121. this.SetStyle(ControlStyles.DoubleBuffer, true);
  122. this.SetStyle(ControlStyles.ResizeRedraw, true);
  123. this.SetStyle(ControlStyles.Selectable, true);
  124. this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  125. this.SetStyle(ControlStyles.UserPaint, true);
  126. this.SizeChanged += UCSampling_SizeChanged;
  127. this.Size = new Size(35, 35);
  128. }
  129. /// <summary>
  130. /// The m border path
  131. /// </summary>
  132. GraphicsPath m_borderPath = new GraphicsPath();
  133. /// <summary>
  134. /// Handles the SizeChanged event of the UCSampling control.
  135. /// </summary>
  136. /// <param name="sender">The source of the event.</param>
  137. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  138. void UCSampling_SizeChanged(object sender, EventArgs e)
  139. {
  140. ResetBorderPath();
  141. }
  142. /// <summary>
  143. /// Resets the border path.
  144. /// </summary>
  145. private void ResetBorderPath()
  146. {
  147. if (samplingImag == null)
  148. {
  149. m_borderPath = this.ClientRectangle.CreateRoundedRectanglePath(5);
  150. }
  151. else
  152. {
  153. var bit = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);
  154. using (var bitg = Graphics.FromImage(bit))
  155. {
  156. bitg.DrawImage(samplingImag, this.ClientRectangle, 0, 0, samplingImag.Width, samplingImag.Height, GraphicsUnit.Pixel);
  157. }
  158. _bitCache = bit;
  159. m_borderPath = new GraphicsPath();
  160. List<PointF> lstPoints = GetBorderPoints(bit, transparent ?? samplingImag.GetPixel(0, 0));
  161. m_borderPath.AddLines(lstPoints.ToArray());
  162. m_borderPath.CloseAllFigures();
  163. }
  164. }
  165. /// <summary>
  166. /// Gets the border points.
  167. /// </summary>
  168. /// <param name="bit">The bit.</param>
  169. /// <param name="transparent">The transparent.</param>
  170. /// <returns>List&lt;PointF&gt;.</returns>
  171. private List<PointF> GetBorderPoints(Bitmap bit, Color transparent)
  172. {
  173. float diameter = (float)Math.Sqrt(bit.Width * bit.Width + bit.Height * bit.Height);
  174. int intSplit = 0;
  175. intSplit = (int)(7 - (diameter - 200) / 100);
  176. if (intSplit < 1)
  177. intSplit = 1;
  178. List<PointF> lstPoint = new List<PointF>();
  179. for (int i = 0; i < 360; i += intSplit)
  180. {
  181. for (int j = (int)diameter / 2; j > 5; j--)
  182. {
  183. Point p = GetPointByAngle(i, j, new PointF(bit.Width / 2, bit.Height / 2));
  184. if (p.X < 0 || p.Y < 0 || p.X >= bit.Width || p.Y >= bit.Height)
  185. continue;
  186. Color _color = bit.GetPixel(p.X, p.Y);
  187. if (!(((int)_color.A) <= alpha || IsLikeColor(_color, transparent)))
  188. {
  189. if (!lstPoint.Contains(p))
  190. {
  191. lstPoint.Add(p);
  192. }
  193. break;
  194. }
  195. }
  196. }
  197. return lstPoint;
  198. }
  199. /// <summary>
  200. /// Determines whether [is like color] [the specified color1].
  201. /// </summary>
  202. /// <param name="color1">The color1.</param>
  203. /// <param name="color2">The color2.</param>
  204. /// <returns><c>true</c> if [is like color] [the specified color1]; otherwise, <c>false</c>.</returns>
  205. private bool IsLikeColor(Color color1, Color color2)
  206. {
  207. 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));
  208. if (cv <= colorThreshold)
  209. return true;
  210. else
  211. return false;
  212. }
  213. #region 根据角度得到坐标 English:Get coordinates from angles
  214. /// <summary>
  215. /// 功能描述:根据角度得到坐标 English:Get coordinates from angles
  216. /// 作  者:HZH
  217. /// 创建日期:2019-09-28 11:56:25
  218. /// 任务编号:POS
  219. /// </summary>
  220. /// <param name="angle">angle</param>
  221. /// <param name="radius">radius</param>
  222. /// <param name="origin">origin</param>
  223. /// <returns>返回值</returns>
  224. private Point GetPointByAngle(float angle, float radius, PointF origin)
  225. {
  226. float y = origin.Y + (float)Math.Sin(Math.PI * (angle / 180.00F)) * radius;
  227. float x = origin.X + (float)Math.Cos(Math.PI * (angle / 180.00F)) * radius;
  228. return new Point((int)x, (int)y);
  229. }
  230. #endregion
  231. /// <summary>
  232. /// 引发 <see cref="E:System.Windows.Forms.Control.Paint" /> 事件。
  233. /// </summary>
  234. /// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.PaintEventArgs" />。</param>
  235. protected override void OnPaint(PaintEventArgs e)
  236. {
  237. base.OnPaint(e);
  238. e.Graphics.SetGDIHigh();
  239. this.Region = new System.Drawing.Region(m_borderPath);
  240. if (_bitCache != null)
  241. e.Graphics.DrawImage(_bitCache, 0, 0);
  242. }
  243. }
  244. }