实现btnLoadTempRegion按钮功能:弹出打开文件对话框并从CSV读取测温区信息

This commit is contained in:
zqm
2025-11-11 14:33:41 +08:00
parent d33dbe3811
commit e3aeaaba54
2 changed files with 103 additions and 0 deletions

View File

@@ -156,6 +156,7 @@ namespace JoyD.Windows.CS
this.btnLoadTempRegion.Size = new System.Drawing.Size(23, 4);
this.btnLoadTempRegion.Text = "加载测温区";
this.btnLoadTempRegion.ToolTipText = "加载测温区";
this.btnLoadTempRegion.Click += new System.EventHandler(this.BtnLoadTempRegion_Click);
//
// btnSaveTempRegion
//

View File

@@ -2999,6 +2999,108 @@ namespace JoyD.Windows.CS
}
}
/// <summary>
/// 加载测温区按钮点击事件
/// </summary>
private void BtnLoadTempRegion_Click(object sender, EventArgs e)
{
try
{
// 弹出打开文件对话框
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "CSV文件 (*.csv)|*.csv|所有文件 (*.*)|*.*";
openFileDialog.Title = "选择测温区信息文件";
openFileDialog.DefaultExt = "csv";
// 显示打开文件对话框,如果用户点击了确定按钮
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
// 移除所有已有的测温区列表
_drawnRectangles.Clear();
_selectedRegionIndex = -1;
// 用透明色清空叠加层图像
CreateRectangleOverlayImage();
// 从CSV文件中读取所有测温区位置大小和颜色信息
using (StreamReader reader = new StreamReader(openFileDialog.FileName, Encoding.UTF8))
{
// 跳过标题行
string headerLine = reader.ReadLine();
int index = 0;
string line;
// 逐行读取数据
while ((line = reader.ReadLine()) != null)
{
try
{
// 分割CSV行数据
string[] parts = line.Split(',');
// 解析数据支持中英文标题的CSV文件
int x, y, width, height;
Color color;
if (parts.Length >= 6)
{
// 尝试解析数据(索引可能是从文件中读取的,也可能是我们自己分配的)
int.TryParse(parts[1], out x);
int.TryParse(parts[2], out y);
int.TryParse(parts[3], out width);
int.TryParse(parts[4], out height);
// 解析颜色
try
{
color = ColorTranslator.FromHtml(parts[5]);
}
catch
{
// 如果颜色解析失败,使用默认颜色
color = Color.Red;
}
// 创建新的测温区并添加到列表
RegionInfo region = new RegionInfo
{
Index = index,
ImageRectangle = new Rectangle(x, y, width, height),
Color = color
};
_drawnRectangles.Add(region);
index++;
}
}
catch (Exception ex)
{
Console.WriteLine("解析行数据失败: " + ex.Message);
// 继续处理下一行
}
}
}
// 用读取的颜色填充叠加层图像对应的区域
DrawRectanglesOnOverlay();
// 触发重绘
picBoxTemp.Invalidate();
// 更新按钮可见性
UpdateButtonsVisibility(0);
// 加载成功后提示用户
MessageBox.Show("测温区信息加载成功,共加载 " + _drawnRectangles.Count + " 个测温区", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
Console.WriteLine("加载测温区信息失败: " + ex.Message);
MessageBox.Show("加载测温区信息失败: " + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// 保存测温区按钮点击事件
/// </summary>