Files
JoyD/Windows/CS/Framework4.0/Camera/Camera/Setting.cs
2026-03-26 10:30:55 +08:00

1251 lines
48 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Camera
{
public partial class Setting : Form
{
private Camera _camera;
private int _selectedZoneIndex = -1;
private bool _isDrawing = false;
private bool _isMoving = false;
private bool _isResizing = false;
private Point _startPoint;
private Point _currentMousePoint;
private Rectangle _originalZone;
private bool _isEditingDetectionZone = false;
private bool _isEditingLedZone = false;
private bool _isDrawingLedMode = false;
private int _ledZoneState = 0;
private Color _detectionZoneColor = Color.Black;
private ConcurrentDictionary<int, Color> _ledZoneColors = new ConcurrentDictionary<int, Color>();
private Point _resizeStartPoint;
private Rectangle _originalResizeRect;
private int _hoveredHandle = -1;
private int _currentLedIndex = -1;
private int[] _handleOffsetsX = { 0, 1, 2, 2, 2, 1, 0, 0 };
private int[] _handleOffsetsY = { 0, 0, 0, 1, 2, 2, 2, 1 };
public Setting()
{
InitializeComponent();
InitializeDataGridView();
}
public void SetCamera(Camera camera)
{
_camera = camera;
}
public void SetImage(Image image)
{
try
{
if (picBoxCamera.Image != null)
{
picBoxCamera.Image.Dispose();
}
// 不设置picBoxCamera.Image避免双重绘制
// 只在Paint事件中绘制缩放后的图像
picBoxCamera.Invalidate();
}
catch { }
}
public void SetConfigPath(string path)
{
if (_camera != null)
{
_camera.SetConfigPath(path);
}
}
private void InitializeDataGridView()
{
dataGridView1.Columns.Clear();
dataGridView1.Columns.Add("Index", "索引");
dataGridView1.Columns.Add("X", "X坐标");
dataGridView1.Columns.Add("Y", "Y坐标");
dataGridView1.Columns.Add("Width", "宽度");
dataGridView1.Columns.Add("Height", "高度");
dataGridView1.Columns.Add("Color", "颜色");
dataGridView1.Columns["Index"].Width = 50;
dataGridView1.Columns["X"].Width = 60;
dataGridView1.Columns["Y"].Width = 60;
dataGridView1.Columns["Width"].Width = 60;
dataGridView1.Columns["Height"].Width = 60;
dataGridView1.Columns["Color"].Width = 70;
}
private void Setting_Load(object sender, EventArgs e)
{
CreateToolIcons();
if (_camera != null)
{
_camera.ImageCaptured += Camera_ImageCaptured;
_detectionZoneColor = _camera.GetDetectionZoneColor();
_ledZoneColors = _camera.GetLedZoneColors();
}
picBoxCamera.BackColor = Color.Gray;
splitContainer1.FixedPanel = System.Windows.Forms.FixedPanel.Panel2;
splitContainer1.Panel2MinSize = 380;
splitContainer1.SplitterDistance = splitContainer1.Width - 380;
splitContainer2.Panel1MinSize = 40;
UpdateDataGridView();
}
private void CreateToolIcons()
{
try
{
Bitmap detectionIcon = new Bitmap(32, 32);
using (Graphics g = Graphics.FromImage(detectionIcon))
{
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.Clear(Color.Transparent);
using (Pen dashedPen = new Pen(Color.Red, 1.5f))
{
dashedPen.DashPattern = new float[] { 2, 1 };
g.DrawRectangle(dashedPen, 6, 6, 20, 20);
}
SolidBrush brush = new SolidBrush(Color.Red);
g.FillEllipse(brush, 6, 6, 4, 4);
g.FillEllipse(brush, 22, 6, 4, 4);
g.FillEllipse(brush, 6, 22, 4, 4);
g.FillEllipse(brush, 22, 22, 4, 4);
g.FillEllipse(brush, 14, 6, 4, 4);
g.FillEllipse(brush, 14, 22, 4, 4);
g.FillEllipse(brush, 6, 14, 4, 4);
g.FillEllipse(brush, 22, 14, 4, 4);
using (Pen gearPen = new Pen(Color.Red, 1.5f))
{
g.DrawEllipse(gearPen, 12, 12, 8, 8);
for (int i = 0; i < 6; i++)
{
double angle = i * Math.PI / 3;
int x1 = 16 + (int)(12 * Math.Cos(angle));
int y1 = 16 + (int)(12 * Math.Sin(angle));
int x2 = 16 + (int)(8 * Math.Cos(angle));
int y2 = 16 + (int)(8 * Math.Sin(angle));
g.DrawLine(gearPen, x1, y1, x2, y2);
}
}
}
toolStripButton1.Image = detectionIcon;
toolStripButton1.ImageTransparentColor = Color.Transparent;
Bitmap ledIcon = new Bitmap(32, 32);
using (Graphics g = Graphics.FromImage(ledIcon))
{
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.Clear(Color.Transparent);
using (Pen dashedPen = new Pen(Color.Lime, 1.5f))
{
dashedPen.DashPattern = new float[] { 2, 1 };
g.DrawRectangle(dashedPen, 6, 6, 20, 20);
}
SolidBrush brush = new SolidBrush(Color.Lime);
g.FillEllipse(brush, 6, 6, 4, 4);
g.FillEllipse(brush, 22, 6, 4, 4);
g.FillEllipse(brush, 6, 22, 4, 4);
g.FillEllipse(brush, 22, 22, 4, 4);
g.FillEllipse(brush, 14, 6, 4, 4);
g.FillEllipse(brush, 14, 22, 4, 4);
g.FillEllipse(brush, 6, 14, 4, 4);
g.FillEllipse(brush, 22, 14, 4, 4);
using (Pen pen = new Pen(Color.Lime, 2))
{
g.DrawLine(pen, 12, 10, 12, 14);
g.DrawLine(pen, 16, 10, 16, 14);
g.DrawLine(pen, 20, 10, 20, 14);
g.DrawLine(pen, 10, 16, 22, 16);
g.DrawLine(pen, 10, 20, 22, 20);
}
}
toolStripButton2.Image = ledIcon;
toolStripButton2.ImageTransparentColor = Color.Transparent;
Bitmap deleteIcon = new Bitmap(32, 32);
using (Graphics g = Graphics.FromImage(deleteIcon))
{
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.Clear(Color.Transparent);
using (Pen pen = new Pen(Color.Red, 3))
{
g.DrawLine(pen, 8, 8, 24, 24);
g.DrawLine(pen, 24, 8, 8, 24);
}
}
toolStripButton4.Image = deleteIcon;
toolStripButton4.ImageTransparentColor = Color.Transparent;
}
catch { }
}
private void Camera_ImageCaptured(object sender, ImageEventArgs e)
{
if (picBoxCamera.InvokeRequired)
{
picBoxCamera.Invoke(new Action<Image>(UpdateImage), e.Image);
}
else
{
UpdateImage(e.Image);
}
}
private void UpdateImage(Image image)
{
try
{
if (picBoxCamera.Image != null)
{
picBoxCamera.Image.Dispose();
}
// 不设置picBoxCamera.Image避免双重绘制
// 只在Paint事件中绘制缩放后的图像
picBoxCamera.Invalidate();
}
catch { }
}
private void UpdateDataGridView()
{
dataGridView1.Rows.Clear();
if (_camera != null)
{
var allLedZones = _camera.GetLedZones();
foreach (var kvp in allLedZones)
{
int index = kvp.Key;
Rectangle ledZone = kvp.Value;
Color ledColor = _camera.GetLedZoneColor(index);
if (ledZone.Width > 0 && ledZone.Height > 0)
{
dataGridView1.Rows.Add(index, ledZone.X, ledZone.Y, ledZone.Width, ledZone.Height, ColorTranslator.ToHtml(ledColor));
}
}
}
picBoxCamera.Invalidate();
}
private void PicBoxCamera_Paint(object sender, PaintEventArgs e)
{
Image currentImage = null;
try
{
// 从Camera获取当前图像
currentImage = _camera != null ? _camera.GetCurrentImage() : null;
if (currentImage == null) return;
}
catch
{
return;
}
int imageWidth = 0, imageHeight = 0;
bool imageValid = false;
try
{
imageWidth = currentImage.Width;
imageHeight = currentImage.Height;
if (imageWidth > 0 && imageHeight > 0)
{
imageValid = true;
}
}
catch
{
return;
}
if (!imageValid) return;
Graphics g = e.Graphics;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
// 绘制缩放后的图像
Rectangle destRect = new Rectangle(0, 0, picBoxCamera.ClientSize.Width, picBoxCamera.ClientSize.Height);
g.DrawImage(currentImage, destRect, 0, 0, currentImage.Width, currentImage.Height, GraphicsUnit.Pixel);
float scaleX = (float)picBoxCamera.ClientSize.Width / imageWidth;
float scaleY = (float)picBoxCamera.ClientSize.Height / imageHeight;
Rectangle detectionZone = _camera != null ? _camera.GetDetectionZone() : new Rectangle(0, 0, 0, 0);
Rectangle scaledDetectionZone = new Rectangle(
(int)(detectionZone.X * scaleX),
(int)(detectionZone.Y * scaleY),
(int)(detectionZone.Width * scaleX),
(int)(detectionZone.Height * scaleY)
);
Rectangle currentEditZone = new Rectangle(0, 0, 0, 0);
bool isEditing = false;
Color editColor = _detectionZoneColor;
if (_isEditingDetectionZone)
{
currentEditZone = scaledDetectionZone;
isEditing = true;
editColor = _detectionZoneColor;
}
else if ((_isEditingLedZone || _isDrawingLedMode) && _currentLedIndex >= 0)
{
Rectangle ledZone = _camera.GetLedZone(_currentLedIndex);
currentEditZone = new Rectangle(
(int)(ledZone.X * scaleX),
(int)(ledZone.Y * scaleY),
(int)(ledZone.Width * scaleX),
(int)(ledZone.Height * scaleY)
);
isEditing = true;
editColor = _camera.GetLedZoneColor(_currentLedIndex);
}
if (_isDrawing && (_isEditingLedZone || _isDrawingLedMode) && _startPoint != null && _currentMousePoint != null)
{
using (Pen pen = new Pen(_detectionZoneColor, 2))
{
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
g.DrawRectangle(pen, scaledDetectionZone);
}
var allLedZones = _camera.GetLedZones();
foreach (var kvp in allLedZones)
{
int index = kvp.Key;
Rectangle ledZone = kvp.Value;
Color ledColor = _camera.GetLedZoneColor(index);
Rectangle scaledLed = new Rectangle(
(int)(ledZone.X * scaleX),
(int)(ledZone.Y * scaleY),
(int)(ledZone.Width * scaleX),
(int)(ledZone.Height * scaleY)
);
using (Pen ledPen = new Pen(ledColor, 2))
{
g.DrawRectangle(ledPen, scaledLed);
}
using (Font font = new Font("Arial", 10))
{
g.DrawString(index.ToString(), font, new SolidBrush(ledColor), scaledLed.X, scaledLed.Y - 15);
}
}
int x = (int)(Math.Min(_startPoint.X, _startPoint.X + (_currentMousePoint.X - _resizeStartPoint.X) / scaleX) * scaleX);
int y = (int)(Math.Min(_startPoint.Y, _startPoint.Y + (_currentMousePoint.Y - _resizeStartPoint.Y) / scaleY) * scaleY);
int w = (int)(Math.Abs(_currentMousePoint.X - _resizeStartPoint.X));
int h = (int)(Math.Abs(_currentMousePoint.Y - _resizeStartPoint.Y));
if (w > 5 && h > 5)
{
Rectangle drawRect = new Rectangle(x, y, w, h);
Color currentLedColor = _currentLedIndex >= 0 && _camera != null ? _camera.GetLedZoneColor(_currentLedIndex) : Color.Lime;
using (Pen pen = new Pen(currentLedColor, 2))
{
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
g.DrawRectangle(pen, drawRect);
}
}
}
else if ((_isEditingLedZone || _isDrawingLedMode) && _currentLedIndex >= 0)
{
using (Pen pen = new Pen(_detectionZoneColor, 2))
{
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
g.DrawRectangle(pen, scaledDetectionZone);
}
var allLedZones = _camera.GetLedZones();
foreach (var kvp in allLedZones)
{
int index = kvp.Key;
Rectangle ledZone = kvp.Value;
Color ledColor = _camera.GetLedZoneColor(index);
Rectangle scaledLed = new Rectangle(
(int)(ledZone.X * scaleX),
(int)(ledZone.Y * scaleY),
(int)(ledZone.Width * scaleX),
(int)(ledZone.Height * scaleY)
);
using (Pen pen = new Pen(ledColor, 2))
{
g.DrawRectangle(pen, scaledLed);
}
using (Font font = new Font("Arial", 10))
{
g.DrawString(index.ToString(), font, new SolidBrush(ledColor), scaledLed.X, scaledLed.Y - 15);
}
}
if (currentEditZone.Width > 0 && currentEditZone.Height > 0)
{
using (Pen pen = new Pen(editColor, 2))
{
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
g.DrawRectangle(pen, currentEditZone);
}
DrawEditHandles(g, currentEditZone, editColor);
}
}
else if (isEditing && currentEditZone.Width > 0 && currentEditZone.Height > 0)
{
using (Pen pen = new Pen(editColor, 2))
{
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
g.DrawRectangle(pen, currentEditZone);
}
DrawEditHandles(g, currentEditZone, editColor);
}
else
{
using (Pen pen = new Pen(_detectionZoneColor, 2))
{
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
g.DrawRectangle(pen, scaledDetectionZone);
}
if (_camera != null)
{
var allLedZones = _camera.GetLedZones();
foreach (var kvp in allLedZones)
{
int index = kvp.Key;
Rectangle ledZone = kvp.Value;
Color ledColor = _camera.GetLedZoneColor(index);
Rectangle scaledLed = new Rectangle(
(int)(ledZone.X * scaleX),
(int)(ledZone.Y * scaleY),
(int)(ledZone.Width * scaleX),
(int)(ledZone.Height * scaleY)
);
using (Pen pen = new Pen(ledColor, 2))
{
g.DrawRectangle(pen, scaledLed);
}
using (Font font = new Font("Arial", 10))
{
g.DrawString(index.ToString(), font, new SolidBrush(ledColor), scaledLed.X, scaledLed.Y - 15);
}
}
}
}
}
private void DrawEditHandles(Graphics g, Rectangle rect, Color handleColor)
{
int handleSize = 8;
using (SolidBrush handleBrush = new SolidBrush(Color.White))
using (Pen handleBorderPen = new Pen(Color.Black, 1))
{
int[] xPoints = { rect.X, rect.X + rect.Width / 2, rect.X + rect.Width, rect.X + rect.Width, rect.X + rect.Width, rect.X + rect.Width / 2, rect.X, rect.X };
int[] yPoints = { rect.Y, rect.Y, rect.Y, rect.Y + rect.Height / 2, rect.Y + rect.Height, rect.Y + rect.Height, rect.Y + rect.Height, rect.Y + rect.Height / 2 };
for (int i = 0; i < 8; i++)
{
Rectangle handleRect = new Rectangle(
xPoints[i] - handleSize / 2,
yPoints[i] - handleSize / 2,
handleSize,
handleSize
);
g.FillRectangle(handleBrush, handleRect);
g.DrawRectangle(handleBorderPen, handleRect);
}
}
}
private void DrawSelectionHandles(Graphics g, Rectangle rect, Color handleColor)
{
int handleSize = 8;
using (Brush brush = new SolidBrush(Color.White))
{
g.FillRectangle(brush, rect.X - handleSize / 2, rect.Y - handleSize / 2, handleSize, handleSize);
g.FillRectangle(brush, rect.X + rect.Width - handleSize / 2, rect.Y - handleSize / 2, handleSize, handleSize);
g.FillRectangle(brush, rect.X - handleSize / 2, rect.Y + rect.Height - handleSize / 2, handleSize, handleSize);
g.FillRectangle(brush, rect.X + rect.Width - handleSize / 2, rect.Y + rect.Height - handleSize / 2, handleSize, handleSize);
}
using (Pen pen = new Pen(handleColor, 1))
{
g.DrawRectangle(pen, rect.X - handleSize / 2, rect.Y - handleSize / 2, handleSize, handleSize);
g.DrawRectangle(pen, rect.X + rect.Width - handleSize / 2, rect.Y - handleSize / 2, handleSize, handleSize);
g.DrawRectangle(pen, rect.X - handleSize / 2, rect.Y + rect.Height - handleSize / 2, handleSize, handleSize);
g.DrawRectangle(pen, rect.X + rect.Width - handleSize / 2, rect.Y + rect.Height - handleSize / 2, handleSize, handleSize);
}
}
private void PicBoxCamera_MouseDown(object sender, MouseEventArgs e)
{
// 处理右键点击
if (e.Button == MouseButtons.Right)
{
return; // 右键点击由MouseUp事件处理
}
Image currentImage = _camera != null ? _camera.GetCurrentImage() : null;
if (currentImage == null || _camera == null) return;
int imageWidth, imageHeight;
try
{
imageWidth = currentImage.Width;
imageHeight = currentImage.Height;
}
catch
{
return;
}
float scaleX = (float)picBoxCamera.ClientSize.Width / imageWidth;
float scaleY = (float)picBoxCamera.ClientSize.Height / imageHeight;
Point imagePoint = new Point((int)(e.Location.X / scaleX), (int)(e.Location.Y / scaleY));
Point controlPoint = e.Location;
Rectangle detectionZone = _camera.GetDetectionZone();
Rectangle ledZone = _camera.GetLedZone();
Rectangle scaledDetectionZone = new Rectangle(
(int)(detectionZone.X * scaleX),
(int)(detectionZone.Y * scaleY),
(int)(detectionZone.Width * scaleX),
(int)(detectionZone.Height * scaleY)
);
var allLedZones = _camera.GetLedZones();
bool clickedOnLedZone = false;
int clickedLedIndex = -1;
foreach (var kvp in allLedZones)
{
Rectangle led = kvp.Value;
Rectangle scaledLed = new Rectangle(
(int)(led.X * scaleX),
(int)(led.Y * scaleY),
(int)(led.Width * scaleX),
(int)(led.Height * scaleY)
);
if (scaledLed.Width > 0 && scaledLed.Height > 0 && scaledLed.Contains(controlPoint))
{
if (!_isEditingDetectionZone && !_isEditingLedZone && !_isDrawingLedMode)
{
clickedOnLedZone = true;
clickedLedIndex = kvp.Key;
ledZone = led;
break;
}
else if (_isEditingLedZone || _isDrawingLedMode)
{
clickedOnLedZone = true;
clickedLedIndex = kvp.Key;
ledZone = led;
break;
}
}
}
if (clickedOnLedZone)
{
_isEditingLedZone = true;
_isDrawingLedMode = false;
_currentLedIndex = clickedLedIndex;
_selectedZoneIndex = clickedLedIndex;
toolStripButton2.Checked = false;
toolStripButton2.Visible = false;
toolStripButton1.Visible = false;
UpdateLedZoneButtonsVisibility(1);
UpdateColorButtonIcon();
_isMoving = true;
_startPoint = imagePoint;
_resizeStartPoint = controlPoint;
_originalZone = ledZone;
_isDrawing = false;
picBoxCamera.Invalidate();
return;
}
Rectangle scaledLedZone = new Rectangle(0, 0, 0, 0);
if ((_isEditingLedZone || _isDrawingLedMode) && _currentLedIndex >= 0)
{
Rectangle led = _camera.GetLedZone(_currentLedIndex);
scaledLedZone = new Rectangle(
(int)(led.X * scaleX),
(int)(led.Y * scaleY),
(int)(led.Width * scaleX),
(int)(led.Height * scaleY)
);
}
if (_isEditingDetectionZone || _isEditingLedZone || _isDrawingLedMode)
{
Rectangle editRect = _isEditingDetectionZone ? scaledDetectionZone : scaledLedZone;
_hoveredHandle = GetHoveredHandle(editRect, controlPoint);
if (_hoveredHandle >= 0)
{
_isResizing = true;
_resizeStartPoint = controlPoint;
_originalResizeRect = editRect;
}
else if (editRect.Width > 0 && editRect.Height > 0 && editRect.Contains(controlPoint))
{
_isMoving = true;
_startPoint = imagePoint;
_resizeStartPoint = controlPoint;
_originalZone = _isEditingDetectionZone ? detectionZone : ledZone;
}
else if ((_isDrawingLedMode || _isEditingLedZone) && !clickedOnLedZone)
{
_currentLedIndex = -1;
_selectedZoneIndex = -1;
if (_isEditingLedZone)
{
_isEditingLedZone = false;
}
_isDrawing = true;
_isDrawingLedMode = true; // 确保处于LED绘制模式
_startPoint = imagePoint;
_resizeStartPoint = controlPoint;
toolStripButton2.Checked = true; // 保持LED编辑按钮按下状态
UpdateLedZoneButtonsVisibility(2);
}
}
else
{
if (_isEditingDetectionZone && detectionZone.Contains(imagePoint))
{
_selectedZoneIndex = 0;
_isMoving = true;
_startPoint = imagePoint;
_resizeStartPoint = controlPoint;
_originalZone = detectionZone;
}
else if (!_isEditingDetectionZone && !_isEditingLedZone && !_isDrawingLedMode)
{
_selectedZoneIndex = -1;
_isDrawing = false;
picBoxCamera.Invalidate();
}
}
UpdateDataGridView();
picBoxCamera.Invalidate();
}
private void PicBoxCamera_MouseMove(object sender, MouseEventArgs e)
{
_currentMousePoint = e.Location;
Image currentImage = _camera != null ? _camera.GetCurrentImage() : null;
if (currentImage == null || _camera == null) return;
int imageWidth, imageHeight;
try
{
imageWidth = currentImage.Width;
imageHeight = currentImage.Height;
}
catch
{
return;
}
float scaleX = (float)picBoxCamera.ClientSize.Width / imageWidth;
float scaleY = (float)picBoxCamera.ClientSize.Height / imageHeight;
Rectangle detectionZone = _camera.GetDetectionZone();
Rectangle ledZone = _camera.GetLedZone();
Rectangle scaledDetectionZone = new Rectangle(
(int)(detectionZone.X * scaleX),
(int)(detectionZone.Y * scaleY),
(int)(detectionZone.Width * scaleX),
(int)(detectionZone.Height * scaleY)
);
Rectangle scaledLedZone = new Rectangle(0, 0, 0, 0);
if (_currentLedIndex >= 0)
{
Rectangle led = _camera.GetLedZone(_currentLedIndex);
scaledLedZone = new Rectangle(
(int)(led.X * scaleX),
(int)(led.Y * scaleY),
(int)(led.Width * scaleX),
(int)(led.Height * scaleY)
);
}
if (_isEditingDetectionZone || _isEditingLedZone || _isDrawingLedMode)
{
Rectangle editRect = _isEditingDetectionZone ? scaledDetectionZone : scaledLedZone;
int handle = GetHoveredHandle(editRect, e.Location);
if (_isResizing)
{
picBoxCamera.Cursor = GetResizeCursor(_hoveredHandle);
int dx = e.Location.X - _resizeStartPoint.X;
int dy = e.Location.Y - _resizeStartPoint.Y;
Rectangle newRect = AdjustRectangle(_originalResizeRect, _hoveredHandle, dx, dy);
Rectangle newZone = new Rectangle(
(int)(newRect.X / scaleX),
(int)(newRect.Y / scaleY),
(int)(newRect.Width / scaleX),
(int)(newRect.Height / scaleY)
);
if (newZone.Width > 10 && newZone.Height > 10 &&
newZone.X >= 0 && newZone.Y >= 0 &&
newZone.X + newZone.Width <= currentImage.Width &&
newZone.Y + newZone.Height <= currentImage.Height)
{
if (_isEditingDetectionZone)
{
_camera.SetDetectionZone(newZone);
}
else if (_isEditingLedZone)
{
_camera.SetLedZone(_currentLedIndex, newZone);
}
picBoxCamera.Update();
}
}
else if (_isMoving)
{
picBoxCamera.Cursor = Cursors.SizeAll;
int dx = e.Location.X - _resizeStartPoint.X;
int dy = e.Location.Y - _resizeStartPoint.Y;
Rectangle newZone = new Rectangle(
_originalZone.X + (int)(dx / scaleX),
_originalZone.Y + (int)(dy / scaleY),
_originalZone.Width,
_originalZone.Height
);
if (newZone.X >= 0 && newZone.Y >= 0 &&
newZone.X + newZone.Width <= currentImage.Width &&
newZone.Y + newZone.Height <= currentImage.Height)
{
if (_isEditingDetectionZone)
{
_camera.SetDetectionZone(newZone);
}
else if (_isEditingLedZone)
{
_camera.SetLedZone(_currentLedIndex, newZone);
}
picBoxCamera.Update();
}
}
else
{
if (handle >= 0)
{
picBoxCamera.Cursor = GetResizeCursor(handle);
}
else if (editRect.Contains(e.Location))
{
picBoxCamera.Cursor = Cursors.SizeAll;
}
else
{
picBoxCamera.Cursor = Cursors.Default;
}
}
}
else if (_isMoving)
{
picBoxCamera.Cursor = Cursors.SizeAll;
int dx = e.Location.X - _resizeStartPoint.X;
int dy = e.Location.Y - _resizeStartPoint.Y;
Rectangle newZone = new Rectangle(
_originalZone.X + (int)(dx / scaleX),
_originalZone.Y + (int)(dy / scaleY),
_originalZone.Width,
_originalZone.Height
);
if (newZone.X >= 0 && newZone.Y >= 0 &&
newZone.X + newZone.Width <= currentImage.Width &&
newZone.Y + newZone.Height <= currentImage.Height)
{
if (_selectedZoneIndex == 0)
{
_camera.SetDetectionZone(newZone);
}
else if (_selectedZoneIndex > 0)
{
_camera.SetLedZone(_selectedZoneIndex, newZone);
}
}
UpdateDataGridView();
picBoxCamera.Update();
}
}
private void PicBoxCamera_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
if (_isEditingDetectionZone)
{
_isEditingDetectionZone = false;
_isMoving = false;
_isResizing = false;
toolStripButton1.Checked = false;
toolStripButton1.ToolTipText = "修改检测区(点击开启)";
toolStripButton2.Visible = true;
toolStripButton3.Visible = false;
picBoxCamera.Invalidate();
return;
}
if (_isDrawingLedMode || _isEditingLedZone || _isDrawing)
{
_isDrawingLedMode = false;
_isEditingLedZone = false;
_isMoving = false;
_isResizing = false;
_currentLedIndex = -1;
_selectedZoneIndex = -1;
_isDrawing = false;
toolStripButton2.Checked = false;
toolStripButton2.ToolTipText = "绘制Led区(点击开启)";
toolStripButton1.Visible = true;
UpdateLedZoneButtonsVisibility(0);
picBoxCamera.Invalidate();
return;
}
}
Image currentImage = _camera != null ? _camera.GetCurrentImage() : null;
if (currentImage == null || _camera == null) return;
int imageWidth, imageHeight;
try
{
imageWidth = currentImage.Width;
imageHeight = currentImage.Height;
}
catch
{
return;
}
float scaleX = (float)picBoxCamera.ClientSize.Width / imageWidth;
float scaleY = (float)picBoxCamera.ClientSize.Height / imageHeight;
Point imagePoint = new Point((int)(e.Location.X / scaleX), (int)(e.Location.Y / scaleY));
if (_isDrawing)
{
int x = Math.Min(_startPoint.X, imagePoint.X);
int y = Math.Min(_startPoint.Y, imagePoint.Y);
int width = Math.Abs(imagePoint.X - _startPoint.X);
int height = Math.Abs(imagePoint.Y - _startPoint.Y);
if (width > 5 && height > 5)
{
if (_isDrawingLedMode)
{
// 确保是新增LED区而不是修改现有LED区
int newIndex = _camera.GetLedZoneCount() + 1;
// 确保新索引不与现有索引冲突
while (_camera.GetLedZones().ContainsKey(newIndex))
{
newIndex++;
}
_camera.AddLedZone(newIndex, new Rectangle(x, y, width, height), Color.Lime);
_selectedZoneIndex = newIndex;
_currentLedIndex = newIndex;
_isEditingLedZone = true;
_isDrawingLedMode = true;
UpdateLedZoneButtonsVisibility(1);
UpdateColorButtonIcon();
}
else
{
_camera.SetDetectionZone(new Rectangle(x, y, width, height));
_selectedZoneIndex = 0;
}
}
}
_isDrawing = false;
_isMoving = false;
_isResizing = false;
_hoveredHandle = -1;
UpdateDataGridView();
picBoxCamera.Invalidate();
}
private int GetHoveredHandle(Rectangle rect, Point point)
{
int handleSize = 8;
int[] xPoints = { rect.X, rect.X + rect.Width / 2, rect.X + rect.Width, rect.X + rect.Width, rect.X + rect.Width, rect.X + rect.Width / 2, rect.X, rect.X };
int[] yPoints = { rect.Y, rect.Y, rect.Y, rect.Y + rect.Height / 2, rect.Y + rect.Height, rect.Y + rect.Height, rect.Y + rect.Height, rect.Y + rect.Height / 2 };
for (int i = 0; i < 8; i++)
{
Rectangle handleRect = new Rectangle(
xPoints[i] - handleSize / 2,
yPoints[i] - handleSize / 2,
handleSize,
handleSize
);
if (handleRect.Contains(point))
{
return i;
}
}
return -1;
}
private Rectangle AdjustRectangle(Rectangle rect, int handle, int dx, int dy)
{
int x = rect.X;
int y = rect.Y;
int width = rect.Width;
int height = rect.Height;
switch (handle)
{
case 0:
x += dx;
y += dy;
width -= dx;
height -= dy;
break;
case 1:
y += dy;
height -= dy;
break;
case 2:
y += dy;
width += dx;
height -= dy;
break;
case 3:
width += dx;
break;
case 4:
width += dx;
height += dy;
break;
case 5:
height += dy;
break;
case 6:
x += dx;
width -= dx;
height += dy;
break;
case 7:
x += dx;
width -= dx;
break;
}
if (width < 10) width = 10;
if (height < 10) height = 10;
return new Rectangle(x, y, width, height);
}
private Cursor GetResizeCursor(int handle)
{
switch (handle)
{
case 0:
case 4:
return Cursors.SizeNWSE;
case 2:
case 6:
return Cursors.SizeNESW;
case 1:
case 5:
return Cursors.SizeNS;
case 3:
case 7:
return Cursors.SizeWE;
default:
return Cursors.Default;
}
}
private void DataGridView1_SelectionChanged(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
_selectedZoneIndex = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells["Index"].Value);
picBoxCamera.Invalidate();
}
}
private void ToolStripButton1_Click(object sender, EventArgs e)
{
if (toolStripButton1.Checked)
{
_isEditingDetectionZone = true;
_isEditingLedZone = false;
_selectedZoneIndex = 0;
toolStripButton1.ToolTipText = "修改检测区(点击关闭)";
toolStripButton2.Checked = false;
toolStripButton2.Visible = false;
toolStripButton3.Visible = true;
UpdateColorButtonIcon();
toolStripButton2.ToolTipText = "绘制Led区(点击开启/关闭)";
}
else
{
_isEditingDetectionZone = false;
_selectedZoneIndex = -1;
toolStripButton1.ToolTipText = "修改检测区(点击开启)";
toolStripButton2.Visible = true;
toolStripButton3.Visible = false;
}
picBoxCamera.Invalidate();
UpdateDataGridView();
}
private void ToolStripButton2_Click(object sender, EventArgs e)
{
if (toolStripButton2.Checked)
{
_isDrawingLedMode = true;
_isEditingDetectionZone = false;
_isMoving = false;
_isResizing = false;
_isDrawing = false;
_selectedZoneIndex = -1;
_currentLedIndex = -1;
toolStripButton2.ToolTipText = "绘制Led区(点击关闭)";
toolStripButton1.Checked = false;
toolStripButton1.Visible = false;
UpdateLedZoneButtonsVisibility(2);
UpdateColorButtonIcon();
toolStripButton1.ToolTipText = "修改检测区(点击开启/关闭)";
}
else
{
_isDrawingLedMode = false;
_isEditingLedZone = false;
_currentLedIndex = -1;
_selectedZoneIndex = -1;
toolStripButton2.ToolTipText = "绘制Led区(点击开启)";
toolStripButton1.Visible = true;
UpdateLedZoneButtonsVisibility(0);
_isDrawing = false;
}
picBoxCamera.Invalidate();
UpdateDataGridView();
}
private void ToolStripButton3_Click(object sender, EventArgs e)
{
using (ColorDialog colorDialog = new ColorDialog())
{
colorDialog.FullOpen = true;
if (_isEditingDetectionZone)
{
colorDialog.Color = _detectionZoneColor;
}
else if ((_isEditingLedZone || _isDrawingLedMode) && _currentLedIndex >= 0)
{
colorDialog.Color = _camera.GetLedZoneColor(_currentLedIndex);
}
if (colorDialog.ShowDialog() == DialogResult.OK)
{
if (_isEditingDetectionZone)
{
_detectionZoneColor = colorDialog.Color;
}
else if ((_isEditingLedZone || _isDrawingLedMode) && _currentLedIndex >= 0)
{
_camera.SetLedZoneColor(_currentLedIndex, colorDialog.Color);
}
UpdateColorButtonIcon();
picBoxCamera.Invalidate();
}
}
}
private void ToolStripButton4_Click(object sender, EventArgs e)
{
if ((_isEditingLedZone || _isDrawingLedMode) && _currentLedIndex >= 0)
{
_camera.RemoveLedZone(_currentLedIndex);
_currentLedIndex = -1;
_selectedZoneIndex = -1;
_isEditingLedZone = false;
_isDrawingLedMode = false;
toolStripButton2.Checked = false;
toolStripButton2.Visible = true;
toolStripButton2.ToolTipText = "绘制Led区(点击开启)";
toolStripButton1.Visible = true;
toolStripTextBox1.Visible = false;
toolStripTextBox1.Text = "";
UpdateLedZoneButtonsVisibility(0);
UpdateDataGridView();
picBoxCamera.Invalidate();
}
}
private void ToolStripTextBox1_TextChanged(object sender, EventArgs e)
{
if (_currentLedIndex >= 0 && int.TryParse(toolStripTextBox1.Text, out int newIndex))
{
if (newIndex > 0)
{
Rectangle ledZone = _camera.GetLedZone(_currentLedIndex);
Color ledColor = _camera.GetLedZoneColor(_currentLedIndex);
_camera.RemoveLedZone(_currentLedIndex);
_camera.AddLedZone(newIndex, ledZone, ledColor);
_currentLedIndex = newIndex;
_selectedZoneIndex = newIndex;
UpdateDataGridView();
picBoxCamera.Invalidate();
}
}
}
private void ToolStripTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (_currentLedIndex >= 0 && int.TryParse(toolStripTextBox1.Text, out int newIndex))
{
if (newIndex > 0)
{
Rectangle ledZone = _camera.GetLedZone(_currentLedIndex);
Color ledColor = _camera.GetLedZoneColor(_currentLedIndex);
_camera.RemoveLedZone(_currentLedIndex);
_camera.AddLedZone(newIndex, ledZone, ledColor);
_currentLedIndex = newIndex;
_selectedZoneIndex = newIndex;
toolStripTextBox1.Text = newIndex.ToString();
UpdateDataGridView();
picBoxCamera.Invalidate();
}
}
toolStripTextBox1.SelectAll();
e.SuppressKeyPress = true;
}
}
private void PicBoxCamera_SizeChanged(object sender, EventArgs e)
{
picBoxCamera.Invalidate();
}
private void UpdateColorButtonIcon()
{
Color currentColor;
if (_isEditingDetectionZone)
{
currentColor = _detectionZoneColor;
}
else if ((_isEditingLedZone || _isDrawingLedMode) && _currentLedIndex >= 0)
{
currentColor = _camera.GetLedZoneColor(_currentLedIndex);
}
else
{
currentColor = _camera.GetLedZoneColor();
}
Bitmap bmp = new Bitmap(24, 24);
using (Graphics g = Graphics.FromImage(bmp))
{
using (Brush brush = new SolidBrush(currentColor))
{
g.FillRectangle(Brushes.White, 0, 0, 24, 24);
g.FillRectangle(brush, 2, 2, 20, 20);
}
}
toolStripButton3.Image = bmp;
}
private void UpdateLedZoneButtonsVisibility(int state)
{
_ledZoneState = state;
switch (state)
{
case 0: // 就绪状态
toolStripButton2.Visible = true;
toolStripButton3.Visible = false;
toolStripButton4.Visible = false;
toolStripTextBox1.Visible = false;
toolStripTextBox1.Text = "";
break;
case 1: // 选中状态
toolStripButton2.Visible = false;
toolStripButton3.Visible = true;
toolStripButton4.Visible = true;
toolStripTextBox1.Visible = true;
toolStripTextBox1.Text = _currentLedIndex.ToString();
break;
case 2: // 绘制状态
toolStripButton2.Visible = true;
toolStripButton3.Visible = true;
toolStripButton4.Visible = false;
toolStripTextBox1.Visible = false;
toolStripTextBox1.Text = "";
break;
}
}
private void Setting_FormClosed(object sender, FormClosedEventArgs e)
{
if (_camera != null)
{
_camera.SetDetectionZoneColor(_detectionZoneColor);
_camera.SaveConfig();
_camera.ImageCaptured -= Camera_ImageCaptured;
}
}
private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
}
}
}