From e3aeaaba54180efe2aff696219543f64dc8f844a Mon Sep 17 00:00:00 2001 From: zqm Date: Tue, 11 Nov 2025 14:33:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0btnLoadTempRegion=E6=8C=89?= =?UTF-8?q?=E9=92=AE=E5=8A=9F=E8=83=BD=EF=BC=9A=E5=BC=B9=E5=87=BA=E6=89=93?= =?UTF-8?q?=E5=BC=80=E6=96=87=E4=BB=B6=E5=AF=B9=E8=AF=9D=E6=A1=86=E5=B9=B6?= =?UTF-8?q?=E4=BB=8ECSV=E8=AF=BB=E5=8F=96=E6=B5=8B=E6=B8=A9=E5=8C=BA?= =?UTF-8?q?=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Toprie/Toprie/Setting.Designer.cs | 1 + .../CS/Framework4.0/Toprie/Toprie/Setting.cs | 102 ++++++++++++++++++ 2 files changed, 103 insertions(+) diff --git a/Windows/CS/Framework4.0/Toprie/Toprie/Setting.Designer.cs b/Windows/CS/Framework4.0/Toprie/Toprie/Setting.Designer.cs index afa77cb..df23ef0 100644 --- a/Windows/CS/Framework4.0/Toprie/Toprie/Setting.Designer.cs +++ b/Windows/CS/Framework4.0/Toprie/Toprie/Setting.Designer.cs @@ -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 // diff --git a/Windows/CS/Framework4.0/Toprie/Toprie/Setting.cs b/Windows/CS/Framework4.0/Toprie/Toprie/Setting.cs index b9f4430..b91a162 100644 --- a/Windows/CS/Framework4.0/Toprie/Toprie/Setting.cs +++ b/Windows/CS/Framework4.0/Toprie/Toprie/Setting.cs @@ -2999,6 +2999,108 @@ namespace JoyD.Windows.CS } } + /// + /// 加载测温区按钮点击事件 + /// + 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); + } + } + /// /// 保存测温区按钮点击事件 ///