修复温差图例颜色显示问题并实现相关功能:1. 移除温差图例加载时错误的单元格颜色赋值 2. 添加btnNewTempDiff功能实现 3. 修改BtnLoadTempDiff_Click要求温度值精确匹配

This commit is contained in:
zqm
2025-11-12 09:35:47 +08:00
parent 958b2c974b
commit c37f782f17
3 changed files with 208 additions and 0 deletions

View File

@@ -179,6 +179,11 @@
1. 用透明色清空温差层图像 1. 用透明色清空温差层图像
2. 移除所有已有的温差图例列表 2. 移除所有已有的温差图例列表
### btnLoadTempDiff加载温差图 ### btnLoadTempDiff加载温差图
1. 弹出用户打开文件对话框用户选择要加载的csv文件
2. 用透明色清空温差层图像
3. 移除所有已有的温差图例列表
4. 从csv文件中读取所有温差图例信息添加到温差图例列表中
5. 从csv文件中读取所有像素的温度值绘制温差层图像对应的像素
### btnSaveTempDiff保存温差图 ### btnSaveTempDiff保存温差图
1. 弹出用户保存文件对话框,用户选择保存路径和文件名 1. 弹出用户保存文件对话框,用户选择保存路径和文件名
2. 保存温差图例列表中的所有温差图例信息到csv文件 2. 保存温差图例列表中的所有温差图例信息到csv文件

View File

@@ -361,6 +361,7 @@ namespace JoyD.Windows.CS
this.btnNewTempDiff.Size = new System.Drawing.Size(23, 4); this.btnNewTempDiff.Size = new System.Drawing.Size(23, 4);
this.btnNewTempDiff.Text = "新建温差图"; this.btnNewTempDiff.Text = "新建温差图";
this.btnNewTempDiff.ToolTipText = "新建温差图"; this.btnNewTempDiff.ToolTipText = "新建温差图";
this.btnNewTempDiff.Click += new System.EventHandler(this.BtnNewTempDiff_Click);
// //
// btnLoadTempDiff // btnLoadTempDiff
// //
@@ -370,6 +371,7 @@ namespace JoyD.Windows.CS
this.btnLoadTempDiff.Size = new System.Drawing.Size(23, 4); this.btnLoadTempDiff.Size = new System.Drawing.Size(23, 4);
this.btnLoadTempDiff.Text = "加载温差图"; this.btnLoadTempDiff.Text = "加载温差图";
this.btnLoadTempDiff.ToolTipText = "加载温差图"; this.btnLoadTempDiff.ToolTipText = "加载温差图";
this.btnLoadTempDiff.Click += new System.EventHandler(this.BtnLoadTempDiff_Click);
// //
// btnSaveTempDiff // btnSaveTempDiff
// //

View File

