From dbc3617ba8a5007e8e41dc651cf898a971a97326 Mon Sep 17 00:00:00 2001 From: zqm Date: Wed, 25 Mar 2026 10:23:26 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Camera/Camera/Setting.Designer.cs | 48 +- .../CS/Framework4.0/Camera/Camera/Setting.cs | 638 ++++++++++++++++-- 2 files changed, 596 insertions(+), 90 deletions(-) diff --git a/Windows/CS/Framework4.0/Camera/Camera/Setting.Designer.cs b/Windows/CS/Framework4.0/Camera/Camera/Setting.Designer.cs index 19af34b..fc80e8b 100644 --- a/Windows/CS/Framework4.0/Camera/Camera/Setting.Designer.cs +++ b/Windows/CS/Framework4.0/Camera/Camera/Setting.Designer.cs @@ -21,9 +21,9 @@ namespace Camera this.picBoxCamera = new System.Windows.Forms.PictureBox(); this.splitContainer2 = new System.Windows.Forms.SplitContainer(); this.toolStrip1 = new System.Windows.Forms.ToolStrip(); - this.toolStripButton2 = new System.Windows.Forms.ToolStripButton(); this.toolStripButton1 = new System.Windows.Forms.ToolStripButton(); this.dataGridView1 = new System.Windows.Forms.DataGridView(); + this.toolStripButton2 = new System.Windows.Forms.ToolStripButton(); ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit(); this.splitContainer1.Panel1.SuspendLayout(); this.splitContainer1.Panel2.SuspendLayout(); @@ -32,9 +32,9 @@ namespace Camera ((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).BeginInit(); this.splitContainer2.Panel1.SuspendLayout(); this.splitContainer2.Panel2.SuspendLayout(); - ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit(); this.splitContainer2.SuspendLayout(); this.toolStrip1.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit(); this.SuspendLayout(); // // splitContainer1 @@ -62,7 +62,7 @@ namespace Camera this.picBoxCamera.Dock = System.Windows.Forms.DockStyle.Fill; this.picBoxCamera.Location = new System.Drawing.Point(0, 0); 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.TabIndex = 0; this.picBoxCamera.TabStop = false; @@ -74,7 +74,6 @@ namespace Camera // splitContainer2 // this.splitContainer2.Dock = System.Windows.Forms.DockStyle.Fill; - this.splitContainer2.IsSplitterFixed = false; this.splitContainer2.Location = new System.Drawing.Point(0, 0); this.splitContainer2.Name = "splitContainer2"; this.splitContainer2.Orientation = System.Windows.Forms.Orientation.Horizontal; @@ -88,37 +87,48 @@ namespace Camera // this.splitContainer2.Panel2.Controls.Add(this.dataGridView1); 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; // // toolStrip1 // - this.toolStrip1.Dock = System.Windows.Forms.DockStyle.Fill; - this.toolStrip1.ImageScalingSize = new System.Drawing.Size(20, 20); + this.toolStrip1.Dock = System.Windows.Forms.DockStyle.None; + this.toolStrip1.ImageScalingSize = new System.Drawing.Size(32, 32); 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.Name = "toolStrip1"; + this.toolStrip1.Size = new System.Drawing.Size(80, 40); this.toolStrip1.TabIndex = 0; 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 // - this.toolStripButton1.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text; - this.toolStripButton1.Font = new System.Drawing.Font("Microsoft YaHei UI", 9F, System.Drawing.FontStyle.Bold); - this.toolStripButton1.ForeColor = System.Drawing.Color.Blue; - this.toolStripButton1.Name = "toolStripButton1"; - this.toolStripButton1.Text = "修改检测区"; - this.toolStripButton1.CheckOnClick = true; - this.toolStripButton1.Click += new System.EventHandler(this.ToolStripButton1_Click); + this.toolStripButton2.CheckOnClick = true; + this.toolStripButton2.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image; + this.toolStripButton2.Name = "toolStripButton2"; + this.toolStripButton2.Size = new System.Drawing.Size(36, 36); + this.toolStripButton2.ToolTipText = "绘制Led区(点击开启/关闭)"; + this.toolStripButton2.Click += new System.EventHandler(this.ToolStripButton2_Click); // // dataGridView1 // this.dataGridView1.AllowUserToAddRows = false; this.dataGridView1.AllowUserToDeleteRows = false; this.dataGridView1.AllowUserToResizeRows = false; - this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.DisableResizing; this.dataGridView1.ColumnHeadersHeight = 25; + this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.DisableResizing; this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill; this.dataGridView1.Location = new System.Drawing.Point(0, 0); this.dataGridView1.Name = "dataGridView1"; @@ -126,7 +136,6 @@ namespace Camera this.dataGridView1.RowHeadersVisible = false; this.dataGridView1.RowTemplate.Height = 25; this.dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect; - this.dataGridView1.Size = new System.Drawing.Size(746, 415); this.dataGridView1.TabIndex = 0; this.dataGridView1.SelectionChanged += new System.EventHandler(this.DataGridView1_SelectionChanged); // @@ -135,10 +144,11 @@ namespace Camera this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(1263, 515); - this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Controls.Add(this.splitContainer1); this.Name = "Setting"; + this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "设置"; + this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Setting_FormClosed); this.Load += new System.EventHandler(this.Setting_Load); this.splitContainer1.Panel1.ResumeLayout(false); this.splitContainer1.Panel2.ResumeLayout(false); @@ -149,10 +159,10 @@ namespace Camera this.splitContainer2.Panel1.PerformLayout(); this.splitContainer2.Panel2.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).EndInit(); - ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit(); this.splitContainer2.ResumeLayout(false); this.toolStrip1.ResumeLayout(false); this.toolStrip1.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit(); this.ResumeLayout(false); } diff --git a/Windows/CS/Framework4.0/Camera/Camera/Setting.cs b/Windows/CS/Framework4.0/Camera/Camera/Setting.cs index 53092b6..1632231 100644 --- a/Windows/CS/Framework4.0/Camera/Camera/Setting.cs +++ b/Windows/CS/Framework4.0/Camera/Camera/Setting.cs @@ -16,8 +16,17 @@ namespace Camera private int _selectedZoneIndex = -1; private bool _isDrawing = false; private bool _isMoving = false; + private bool _isResizing = false; private Point _startPoint; 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() { @@ -65,6 +74,12 @@ namespace Camera private void Setting_Load(object sender, EventArgs e) { + CreateToolIcons(); + + if (_camera != null) + { + _camera.ImageCaptured += Camera_ImageCaptured; + } picBoxCamera.BackColor = Color.Gray; splitContainer1.FixedPanel = System.Windows.Forms.FixedPanel.Panel2; splitContainer1.Panel2MinSize = 380; @@ -73,6 +88,109 @@ namespace Camera 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(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() { dataGridView1.Rows.Clear(); @@ -98,12 +216,26 @@ namespace Camera 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; + g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; - float scaleX = (float)picBoxCamera.ClientSize.Width / picBoxCamera.Image.Width; - float scaleY = (float)picBoxCamera.ClientSize.Height / picBoxCamera.Image.Height; + 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 ledZone = _camera != null ? _camera.GetLedZone() : new Rectangle(0, 0, 0, 0); @@ -122,33 +254,76 @@ namespace Camera (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)) - { - g.DrawString("检测区", font, Brushes.Red, scaledDetectionZone.X, scaledDetectionZone.Y - 15); - g.DrawString("Led区", font, Brushes.Green, scaledLedZone.X, scaledLedZone.Y - 15); - } + using (Pen pen = new Pen(Color.Green, 2)) + { + g.DrawRectangle(pen, scaledLedZone); + } - if (_selectedZoneIndex == 0) - { - DrawSelectionHandles(g, scaledDetectionZone); - } - else if (_selectedZoneIndex == 1) - { - DrawSelectionHandles(g, scaledLedZone); + using (Font font = new Font("Arial", 10)) + { + g.DrawString("检测区", font, Brushes.Red, scaledDetectionZone.X, scaledDetectionZone.Y - 15); + g.DrawString("Led区", font, Brushes.Green, scaledLedZone.X, scaledLedZone.Y - 15); + } } } - 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; 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 + 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 (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; - float scaleY = (float)picBoxCamera.Image.Height / picBoxCamera.ClientSize.Height; + int imageWidth, imageHeight; + 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 controlPoint = e.Location; Rectangle detectionZone = _camera.GetDetectionZone(); 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; - _isMoving = true; - _startPoint = imagePoint; - _originalZone = detectionZone; - } - else if (ledZone.Contains(imagePoint)) - { - _selectedZoneIndex = 1; - _isMoving = true; - _startPoint = imagePoint; - _originalZone = ledZone; + Rectangle editRect = _isEditingDetectionZone ? scaledDetectionZone : scaledLedZone; + _hoveredHandle = GetHoveredHandle(editRect, controlPoint); + + if (_hoveredHandle >= 0) + { + _isResizing = true; + _resizeStartPoint = controlPoint; + _originalResizeRect = editRect; + } + else if (editRect.Contains(controlPoint)) + { + _isMoving = true; + _startPoint = imagePoint; + _originalZone = _isEditingDetectionZone ? detectionZone : ledZone; + } } else { - _selectedZoneIndex = -1; - _isDrawing = true; - _startPoint = imagePoint; + if (detectionZone.Contains(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(); @@ -198,46 +428,170 @@ namespace Camera 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; - float scaleY = (float)picBoxCamera.Image.Height / picBoxCamera.ClientSize.Height; - 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) + int imageWidth, imageHeight; + try { - if (_selectedZoneIndex == 0) - { - _camera.SetDetectionZone(newZone); - } - else if (_selectedZoneIndex == 1) - { - _camera.SetLedZone(newZone); - } + imageWidth = currentImage.Width; + imageHeight = currentImage.Height; + } + catch + { + return; } - UpdateDataGridView(); - picBoxCamera.Invalidate(); + float scaleX = (float)imageWidth / picBoxCamera.ClientSize.Width; + 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) { - 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; - float scaleY = (float)picBoxCamera.Image.Height / picBoxCamera.ClientSize.Height; + int imageWidth, imageHeight; + 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)); if (_isDrawing && !_isMoving) @@ -249,17 +603,121 @@ namespace Camera if (width > 10 && height > 10) { - _camera.SetDetectionZone(new Rectangle(x, y, width, height)); - _selectedZoneIndex = 0; + if (toolStripButton2.Checked) + { + _camera.SetLedZone(new Rectangle(x, y, width, height)); + _selectedZoneIndex = 1; + } + 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 = 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) { if (dataGridView1.SelectedRows.Count > 0) @@ -273,16 +731,54 @@ namespace Camera { if (toolStripButton1.Checked) { + _isEditingDetectionZone = true; + _isEditingLedZone = false; _selectedZoneIndex = 0; + toolStripButton1.ToolTipText = "修改检测区(点击关闭)"; + toolStripButton2.Checked = false; + toolStripButton2.ToolTipText = "绘制Led区(点击开启/关闭)"; + _tempZone = _camera.GetDetectionZone(); } else { + _isEditingDetectionZone = false; _selectedZoneIndex = -1; + toolStripButton1.ToolTipText = "修改检测区(点击开启)"; } UpdateDataGridView(); 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) {