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

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

View File

@@ -1267,33 +1267,83 @@ 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);
// 创建临时位图用于双重缓冲,避免闪烁 // 对于擦除模式,在临时层上绘制预览框,不影响实际的温差图
using (Bitmap tempBitmap = new Bitmap(_tempDiffOverlayImage)) if (_isEraseMode)
{ {
using (Graphics g = Graphics.FromImage(tempBitmap)) // 创建临时缓冲区用于绘制预览
{ Bitmap bufferBitmap = null;
g.SmoothingMode = SmoothingMode.AntiAlias; try
g.InterpolationMode = InterpolationMode.HighQualityBicubic; {
bufferBitmap = new Bitmap(picBoxTemp.Width, picBoxTemp.Height);
if (_isEraseMode) using (Graphics g = Graphics.FromImage(bufferBitmap))
{ {
// 擦除模式显示虚线预览框不实际擦除等待MouseUp时再擦除 // 绘制背景图像
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)) using (Pen dashPen = new Pen(Color.White, 1))
{ {
dashPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash; dashPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
g.DrawRectangle(dashPen, rect); // 将图像坐标转换为控件坐标
Rectangle controlRect = ImageRectangleToControlRectangle(rect);
g.DrawRectangle(dashPen, controlRect);
} }
} }
else
{ // 直接在控件上绘制缓冲区内容
// 普通绘制模式:使用半透明填充和边框 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 (Graphics g = Graphics.FromImage(tempBitmap))
{
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
// 创建半透明的填充颜色 // 创建半透明的填充颜色
Color fillColor = Color.FromArgb(128, selectedColor); Color fillColor = Color.FromArgb(128, selectedColor);
@@ -1302,16 +1352,16 @@ namespace JoyD.Windows.CS
// 绘制矩形边框 // 绘制矩形边框
using (Pen pen = new Pen(selectedColor, _currentBrushSize)) using (Pen pen = new Pen(selectedColor, _currentBrushSize))
{ {
g.DrawRectangle(pen, rect); g.DrawRectangle(pen, rect);
} }
} }
}
// 将临时位图复制回温差层图像
// 将临时位图复制回温差层图像 using (Graphics g = Graphics.FromImage(_tempDiffOverlayImage))
using (Graphics g = Graphics.FromImage(_tempDiffOverlayImage)) {
{ g.DrawImage(tempBitmap, 0, 0);
g.DrawImage(tempBitmap, 0, 0); }
} }
} }
@@ -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);
// 只有当矩形有实际大小时才执行擦除 // 只有当矩形有实际大小时才执行擦除