修复温差图画框逻辑问题:矩形区坐标调整和擦除模式预览框绘制到临时层

This commit is contained in:
zqm
2025-11-10 16:50:55 +08:00
parent 79606a53aa
commit 18c5fdf30b

View File

@@ -1267,14 +1267,76 @@ namespace JoyD.Windows.CS
// 获取相对于图像的当前坐标 // 获取相对于图像的当前坐标
Point currentImagePoint = ControlPointToImagePoint(e.Location); Point currentImagePoint = ControlPointToImagePoint(e.Location);
// 计算矩形参数 // 计算矩形参数:左上点是起笔方块的左上点,右下点是收笔方块的右下点
int x = Math.Min(_rectangleStartPoint.X, currentImagePoint.X); int x = _rectangleStartPoint.X;
int y = Math.Min(_rectangleStartPoint.Y, currentImagePoint.Y); int y = _rectangleStartPoint.Y;
int width = Math.Abs(currentImagePoint.X - _rectangleStartPoint.X); int width = currentImagePoint.X - _rectangleStartPoint.X;
int height = Math.Abs(currentImagePoint.Y - _rectangleStartPoint.Y); int height = currentImagePoint.Y - _rectangleStartPoint.Y;
// 确保矩形大小为正数,调整起点和大小
if (width < 0)
{
x += width;
width = -width;
}
if (height < 0)
{
y += height;
height = -height;
}
Rectangle rect = new Rectangle(x, y, width, height); Rectangle rect = new Rectangle(x, y, width, height);
// 创建临时位图用于双重缓冲,避免闪烁 // 对于擦除模式,在临时层上绘制预览框,不影响实际的温差图
if (_isEraseMode)
{
// 创建临时缓冲区用于绘制预览
Bitmap bufferBitmap = null;
try
{
bufferBitmap = new Bitmap(picBoxTemp.Width, picBoxTemp.Height);
using (Graphics g = Graphics.FromImage(bufferBitmap))
{
// 绘制背景图像
if (picBoxTemp.Image != null)
{
g.DrawImage(picBoxTemp.Image, 0, 0, picBoxTemp.Width, picBoxTemp.Height);
}
// 绘制温差层
if (_tempDiffOverlayImage != null)
{
g.DrawImage(_tempDiffOverlayImage, 0, 0, picBoxTemp.Width, picBoxTemp.Height);
}
// 绘制虚线预览框
using (Pen dashPen = new Pen(Color.White, 1))
{
dashPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
// 将图像坐标转换为控件坐标
Rectangle controlRect = ImageRectangleToControlRectangle(rect);
g.DrawRectangle(dashPen, controlRect);
}
}
// 直接在控件上绘制缓冲区内容
using (Graphics g = picBoxTemp.CreateGraphics())
{
g.DrawImage(bufferBitmap, 0, 0);
}
// 防止闪烁
return;
}
finally
{
if (bufferBitmap != null)
bufferBitmap.Dispose();
}
}
else
{
// 普通绘制模式:使用半透明填充和边框
using (Bitmap tempBitmap = new Bitmap(_tempDiffOverlayImage)) using (Bitmap tempBitmap = new Bitmap(_tempDiffOverlayImage))
{ {
using (Graphics g = Graphics.FromImage(tempBitmap)) using (Graphics g = Graphics.FromImage(tempBitmap))
@@ -1282,18 +1344,6 @@ namespace JoyD.Windows.CS
g.SmoothingMode = SmoothingMode.AntiAlias; g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.InterpolationMode = InterpolationMode.HighQualityBicubic;
if (_isEraseMode)
{
// 擦除模式显示虚线预览框不实际擦除等待MouseUp时再擦除
using (Pen dashPen = new Pen(Color.White, 1))
{
dashPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
g.DrawRectangle(dashPen, rect);
}
}
else
{
// 普通绘制模式:使用半透明填充和边框
// 创建半透明的填充颜色 // 创建半透明的填充颜色
Color fillColor = Color.FromArgb(128, selectedColor); Color fillColor = Color.FromArgb(128, selectedColor);
@@ -1306,7 +1356,6 @@ namespace JoyD.Windows.CS
g.DrawRectangle(pen, rect); g.DrawRectangle(pen, rect);
} }
} }
}
// 将临时位图复制回温差层图像 // 将临时位图复制回温差层图像
using (Graphics g = Graphics.FromImage(_tempDiffOverlayImage)) using (Graphics g = Graphics.FromImage(_tempDiffOverlayImage))
@@ -1314,6 +1363,7 @@ namespace JoyD.Windows.CS
g.DrawImage(tempBitmap, 0, 0); g.DrawImage(tempBitmap, 0, 0);
} }
} }
}
// 触发重绘 // 触发重绘
picBoxTemp.Invalidate(); picBoxTemp.Invalidate();
@@ -1698,11 +1748,24 @@ namespace JoyD.Windows.CS
// 获取相对于图像的当前坐标 // 获取相对于图像的当前坐标
Point currentImagePoint = ControlPointToImagePoint(e.Location); Point currentImagePoint = ControlPointToImagePoint(e.Location);
// 计算矩形参数 // 计算矩形参数:左上点是起笔方块的左上点,右下点是收笔方块的右下点
int x = Math.Min(_rectangleStartPoint.X, currentImagePoint.X); int x = _rectangleStartPoint.X;
int y = Math.Min(_rectangleStartPoint.Y, currentImagePoint.Y); int y = _rectangleStartPoint.Y;
int width = Math.Abs(currentImagePoint.X - _rectangleStartPoint.X); int width = currentImagePoint.X - _rectangleStartPoint.X;
int height = Math.Abs(currentImagePoint.Y - _rectangleStartPoint.Y); int height = currentImagePoint.Y - _rectangleStartPoint.Y;
// 确保矩形大小为正数,调整起点和大小
if (width < 0)
{
x += width;
width = -width;
}
if (height < 0)
{
y += height;
height = -height;
}
Rectangle rect = new Rectangle(x, y, width, height); Rectangle rect = new Rectangle(x, y, width, height);
// 只有当矩形有实际大小时才执行擦除 // 只有当矩形有实际大小时才执行擦除