解决异常

This commit is contained in:
zqm
2026-03-25 10:23:26 +08:00
parent e1cf0ad723
commit dbc3617ba8
2 changed files with 596 additions and 90 deletions

View File

@@ -21,9 +21,9 @@ namespace Camera
this.picBoxCamera = new System.Windows.Forms.PictureBox(); this.picBoxCamera = new System.Windows.Forms.PictureBox();
this.splitContainer2 = new System.Windows.Forms.SplitContainer(); this.splitContainer2 = new System.Windows.Forms.SplitContainer();
this.toolStrip1 = new System.Windows.Forms.ToolStrip(); this.toolStrip1 = new System.Windows.Forms.ToolStrip();
this.toolStripButton2 = new System.Windows.Forms.ToolStripButton();
this.toolStripButton1 = new System.Windows.Forms.ToolStripButton(); this.toolStripButton1 = new System.Windows.Forms.ToolStripButton();
this.dataGridView1 = new System.Windows.Forms.DataGridView(); this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.toolStripButton2 = new System.Windows.Forms.ToolStripButton();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
this.splitContainer1.Panel1.SuspendLayout(); this.splitContainer1.Panel1.SuspendLayout();
this.splitContainer1.Panel2.SuspendLayout(); this.splitContainer1.Panel2.SuspendLayout();
@@ -32,9 +32,9 @@ namespace Camera
((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).BeginInit();
this.splitContainer2.Panel1.SuspendLayout(); this.splitContainer2.Panel1.SuspendLayout();
this.splitContainer2.Panel2.SuspendLayout(); this.splitContainer2.Panel2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.splitContainer2.SuspendLayout(); this.splitContainer2.SuspendLayout();
this.toolStrip1.SuspendLayout(); this.toolStrip1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout(); this.SuspendLayout();
// //
// splitContainer1 // splitContainer1
@@ -62,7 +62,7 @@ namespace Camera
this.picBoxCamera.Dock = System.Windows.Forms.DockStyle.Fill; this.picBoxCamera.Dock = System.Windows.Forms.DockStyle.Fill;
this.picBoxCamera.Location = new System.Drawing.Point(0, 0); this.picBoxCamera.Location = new System.Drawing.Point(0, 0);
this.picBoxCamera.Name = "picBoxCamera"; this.picBoxCamera.Name = "picBoxCamera";
this.picBoxCamera.Size = new System.Drawing.Size(100, 469); this.picBoxCamera.Size = new System.Drawing.Size(700, 515);
this.picBoxCamera.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage; this.picBoxCamera.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.picBoxCamera.TabIndex = 0; this.picBoxCamera.TabIndex = 0;
this.picBoxCamera.TabStop = false; this.picBoxCamera.TabStop = false;
@@ -74,7 +74,6 @@ namespace Camera
// splitContainer2 // splitContainer2
// //
this.splitContainer2.Dock = System.Windows.Forms.DockStyle.Fill; this.splitContainer2.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer2.IsSplitterFixed = false;
this.splitContainer2.Location = new System.Drawing.Point(0, 0); this.splitContainer2.Location = new System.Drawing.Point(0, 0);
this.splitContainer2.Name = "splitContainer2"; this.splitContainer2.Name = "splitContainer2";
this.splitContainer2.Orientation = System.Windows.Forms.Orientation.Horizontal; this.splitContainer2.Orientation = System.Windows.Forms.Orientation.Horizontal;
@@ -88,37 +87,48 @@ namespace Camera
// //
this.splitContainer2.Panel2.Controls.Add(this.dataGridView1); this.splitContainer2.Panel2.Controls.Add(this.dataGridView1);
this.splitContainer2.Panel2MinSize = 100; this.splitContainer2.Panel2MinSize = 100;
this.splitContainer2.Size = new System.Drawing.Size(746, 469); this.splitContainer2.Size = new System.Drawing.Size(559, 515);
this.splitContainer2.SplitterDistance = 257;
this.splitContainer2.TabIndex = 0; this.splitContainer2.TabIndex = 0;
// //
// toolStrip1 // toolStrip1
// //
this.toolStrip1.Dock = System.Windows.Forms.DockStyle.Fill; this.toolStrip1.Dock = System.Windows.Forms.DockStyle.None;
this.toolStrip1.ImageScalingSize = new System.Drawing.Size(20, 20); this.toolStrip1.ImageScalingSize = new System.Drawing.Size(32, 32);
this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripButton1}); this.toolStripButton1,
this.toolStripButton2});
this.toolStrip1.Location = new System.Drawing.Point(0, 0); this.toolStrip1.Location = new System.Drawing.Point(0, 0);
this.toolStrip1.Name = "toolStrip1"; this.toolStrip1.Name = "toolStrip1";
this.toolStrip1.Size = new System.Drawing.Size(80, 40);
this.toolStrip1.TabIndex = 0; this.toolStrip1.TabIndex = 0;
this.toolStrip1.ItemClicked += new System.Windows.Forms.ToolStripItemClickedEventHandler(this.toolStrip1_ItemClicked); this.toolStrip1.ItemClicked += new System.Windows.Forms.ToolStripItemClickedEventHandler(this.toolStrip1_ItemClicked);
// //
// toolStripButton1
//
this.toolStripButton1.CheckOnClick = true;
this.toolStripButton1.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.toolStripButton1.Name = "toolStripButton1";
this.toolStripButton1.Size = new System.Drawing.Size(36, 36);
this.toolStripButton1.ToolTipText = "修改检测区(点击开启/关闭)";
this.toolStripButton1.Click += new System.EventHandler(this.ToolStripButton1_Click);
//
// toolStripButton2 // toolStripButton2
// //
this.toolStripButton1.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text; this.toolStripButton2.CheckOnClick = true;
this.toolStripButton1.Font = new System.Drawing.Font("Microsoft YaHei UI", 9F, System.Drawing.FontStyle.Bold); this.toolStripButton2.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.toolStripButton1.ForeColor = System.Drawing.Color.Blue; this.toolStripButton2.Name = "toolStripButton2";
this.toolStripButton1.Name = "toolStripButton1"; this.toolStripButton2.Size = new System.Drawing.Size(36, 36);
this.toolStripButton1.Text = "修改检测区"; this.toolStripButton2.ToolTipText = "绘制Led区(点击开启/关闭)";
this.toolStripButton1.CheckOnClick = true; this.toolStripButton2.Click += new System.EventHandler(this.ToolStripButton2_Click);
this.toolStripButton1.Click += new System.EventHandler(this.ToolStripButton1_Click);
// //
// dataGridView1 // dataGridView1
// //
this.dataGridView1.AllowUserToAddRows = false; this.dataGridView1.AllowUserToAddRows = false;
this.dataGridView1.AllowUserToDeleteRows = false; this.dataGridView1.AllowUserToDeleteRows = false;
this.dataGridView1.AllowUserToResizeRows = false; this.dataGridView1.AllowUserToResizeRows = false;
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
this.dataGridView1.ColumnHeadersHeight = 25; this.dataGridView1.ColumnHeadersHeight = 25;
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill; this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.dataGridView1.Location = new System.Drawing.Point(0, 0); this.dataGridView1.Location = new System.Drawing.Point(0, 0);
this.dataGridView1.Name = "dataGridView1"; this.dataGridView1.Name = "dataGridView1";
@@ -126,7 +136,6 @@ namespace Camera
this.dataGridView1.RowHeadersVisible = false; this.dataGridView1.RowHeadersVisible = false;
this.dataGridView1.RowTemplate.Height = 25; this.dataGridView1.RowTemplate.Height = 25;
this.dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect; this.dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
this.dataGridView1.Size = new System.Drawing.Size(746, 415);
this.dataGridView1.TabIndex = 0; this.dataGridView1.TabIndex = 0;
this.dataGridView1.SelectionChanged += new System.EventHandler(this.DataGridView1_SelectionChanged); this.dataGridView1.SelectionChanged += new System.EventHandler(this.DataGridView1_SelectionChanged);
// //
@@ -135,10 +144,11 @@ namespace Camera
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F); this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1263, 515); this.ClientSize = new System.Drawing.Size(1263, 515);
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Controls.Add(this.splitContainer1); this.Controls.Add(this.splitContainer1);
this.Name = "Setting"; this.Name = "Setting";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "设置"; this.Text = "设置";
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Setting_FormClosed);
this.Load += new System.EventHandler(this.Setting_Load); this.Load += new System.EventHandler(this.Setting_Load);
this.splitContainer1.Panel1.ResumeLayout(false); this.splitContainer1.Panel1.ResumeLayout(false);
this.splitContainer1.Panel2.ResumeLayout(false); this.splitContainer1.Panel2.ResumeLayout(false);
@@ -149,10 +159,10 @@ namespace Camera
this.splitContainer2.Panel1.PerformLayout(); this.splitContainer2.Panel1.PerformLayout();
this.splitContainer2.Panel2.ResumeLayout(false); this.splitContainer2.Panel2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.splitContainer2.ResumeLayout(false); this.splitContainer2.ResumeLayout(false);
this.toolStrip1.ResumeLayout(false); this.toolStrip1.ResumeLayout(false);
this.toolStrip1.PerformLayout(); this.toolStrip1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false); this.ResumeLayout(false);
} }

