实现温差图例颜色修改时同步更新温差层像素颜色功能

This commit is contained in:
zqm
2025-11-11 09:41:43 +08:00
parent ffac0db1e4
commit 0d55fed6df

View File

@@ -4,8 +4,10 @@ using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
@@ -617,14 +619,22 @@ namespace JoyD.Windows.CS
using (ColorDialog colorDialog = new ColorDialog())
{
// 设置初始颜色为当前行的颜色
colorDialog.Color = (Color)tempDiffData[e.RowIndex]["color"];
Color oldColor = (Color)tempDiffData[e.RowIndex]["color"];
colorDialog.Color = oldColor;
if (colorDialog.ShowDialog() == DialogResult.OK)
{
Color newColor = colorDialog.Color;
// 更新颜色数据
tempDiffData[e.RowIndex]["color"] = colorDialog.Color;
tempDiffData[e.RowIndex]["color"] = newColor;
// 刷新单元格以显示新颜色
dataGridViewTempDiff.Refresh();
// 更新温差层图像中所有原颜色的像素为新颜色
if (_tempDiffOverlayImage != null)
{
UpdateTempDiffOverlayPixelsColor(oldColor, newColor);
}
}
}
}
@@ -2588,6 +2598,74 @@ namespace JoyD.Windows.CS
}
}
/// <summary>
/// 更新温差层图像中指定颜色的所有像素
/// 将旧颜色的像素替换为新颜色
/// </summary>
/// <param name="oldColor">需要替换的旧颜色</param>
/// <param name="newColor">替换后的新颜色</param>
private void UpdateTempDiffOverlayPixelsColor(Color oldColor, Color newColor)
{
if (_tempDiffOverlayImage == null || !( _tempDiffOverlayImage is Bitmap))
return;
Bitmap bitmap = (Bitmap)_tempDiffOverlayImage;
// 锁定位图以提高性能
BitmapData bitmapData = bitmap.LockBits(
new Rectangle(0, 0, bitmap.Width, bitmap.Height),
ImageLockMode.ReadWrite,
PixelFormat.Format32bppArgb);
try
{
int bytesPerPixel = 4; // 32bppArgb格式每像素4字节
int byteCount = bitmapData.Stride * bitmapData.Height;
byte[] pixels = new byte[byteCount];
// 将图像数据复制到数组
Marshal.Copy(bitmapData.Scan0, pixels, 0, byteCount);
// 转换颜色为ARGB字节数组以便比较
byte oldA = oldColor.A;
byte oldR = oldColor.R;
byte oldG = oldColor.G;
byte oldB = oldColor.B;
byte newA = newColor.A;
byte newR = newColor.R;
byte newG = newColor.G;
byte newB = newColor.B;
// 遍历所有像素并替换颜色
for (int i = 0; i < byteCount; i += bytesPerPixel)
{
// ARGB格式从低位到高位是B, G, R, A
if (pixels[i] == oldB && pixels[i + 1] == oldG && pixels[i + 2] == oldR && pixels[i + 3] == oldA)
{
pixels[i] = newB; // B
pixels[i + 1] = newG; // G
pixels[i + 2] = newR; // R
pixels[i + 3] = newA; // A
}
}
// 将修改后的数据复制回位图
Marshal.Copy(pixels, 0, bitmapData.Scan0, byteCount);
}
catch (Exception ex)
{
Console.WriteLine("更新温差层像素颜色失败: " + ex.Message);
}
finally
{
// 解锁位图
bitmap.UnlockBits(bitmapData);
// 刷新图像显示
picBoxTemp.Invalidate();
}
}
/// <summary>
/// 当图像更新或控件大小变化时,重新创建叠加层图像
/// 确保矩形框正确显示在新的尺寸下