窗口缩放时图像一起缩放
This commit is contained in:
@@ -207,6 +207,8 @@ namespace Camera
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public void AddLedZone(int index, Rectangle zone, Color color)
|
public void AddLedZone(int index, Rectangle zone, Color color)
|
||||||
{
|
{
|
||||||
|
// 裁截LED区域,确保在图像范围内
|
||||||
|
zone = ClipZone(zone);
|
||||||
_ledZones[index] = zone;
|
_ledZones[index] = zone;
|
||||||
_ledZoneColors[index] = color;
|
_ledZoneColors[index] = color;
|
||||||
}
|
}
|
||||||
@@ -218,10 +220,35 @@ namespace Camera
|
|||||||
{
|
{
|
||||||
if (_ledZones.ContainsKey(index))
|
if (_ledZones.ContainsKey(index))
|
||||||
{
|
{
|
||||||
|
// 裁截LED区域,确保在图像范围内
|
||||||
|
zone = ClipZone(zone);
|
||||||
_ledZones[index] = 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>
|
/// <summary>
|
||||||
/// 移除Led区域
|
/// 移除Led区域
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -69,6 +69,7 @@ namespace Camera
|
|||||||
this.picBoxCamera.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Normal;
|
this.picBoxCamera.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Normal;
|
||||||
this.picBoxCamera.TabIndex = 0;
|
this.picBoxCamera.TabIndex = 0;
|
||||||
this.picBoxCamera.TabStop = false;
|
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.Paint += new System.Windows.Forms.PaintEventHandler(this.PicBoxCamera_Paint);
|
||||||
this.picBoxCamera.MouseDown += new System.Windows.Forms.MouseEventHandler(this.PicBoxCamera_MouseDown);
|
this.picBoxCamera.MouseDown += new System.Windows.Forms.MouseEventHandler(this.PicBoxCamera_MouseDown);
|
||||||
this.picBoxCamera.MouseMove += new System.Windows.Forms.MouseEventHandler(this.PicBoxCamera_MouseMove);
|
this.picBoxCamera.MouseMove += new System.Windows.Forms.MouseEventHandler(this.PicBoxCamera_MouseMove);
|
||||||
|
|||||||
@@ -53,12 +53,8 @@ namespace Camera
|
|||||||
{
|
{
|
||||||
picBoxCamera.Image.Dispose();
|
picBoxCamera.Image.Dispose();
|
||||||
}
|
}
|
||||||
Bitmap newImage = new Bitmap(image.Width, image.Height);
|
// 不设置picBoxCamera.Image,避免双重绘制
|
||||||
using (Graphics g = Graphics.FromImage(newImage))
|
// 只在Paint事件中绘制缩放后的图像
|
||||||
{
|
|
||||||
g.DrawImage(image, 0, 0);
|
|
||||||
}
|
|
||||||
picBoxCamera.Image = newImage;
|
|
||||||
picBoxCamera.Invalidate();
|
picBoxCamera.Invalidate();
|
||||||
}
|
}
|
||||||
catch { }
|
catch { }
|
||||||
@@ -225,12 +221,9 @@ namespace Camera
|
|||||||
{
|
{
|
||||||
picBoxCamera.Image.Dispose();
|
picBoxCamera.Image.Dispose();
|
||||||
}
|
}
|
||||||
Bitmap newImage = new Bitmap(image.Width, image.Height);
|
// 不设置picBoxCamera.Image,避免双重绘制
|
||||||
using (Graphics g = Graphics.FromImage(newImage))
|
// 只在Paint事件中绘制缩放后的图像
|
||||||
{
|
picBoxCamera.Invalidate();
|
||||||
g.DrawImage(image, 0, 0);
|
|
||||||
}
|
|
||||||
picBoxCamera.Image = newImage;
|
|
||||||
}
|
}
|
||||||
catch { }
|
catch { }
|
||||||
}
|
}
|
||||||
@@ -263,7 +256,8 @@ namespace Camera
|
|||||||
Image currentImage = null;
|
Image currentImage = null;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
currentImage = picBoxCamera.Image;
|
// 从Camera获取当前图像
|
||||||
|
currentImage = _camera != null ? _camera.GetCurrentImage() : null;
|
||||||
if (currentImage == null) return;
|
if (currentImage == null) return;
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
@@ -293,6 +287,7 @@ namespace Camera
|
|||||||
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
|
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
|
||||||
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
|
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
|
||||||
|
|
||||||
|
// 绘制缩放后的图像
|
||||||
Rectangle destRect = new Rectangle(0, 0, picBoxCamera.ClientSize.Width, picBoxCamera.ClientSize.Height);
|
Rectangle destRect = new Rectangle(0, 0, picBoxCamera.ClientSize.Width, picBoxCamera.ClientSize.Height);
|
||||||
g.DrawImage(currentImage, destRect, 0, 0, currentImage.Width, currentImage.Height, GraphicsUnit.Pixel);
|
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)
|
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;
|
if (currentImage == null || _camera == null) return;
|
||||||
|
|
||||||
int imageWidth, imageHeight;
|
int imageWidth, imageHeight;
|
||||||
@@ -635,8 +636,10 @@ namespace Camera
|
|||||||
_isEditingLedZone = false;
|
_isEditingLedZone = false;
|
||||||
}
|
}
|
||||||
_isDrawing = true;
|
_isDrawing = true;
|
||||||
|
_isDrawingLedMode = true; // 确保处于LED绘制模式
|
||||||
_startPoint = imagePoint;
|
_startPoint = imagePoint;
|
||||||
_resizeStartPoint = controlPoint;
|
_resizeStartPoint = controlPoint;
|
||||||
|
toolStripButton2.Checked = true; // 保持LED编辑按钮按下状态
|
||||||
UpdateLedZoneButtonsVisibility(2);
|
UpdateLedZoneButtonsVisibility(2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -665,7 +668,7 @@ namespace Camera
|
|||||||
private void PicBoxCamera_MouseMove(object sender, MouseEventArgs e)
|
private void PicBoxCamera_MouseMove(object sender, MouseEventArgs e)
|
||||||
{
|
{
|
||||||
_currentMousePoint = e.Location;
|
_currentMousePoint = e.Location;
|
||||||
Image currentImage = picBoxCamera.Image;
|
Image currentImage = _camera != null ? _camera.GetCurrentImage() : null;
|
||||||
if (currentImage == null || _camera == null) return;
|
if (currentImage == null || _camera == null) return;
|
||||||
|
|
||||||
int imageWidth, imageHeight;
|
int imageWidth, imageHeight;
|
||||||
@@ -835,7 +838,7 @@ namespace Camera
|
|||||||
picBoxCamera.Invalidate();
|
picBoxCamera.Invalidate();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (_isDrawingLedMode || _isEditingLedZone)
|
if (_isDrawingLedMode || _isEditingLedZone || _isDrawing)
|
||||||
{
|
{
|
||||||
_isDrawingLedMode = false;
|
_isDrawingLedMode = false;
|
||||||
_isEditingLedZone = 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;
|
if (currentImage == null || _camera == null) return;
|
||||||
|
|
||||||
int imageWidth, imageHeight;
|
int imageWidth, imageHeight;
|
||||||
@@ -1162,6 +1165,11 @@ namespace Camera
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void PicBoxCamera_SizeChanged(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
picBoxCamera.Invalidate();
|
||||||
|
}
|
||||||
|
|
||||||
private void UpdateColorButtonIcon()
|
private void UpdateColorButtonIcon()
|
||||||
{
|
{
|
||||||
Color currentColor;
|
Color currentColor;
|
||||||
|
|||||||
Reference in New Issue
Block a user