窗口缩放时图像一起缩放

This commit is contained in:
zqm
2026-03-26 09:31:14 +08:00
parent 25f93232a8
commit d779af975f
3 changed files with 53 additions and 17 deletions

View File

@@ -207,6 +207,8 @@ namespace Camera
/// </summary>
public void AddLedZone(int index, Rectangle zone, Color color)
{
// 裁截LED区域确保在图像范围内
zone = ClipZone(zone);
_ledZones[index] = zone;
_ledZoneColors[index] = color;
}
@@ -218,10 +220,35 @@ namespace Camera
{
if (_ledZones.ContainsKey(index))
{
// 裁截LED区域确保在图像范围内
zone = ClipZone(zone);
_ledZones[index] = zone;
}
}
/// <summary>
/// 裁截区域,确保在图像范围内
/// </summary>
private Rectangle ClipZone(Rectangle zone)
{
if (_currentImage == null) return zone;
int imageWidth = _currentImage.Width;
int imageHeight = _currentImage.Height;
// 确保坐标和尺寸在图像范围内
int x = Math.Max(0, zone.X);
int y = Math.Max(0, zone.Y);
int width = Math.Min(zone.Width, imageWidth - x);
int height = Math.Min(zone.Height, imageHeight - y);
// 确保宽度和高度为正数
width = Math.Max(1, width);
height = Math.Max(1, height);
return new Rectangle(x, y, width, height);
}
/// <summary>
/// 移除Led区域
/// </summary>

View File

@@ -69,6 +69,7 @@ namespace Camera
this.picBoxCamera.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Normal;
this.picBoxCamera.TabIndex = 0;
this.picBoxCamera.TabStop = false;
this.picBoxCamera.SizeChanged += new System.EventHandler(this.PicBoxCamera_SizeChanged);
this.picBoxCamera.Paint += new System.Windows.Forms.PaintEventHandler(this.PicBoxCamera_Paint);
this.picBoxCamera.MouseDown += new System.Windows.Forms.MouseEventHandler(this.PicBoxCamera_MouseDown);
this.picBoxCamera.MouseMove += new System.Windows.Forms.MouseEventHandler(this.PicBoxCamera_MouseMove);

View File

@@ -53,12 +53,8 @@ namespace Camera
{
picBoxCamera.Image.Dispose();
}
Bitmap newImage = new Bitmap(image.Width, image.Height);
using (Graphics g = Graphics.FromImage(newImage))
{
g.DrawImage(image, 0, 0);
}
picBoxCamera.Image = newImage;
// 不设置picBoxCamera.Image避免双重绘制
// 只在Paint事件中绘制缩放后的图像
picBoxCamera.Invalidate();
}
catch { }
@@ -225,12 +221,9 @@ namespace Camera
{
picBoxCamera.Image.Dispose();
}
Bitmap newImage = new Bitmap(image.Width, image.Height);
using (Graphics g = Graphics.FromImage(newImage))
{
g.DrawImage(image, 0, 0);
}
picBoxCamera.Image = newImage;
// 不设置picBoxCamera.Image避免双重绘制
// 只在Paint事件中绘制缩放后的图像
picBoxCamera.Invalidate();
}
catch { }
}
@@ -263,7 +256,8 @@ namespace Camera
Image currentImage = null;
try
{
currentImage = picBoxCamera.Image;
// 从Camera获取当前图像
currentImage = _camera != null ? _camera.GetCurrentImage() : null;
if (currentImage == null) return;
}
catch
@@ -293,6 +287,7 @@ namespace Camera
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);
@@ -514,7 +509,13 @@ namespace Camera
private void PicBoxCamera_MouseDown(object sender, MouseEventArgs e)
{
Image currentImage = picBoxCamera.Image;
// 处理右键点击
if (e.Button == MouseButtons.Right)
{
return; // 右键点击由MouseUp事件处理
}
Image currentImage = _camera != null ? _camera.GetCurrentImage() : null;
if (currentImage == null || _camera == null) return;
int imageWidth, imageHeight;
@@ -635,8 +636,10 @@ namespace Camera
_isEditingLedZone = false;
}
_isDrawing = true;
_isDrawingLedMode = true; // 确保处于LED绘制模式
_startPoint = imagePoint;
_resizeStartPoint = controlPoint;
toolStripButton2.Checked = true; // 保持LED编辑按钮按下状态
UpdateLedZoneButtonsVisibility(2);
}
}
@@ -665,7 +668,7 @@ namespace Camera
private void PicBoxCamera_MouseMove(object sender, MouseEventArgs e)
{
_currentMousePoint = e.Location;
Image currentImage = picBoxCamera.Image;
Image currentImage = _camera != null ? _camera.GetCurrentImage() : null;
if (currentImage == null || _camera == null) return;
int imageWidth, imageHeight;
@@ -835,7 +838,7 @@ namespace Camera
picBoxCamera.Invalidate();
return;
}
if (_isDrawingLedMode || _isEditingLedZone)
if (_isDrawingLedMode || _isEditingLedZone || _isDrawing)
{
_isDrawingLedMode = false;
_isEditingLedZone = false;
@@ -853,7 +856,7 @@ namespace Camera
}
}
Image currentImage = picBoxCamera.Image;
Image currentImage = _camera != null ? _camera.GetCurrentImage() : null;
if (currentImage == null || _camera == null) return;
int imageWidth, imageHeight;
@@ -1162,6 +1165,11 @@ namespace Camera
}
}
private void PicBoxCamera_SizeChanged(object sender, EventArgs e)
{
picBoxCamera.Invalidate();
}
private void UpdateColorButtonIcon()
{
Color currentColor;