@@ -4173,6 +4173,207 @@ namespace JoyD.Windows.CS
/// <summary> /// <summary>
/// 保存温差图例按钮点击事件处理程序 /// 保存温差图例按钮点击事件处理程序
/// </summary> /// </summary>
/// <summary>
/// 新建温差图按钮点击事件处理程序
/// </summary>
private void BtnNewTempDiff_Click(object sender, EventArgs e)
{
try
{
// 用透明色清空温差层图像
if (_tempDiffOverlayImage != null)
{
_tempDiffOverlayImage.Dispose();
_tempDiffOverlayImage = null;
}
// 确保温差层图像已初始化
if (picBoxTemp.Image != null)
{
InitializeTempDiffOverlayImage();
}
// 移除所有已有的温差图例列表
tempDiffData.Clear();
dataGridViewTempDiff.Rows.Clear();
// 更新显示
picBoxTemp.Invalidate();
MessageBox.Show("已成功创建新的温差图!", "操作成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
Console.WriteLine($"新建温差图时发生错误: {ex.Message}");
MessageBox.Show($"新建失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// 加载温差图按钮点击事件处理程序
/// </summary>
private void BtnLoadTempDiff_Click(object sender, EventArgs e)
{
try
{
// 弹出用户打开文件对话框用户选择要加载的csv文件
OpenFileDialog openFileDialog = new OpenFileDialog
{
Filter = "CSV文件 (*.csv)|*.csv|所有文件 (*.*)|*.*",
Title = "加载温差图例和温度数据",
FileName = "温差数据.csv"
};
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
// 用透明色清空温差层图像
if (_tempDiffOverlayImage != null)
{
_tempDiffOverlayImage.Dispose();
_tempDiffOverlayImage = null;
}
// 确保温差层图像已初始化
if (picBoxTemp.Image != null)
{
InitializeTempDiffOverlayImage();
}
// 移除所有已有的温差图例列表
tempDiffData.Clear();
dataGridViewTempDiff.Rows.Clear();
bool readingLegend = false;
bool readingPixelData = false;
// 使用温度值作为键,颜色作为值的映射表,与保存时保持一致
Dictionary<double, Color> tempToColorMap = new Dictionary<double, Color>();
// 读取CSV文件
using (StreamReader reader = new StreamReader(openFileDialog.FileName, Encoding.UTF8))
{
string line;
while ((line = reader.ReadLine()) != null)
{
line = line.Trim();
if (string.IsNullOrEmpty(line))
continue;
// 识别温差图例信息部分
if (line == "温差图例信息")
{
readingLegend = true;
readingPixelData = false;
reader.ReadLine(); // 跳过表头行
continue;
}
// 识别像素温度数据部分
if (line == "像素温度数据")
{
readingLegend = false;
readingPixelData = true;
reader.ReadLine(); // 跳过表头行
continue;
}
// 处理温差图例信息
if (readingLegend && _tempDiffOverlayImage is Bitmap)
{
string[] parts = line.Split(',');
if (parts.Length == 2)
{
try
{
double temperature = Convert.ToDouble(parts[0]);
string colorHex = parts[1].TrimStart('#');
Color color = Color.FromArgb(
Convert.ToInt32(colorHex.Substring(0, 2), 16),
Convert.ToInt32(colorHex.Substring(2, 2), 16),
Convert.ToInt32(colorHex.Substring(4, 2), 16)
);
// 添加到温差图例列表
int rowIndex = dataGridViewTempDiff.Rows.Add();
dataGridViewTempDiff.Rows[rowIndex].Cells["tempDiffValue"].Value = $"{temperature:F1}°C";
// 保存到tempDiffData列表和映射表
Dictionary<string, object> item = new Dictionary<string, object>
{
{ "tempDiffValue", $"{temperature:F1}°C" },
{ "color", color }
};
tempDiffData.Add(item);
tempToColorMap[temperature] = color;
}
catch (Exception ex)
{
Console.WriteLine($"解析温差图例失败: {ex.Message}");
}
}
}
// 处理像素温度数据
if (readingPixelData && _tempDiffOverlayImage is Bitmap)
{
string[] parts = line.Split(',');
if (parts.Length == 3 && parts[0] != "无温差图像数据")
{
try
{
int x = Convert.ToInt32(parts[0]);
int y = Convert.ToInt32(parts[1]);
double temperature = Convert.ToDouble(parts[2]);
// 找到对应的颜色 - 必须精确匹配
Color pixelColor = Color.Transparent;
// 尝试精确匹配
if (tempToColorMap.TryGetValue(Math.Round(temperature, 1), out Color exactColor))
{
pixelColor = exactColor;
}
else
{
// 如果没有精确匹配,显示错误并中止加载
MessageBox.Show($"温度值 {temperature:F1}°C 在温差图例中找不到精确匹配,请检查温差图例设置!", "加载失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (pixelColor != Color.Transparent)
{
// 绘制温差层图像对应的像素
// 注意由于保存时是每隔10个像素采样加载时需要填充这10x10的区域
using (Graphics g = Graphics.FromImage(_tempDiffOverlayImage))
{
using (SolidBrush brush = new SolidBrush(pixelColor))
{
// 填充10x10的区域
g.FillRectangle(brush, x, y, 10, 10);
}
}
}
}
catch (Exception ex)
{
Console.WriteLine($"解析像素数据失败: {ex.Message}");
}
}
}
}
}
// 更新显示
picBoxTemp.Invalidate();
MessageBox.Show("温差图已成功加载!", "加载成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
Console.WriteLine($"加载温差数据时发生错误: {ex.Message}");
MessageBox.Show($"加载失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void BtnSaveTempDiff_Click(object sender, EventArgs e) private void BtnSaveTempDiff_Click(object sender, EventArgs e)
{ {
try try