View File

@@ -16,8 +16,17 @@ namespace Camera
private int _selectedZoneIndex = -1; private int _selectedZoneIndex = -1;
private bool _isDrawing = false; private bool _isDrawing = false;
private bool _isMoving = false; private bool _isMoving = false;
private bool _isResizing = false;
private Point _startPoint; private Point _startPoint;
private Rectangle _originalZone; private Rectangle _originalZone;
private Rectangle _tempZone;
private bool _isEditingDetectionZone = false;
private bool _isEditingLedZone = false;
private Point _resizeStartPoint;
private Rectangle _originalResizeRect;
private int _hoveredHandle = -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() public Setting()
{ {
@@ -65,6 +74,12 @@ namespace Camera
private void Setting_Load(object sender, EventArgs e) private void Setting_Load(object sender, EventArgs e)
{ {
CreateToolIcons();
if (_camera != null)
{
_camera.ImageCaptured += Camera_ImageCaptured;
}
picBoxCamera.BackColor = Color.Gray; picBoxCamera.BackColor = Color.Gray;
splitContainer1.FixedPanel = System.Windows.Forms.FixedPanel.Panel2; splitContainer1.FixedPanel = System.Windows.Forms.FixedPanel.Panel2;
splitContainer1.Panel2MinSize = 380; splitContainer1.Panel2MinSize = 380;
@@ -73,6 +88,109 @@ namespace Camera
UpdateDataGridView(); 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;
}
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)
{
if (picBoxCamera.Image != null)
{
picBoxCamera.Image.Dispose();
}
picBoxCamera.Image = image;
}
private void UpdateDataGridView() private void UpdateDataGridView()
{ {
dataGridView1.Rows.Clear(); dataGridView1.Rows.Clear();
@@ -98,12 +216,26 @@ namespace Camera
private void PicBoxCamera_Paint(object sender, PaintEventArgs e) private void PicBoxCamera_Paint(object sender, PaintEventArgs e)
{ {
if (picBoxCamera.Image == null) return; Image currentImage = picBoxCamera.Image;
if (currentImage == null) return;
int imageWidth, imageHeight;
try
{
imageWidth = currentImage.Width;
imageHeight = currentImage.Height;
if (imageWidth <= 0 || imageHeight <= 0) return;
}
catch
{
return;
}
Graphics g = e.Graphics; Graphics g = e.Graphics;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
float scaleX = (float)picBoxCamera.ClientSize.Width / picBoxCamera.Image.Width; float scaleX = (float)picBoxCamera.ClientSize.Width / imageWidth;
float scaleY = (float)picBoxCamera.ClientSize.Height / picBoxCamera.Image.Height; float scaleY = (float)picBoxCamera.ClientSize.Height / imageHeight;
Rectangle detectionZone = _camera != null ? _camera.GetDetectionZone() : new Rectangle(0, 0, 0, 0); Rectangle detectionZone = _camera != null ? _camera.GetDetectionZone() : new Rectangle(0, 0, 0, 0);
Rectangle ledZone = _camera != null ? _camera.GetLedZone() : new Rectangle(0, 0, 0, 0); Rectangle ledZone = _camera != null ? _camera.GetLedZone() : new Rectangle(0, 0, 0, 0);
@@ -122,33 +254,76 @@ namespace Camera
(int)(ledZone.Height * scaleY) (int)(ledZone.Height * scaleY)
); );
using (Pen pen = new Pen(Color.Red, 2)) Rectangle currentEditZone = new Rectangle(0, 0, 0, 0);
bool isEditing = false;
Color editColor = Color.Red;
if (_isEditingDetectionZone)
{ {
g.DrawRectangle(pen, scaledDetectionZone); currentEditZone = scaledDetectionZone;
isEditing = true;
editColor = Color.Red;
}
else if (_isEditingLedZone)
{
currentEditZone = scaledLedZone;
isEditing = true;
editColor = Color.Lime;
} }
using (Pen pen = new Pen(Color.Green, 2)) if (isEditing && currentEditZone.Width > 0 && currentEditZone.Height > 0)
{ {
g.DrawRectangle(pen, scaledLedZone); 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(Color.Red, 2))
{
g.DrawRectangle(pen, scaledDetectionZone);
}
using (Font font = new Font("Arial", 10)) using (Pen pen = new Pen(Color.Green, 2))
{ {
g.DrawString("检测区", font, Brushes.Red, scaledDetectionZone.X, scaledDetectionZone.Y - 15); g.DrawRectangle(pen, scaledLedZone);
g.DrawString("Led区", font, Brushes.Green, scaledLedZone.X, scaledLedZone.Y - 15); }
}
if (_selectedZoneIndex == 0) using (Font font = new Font("Arial", 10))
{ {
DrawSelectionHandles(g, scaledDetectionZone); g.DrawString("检测区", font, Brushes.Red, scaledDetectionZone.X, scaledDetectionZone.Y - 15);
} g.DrawString("Led区", font, Brushes.Green, scaledLedZone.X, scaledLedZone.Y - 15);
else if (_selectedZoneIndex == 1) }
{
DrawSelectionHandles(g, scaledLedZone);
} }
} }
private void DrawSelectionHandles(Graphics g, Rectangle rect) 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; int handleSize = 8;
using (Brush brush = new SolidBrush(Color.White)) using (Brush brush = new SolidBrush(Color.White))
@@ -158,38 +333,93 @@ namespace Camera
g.FillRectangle(brush, rect.X - handleSize / 2, rect.Y + rect.Height - 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); 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) private void PicBoxCamera_MouseDown(object sender, MouseEventArgs e)
{ {
if (picBoxCamera.Image == null || _camera == null) return; Image currentImage = picBoxCamera.Image;
if (currentImage == null || _camera == null) return;
float scaleX = (float)picBoxCamera.Image.Width / picBoxCamera.ClientSize.Width; int imageWidth, imageHeight;
float scaleY = (float)picBoxCamera.Image.Height / picBoxCamera.ClientSize.Height; try
{
imageWidth = currentImage.Width;
imageHeight = currentImage.Height;
}
catch
{
return;
}
float scaleX = (float)imageWidth / picBoxCamera.ClientSize.Width;
float scaleY = (float)imageHeight / picBoxCamera.ClientSize.Height;
Point imagePoint = new Point((int)(e.X * scaleX), (int)(e.Y * scaleY)); Point imagePoint = new Point((int)(e.X * scaleX), (int)(e.Y * scaleY));
Point controlPoint = e.Location;
Rectangle detectionZone = _camera.GetDetectionZone(); Rectangle detectionZone = _camera.GetDetectionZone();
Rectangle ledZone = _camera.GetLedZone(); Rectangle ledZone = _camera.GetLedZone();
if (detectionZone.Contains(imagePoint)) Rectangle scaledDetectionZone = new Rectangle(
(int)(detectionZone.X * scaleX),
(int)(detectionZone.Y * scaleY),
(int)(detectionZone.Width * scaleX),
(int)(detectionZone.Height * scaleY)
);
Rectangle scaledLedZone = new Rectangle(
(int)(ledZone.X * scaleX),
(int)(ledZone.Y * scaleY),
(int)(ledZone.Width * scaleX),
(int)(ledZone.Height * scaleY)
);
if (_isEditingDetectionZone || _isEditingLedZone)
{ {
_selectedZoneIndex = 0; Rectangle editRect = _isEditingDetectionZone ? scaledDetectionZone : scaledLedZone;
_isMoving = true; _hoveredHandle = GetHoveredHandle(editRect, controlPoint);
_startPoint = imagePoint;
_originalZone = detectionZone; if (_hoveredHandle >= 0)
} {
else if (ledZone.Contains(imagePoint)) _isResizing = true;
{ _resizeStartPoint = controlPoint;
_selectedZoneIndex = 1; _originalResizeRect = editRect;
_isMoving = true; }
_startPoint = imagePoint; else if (editRect.Contains(controlPoint))
_originalZone = ledZone; {
_isMoving = true;
_startPoint = imagePoint;
_originalZone = _isEditingDetectionZone ? detectionZone : ledZone;
}
} }
else else
{ {
_selectedZoneIndex = -1; if (detectionZone.Contains(imagePoint))
_isDrawing = true; {
_startPoint = imagePoint; _selectedZoneIndex = 0;
_isMoving = true;
_startPoint = imagePoint;
_originalZone = detectionZone;
}
else if (ledZone.Contains(imagePoint))
{
_selectedZoneIndex = 1;
_isMoving = true;
_startPoint = imagePoint;
_originalZone = ledZone;
}
else
{
_selectedZoneIndex = -1;
_isDrawing = true;
_startPoint = imagePoint;
}
} }
UpdateDataGridView(); UpdateDataGridView();
@@ -198,46 +428,170 @@ namespace Camera
private void PicBoxCamera_MouseMove(object sender, MouseEventArgs e) private void PicBoxCamera_MouseMove(object sender, MouseEventArgs e)
{ {
if (picBoxCamera.Image == null || _camera == null || !_isMoving) return; Image currentImage = picBoxCamera.Image;
if (currentImage == null || _camera == null) return;
float scaleX = (float)picBoxCamera.Image.Width / picBoxCamera.ClientSize.Width; int imageWidth, imageHeight;
float scaleY = (float)picBoxCamera.Image.Height / picBoxCamera.ClientSize.Height; try
Point imagePoint = new Point((int)(e.X * scaleX), (int)(e.Y * scaleY));
int dx = imagePoint.X - _startPoint.X;
int dy = imagePoint.Y - _startPoint.Y;
Rectangle newZone = new Rectangle(
_originalZone.X + dx,
_originalZone.Y + dy,
_originalZone.Width,
_originalZone.Height
);
if (newZone.X >= 0 && newZone.Y >= 0 &&
newZone.X + newZone.Width <= picBoxCamera.Image.Width &&
newZone.Y + newZone.Height <= picBoxCamera.Image.Height)
{ {
if (_selectedZoneIndex == 0) imageWidth = currentImage.Width;
{ imageHeight = currentImage.Height;
_camera.SetDetectionZone(newZone); }
} catch
else if (_selectedZoneIndex == 1) {
{ return;
_camera.SetLedZone(newZone);
}
} }
UpdateDataGridView(); float scaleX = (float)imageWidth / picBoxCamera.ClientSize.Width;
picBoxCamera.Invalidate(); float scaleY = (float)imageHeight / picBoxCamera.ClientSize.Height;
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(
(int)(ledZone.X * scaleX),
(int)(ledZone.Y * scaleY),
(int)(ledZone.Width * scaleX),
(int)(ledZone.Height * scaleY)
);
if (_isEditingDetectionZone || _isEditingLedZone)
{
Rectangle editRect = _isEditingDetectionZone ? scaledDetectionZone : scaledLedZone;
if (_hoveredHandle >= 0 && _isResizing)
{
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
{
_camera.SetLedZone(newZone);
}
picBoxCamera.Invalidate();
}
}
else if (_isMoving)
{
Point imagePoint = new Point((int)(e.X * scaleX), (int)(e.Y * scaleY));
int moveDx = imagePoint.X - _startPoint.X;
int moveDy = imagePoint.Y - _startPoint.Y;
Rectangle newZone = new Rectangle(
_originalZone.X + moveDx,
_originalZone.Y + moveDy,
_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
{
_camera.SetLedZone(newZone);
}
picBoxCamera.Invalidate();
}
}
else
{
int handle = GetHoveredHandle(editRect, e.Location);
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)
{
Point imagePoint = new Point((int)(e.X * scaleX), (int)(e.Y * scaleY));
int dx = imagePoint.X - _startPoint.X;
int dy = imagePoint.Y - _startPoint.Y;
Rectangle newZone = new Rectangle(
_originalZone.X + dx,
_originalZone.Y + dy,
_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 == 1)
{
_camera.SetLedZone(newZone);
}
}
UpdateDataGridView();
picBoxCamera.Invalidate();
}
} }
private void PicBoxCamera_MouseUp(object sender, MouseEventArgs e) private void PicBoxCamera_MouseUp(object sender, MouseEventArgs e)
{ {
if (picBoxCamera.Image == null || _camera == null) return; Image currentImage = picBoxCamera.Image;
if (currentImage == null || _camera == null) return;
float scaleX = (float)picBoxCamera.Image.Width / picBoxCamera.ClientSize.Width; int imageWidth, imageHeight;
float scaleY = (float)picBoxCamera.Image.Height / picBoxCamera.ClientSize.Height; try
{
imageWidth = currentImage.Width;
imageHeight = currentImage.Height;
}
catch
{
return;
}
float scaleX = (float)imageWidth / picBoxCamera.ClientSize.Width;
float scaleY = (float)imageHeight / picBoxCamera.ClientSize.Height;
Point imagePoint = new Point((int)(e.X * scaleX), (int)(e.Y * scaleY)); Point imagePoint = new Point((int)(e.X * scaleX), (int)(e.Y * scaleY));
if (_isDrawing && !_isMoving) if (_isDrawing && !_isMoving)
@@ -249,17 +603,121 @@ namespace Camera
if (width > 10 && height > 10) if (width > 10 && height > 10)
{ {
_camera.SetDetectionZone(new Rectangle(x, y, width, height)); if (toolStripButton2.Checked)
_selectedZoneIndex = 0; {
_camera.SetLedZone(new Rectangle(x, y, width, height));
_selectedZoneIndex = 1;
}
else
{
_camera.SetDetectionZone(new Rectangle(x, y, width, height));
_selectedZoneIndex = 0;
}
} }
} }
_isDrawing = false; _isDrawing = false;
_isMoving = false; _isMoving = false;
_isResizing = false;
_hoveredHandle = -1;
UpdateDataGridView(); UpdateDataGridView();
picBoxCamera.Invalidate(); picBoxCamera.Invalidate();
} }
private int GetHoveredHandle(Rectangle rect, Point point)
{
int handleSize = 10;
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) private void DataGridView1_SelectionChanged(object sender, EventArgs e)
{ {
if (dataGridView1.SelectedRows.Count > 0) if (dataGridView1.SelectedRows.Count > 0)
@@ -273,16 +731,54 @@ namespace Camera
{ {
if (toolStripButton1.Checked) if (toolStripButton1.Checked)
{ {
_isEditingDetectionZone = true;
_isEditingLedZone = false;
_selectedZoneIndex = 0; _selectedZoneIndex = 0;
toolStripButton1.ToolTipText = "修改检测区(点击关闭)";
toolStripButton2.Checked = false;
toolStripButton2.ToolTipText = "绘制Led区(点击开启/关闭)";
_tempZone = _camera.GetDetectionZone();
} }
else else
{ {
_isEditingDetectionZone = false;
_selectedZoneIndex = -1; _selectedZoneIndex = -1;
toolStripButton1.ToolTipText = "修改检测区(点击开启)";
} }
UpdateDataGridView(); UpdateDataGridView();
picBoxCamera.Invalidate(); picBoxCamera.Invalidate();
} }
private void ToolStripButton2_Click(object sender, EventArgs e)
{
if (toolStripButton2.Checked)
{
_isEditingLedZone = true;
_isEditingDetectionZone = false;
_selectedZoneIndex = 1;
toolStripButton2.ToolTipText = "绘制Led区(点击关闭)";
toolStripButton1.Checked = false;
toolStripButton1.ToolTipText = "修改检测区(点击开启/关闭)";
_tempZone = _camera.GetLedZone();
}
else
{
_isEditingLedZone = false;
_selectedZoneIndex = -1;
toolStripButton2.ToolTipText = "绘制Led区(点击开启)";
}
UpdateDataGridView();
picBoxCamera.Invalidate();
}
private void Setting_FormClosed(object sender, FormClosedEventArgs e)
{
if (_camera != null)
{
_camera.ImageCaptured -= Camera_ImageCaptured;
}
}
private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e) private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{ {