using System.ComponentModel;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Windows.Forms;
using XKRS.UI.Helper;
using System.Text.Json;
using System.Security.Cryptography.X509Certificates;
using System.Xml.Serialization;
using System.Drawing;
using Sunny.UI;
using Sunny.UI.Win32;
using OpenCvSharp;
using OpenCvSharp.Extensions;
using Point = System.Drawing.Point;
namespace XKRS.UI
{
public partial class Canvas : UserControl, INotifyPropertyChanged
{
#region 变量通知
public event PropertyChangedEventHandler? PropertyChanged;
private void NotifyPropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
#endregion
[DisplayName("方向键移动间距")]
[Description("按下方向键时,选中的缺陷可以向对应的方向平移,该值设置按下一次方向键缺陷图片的移动距离,单位:像素")]
public float MoveStep { get; set; } = 5f;
[DisplayName("允许选中缺陷")]
[Description("鼠标单击时,是否允许选中图上绘制的缺陷,允许选中时才可以对缺陷进行操作")]
public bool AllowSelectDefect { get; set; } = true;
private Keys _keyLastPressed = Keys.None;
///
/// 顺序查找已选中的缺陷
///
private Defect? _selectedDefect;
public Defect SelectedDefect
{
get
{
_selectedDefect = DefectList.FirstOrDefault(d => d.Selected);
return _selectedDefect;
}
}
///
/// 整体缩放比例
///
private float _wholeScale;
public float WholeScale
{
get
{
return _wholeScale;
}
set
{
_wholeScale = value;
WholeScaleChange();
}
}
///
/// 横向缩放比列
///
private float _xScale;
public float XScale
{
get
{
return _xScale;
}
set
{
_xScale = value;
XScaleChange();
}
}
///
/// 竖向缩放比例
///
private float _yScale;
public float YScale
{
get
{
return _yScale;
}
set
{
_yScale = value;
YScaleChange();
}
}
///
/// 缩放比例,该参数必须大于0
///
private double _scale = 1.0;
public double Scale
{
get { return _scale; }
set
{
if (value <= 0.000001)
{
return;
}
_scale = value;
NotifyPropertyChanged("DScale");
}
}
///
/// 旋转角度
///
private double _route = 0.0;
public double Route
{
get { return _route; }
set
{
_route = value;
DRouteChange();
NotifyPropertyChanged("DRoute");
}
}
///
/// 横向移动距离
///
private float _xMove;
public float XMove
{
get { return _xMove; }
set
{
_xMove = value;
XMoveChange();
}
}
///
/// 竖向移动距离
///
private float _yMove;
public float YMove
{
get { return _yMove; }
set
{
_yMove = value;
YMoveChange();
}
}
///
/// 边框颜色
///
private Brush _brushColor;
public Brush BrushColor
{
get
{
return _brushColor;
}
set
{
_brushColor = value;
ColorChange();
}
}
///
/// 变换矩阵
///
private Matrix _matrix = new Matrix();
public Matrix Matrix => _matrix.Clone();
///
/// 矩形框
///
private Rectangle _rcImg = new Rectangle(0, 0, 0, 0);
public int _width { get; private set; }
public int _height { get; private set; }
///
/// 位图
///
private Bitmap? _bitmap = null;
public Bitmap CanvasBitmap
{
get
{
return _bitmap;
}
}
///
/// 预览缺陷图片的GraphicsPath
///
private GraphicsPath? _path = null;
public Bitmap ImgData
{
get { return _bitmap; }
set
{
Init();
_bitmap = value;
if (null == _bitmap)
{
_rcImg.Width = 0;
_rcImg.Height = 0;
}
else
{
_rcImg.Width = _bitmap.Width;
_rcImg.Height = _bitmap.Height;
FitImage();
}
//强制控件使其工作区无效并且重新绘制自己以及任何子控件
Refresh();
}
}
private string strImgPath = string.Empty;
public string ImagePath
{
get { return strImgPath; }
set
{
try
{
if (null != _bitmap)
{
_bitmap.Dispose();
}
using (Stream s = File.Open(value, FileMode.Open))
{
ImgData = (Bitmap)Image.FromStream(s);
}
}
catch (Exception)
{
_bitmap = null;
strImgPath = string.Empty;
Refresh();
}
}
}
private Mat strImgMat = new Mat();
public Mat ImaMAt
{
get { return strImgMat; }
set
{
try
{
if (null != _bitmap)
{
_bitmap.Dispose();
}
ImgData = value.ToBitmap();
}
catch (Exception)
{
_bitmap = null;
strImgPath = string.Empty;
Refresh();
}
}
}
private PointF _panBasePoint = PointF.Empty;
private ClickArea _clickArea = ClickArea.AREA_UNKNOW;
public List DefectList = new List();
public List GPathList = new List();
Action