自动加载测温区和温差图
This commit is contained in:
@@ -3457,6 +3457,51 @@ namespace JoyD.Windows.CS
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 自动保存测温区配置到默认路径
|
||||
/// </summary>
|
||||
private void SaveTempRegionToDefaultPath()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_drawnRectangles.Count == 0)
|
||||
return;
|
||||
|
||||
// 构建默认保存路径
|
||||
string configDir = Path.Combine(_projectPath, "Config");
|
||||
string configPath = Path.Combine(configDir, "测温区信息.csv");
|
||||
|
||||
// 创建Config目录(如果不存在)
|
||||
if (!Directory.Exists(configDir))
|
||||
{
|
||||
Directory.CreateDirectory(configDir);
|
||||
}
|
||||
|
||||
// 使用StreamWriter保存为CSV文件
|
||||
using (StreamWriter writer = new StreamWriter(configPath, false, Encoding.UTF8))
|
||||
{
|
||||
// 写入CSV文件头部(使用中文标题)
|
||||
writer.WriteLine("索引,X坐标,Y坐标,宽度,高度,颜色");
|
||||
|
||||
// 遍历所有测温区,将信息写入CSV文件
|
||||
foreach (RegionInfo region in _drawnRectangles)
|
||||
{
|
||||
// 获取颜色的十六进制表示
|
||||
string colorHex = ColorTranslator.ToHtml(region.Color);
|
||||
|
||||
// 写入一行数据,格式:索引,X坐标,Y坐标,宽度,高度,颜色
|
||||
writer.WriteLine($"{region.Index},{region.ImageRectangle.X},{region.ImageRectangle.Y},{region.ImageRectangle.Width},{region.ImageRectangle.Height},{colorHex}");
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine("测温区信息已自动保存到默认路径: " + configPath);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("自动保存测温区信息失败: " + ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 新建测温区按钮点击事件
|
||||
/// </summary>
|
||||
@@ -3523,6 +3568,15 @@ namespace JoyD.Windows.CS
|
||||
{
|
||||
_timer.Stop();
|
||||
|
||||
// 当自动配置为true时,保存回加载时的文件
|
||||
if (_autoConfig && !string.IsNullOrEmpty(_projectPath))
|
||||
{
|
||||
// 自动保存测温区配置
|
||||
SaveTempRegionToDefaultPath();
|
||||
// 自动保存温差图配置
|
||||
SaveTempDiffToDefaultPath();
|
||||
}
|
||||
|
||||
// 释放叠加层图像资源
|
||||
if (_rectangleOverlayImage != null)
|
||||
{
|
||||
@@ -4646,6 +4700,115 @@ namespace JoyD.Windows.CS
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 自动保存温差图配置到默认路径
|
||||
/// </summary>
|
||||
private void SaveTempDiffToDefaultPath()
|
||||
{
|
||||
try
|
||||
{
|
||||
// 构建默认保存路径
|
||||
string configDir = Path.Combine(_projectPath, "Config");
|
||||
string configPath = Path.Combine(configDir, "温差数据.csv");
|
||||
|
||||
// 创建Config目录(如果不存在)
|
||||
if (!Directory.Exists(configDir))
|
||||
{
|
||||
Directory.CreateDirectory(configDir);
|
||||
}
|
||||
|
||||
using (StreamWriter writer = new StreamWriter(configPath, false, Encoding.UTF8))
|
||||
{
|
||||
// 写入温差图例信息
|
||||
writer.WriteLine("温差图例信息");
|
||||
writer.WriteLine("温度(°C),颜色");
|
||||
|
||||
// 保存温差图例列表中的所有温差图例信息
|
||||
foreach (var item in tempDiffData)
|
||||
{
|
||||
// 获取温差值并去除°C符号进行转换
|
||||
string tempString = item["tempDiffValue"].ToString().Replace("°C", "").Trim();
|
||||
double temperature = Convert.ToDouble(tempString);
|
||||
Color color = (Color)item["color"];
|
||||
string colorHex = $"#{color.R:X2}{color.G:X2}{color.B:X2}";
|
||||
writer.WriteLine($"{temperature:F1},{colorHex}");
|
||||
}
|
||||
|
||||
// 写入分隔行
|
||||
writer.WriteLine();
|
||||
writer.WriteLine("像素温度数据");
|
||||
writer.WriteLine("X坐标,Y坐标,温度值(°C)");
|
||||
|
||||
// 从_tempDiffOverlayImage中获取真实温度数据
|
||||
if (_tempDiffOverlayImage != null && _tempDiffOverlayImage is Bitmap)
|
||||
{
|
||||
Bitmap bitmap = (Bitmap)_tempDiffOverlayImage;
|
||||
|
||||
// 锁定位图以提高性能
|
||||
BitmapData bitmapData = bitmap.LockBits(
|
||||
new Rectangle(0, 0, bitmap.Width, bitmap.Height),
|
||||
ImageLockMode.ReadOnly,
|
||||
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);
|
||||
|
||||
// 保存所有非透明像素
|
||||
for (int y = 0; y < bitmap.Height; y++)
|
||||
{
|
||||
for (int x = 0; x < bitmap.Width; x++)
|
||||
{
|
||||
// 计算当前像素在数组中的位置
|
||||
int pixelIndex = y * bitmapData.Stride + x * bytesPerPixel;
|
||||
|
||||
// 获取像素颜色
|
||||
byte b = pixels[pixelIndex];
|
||||
byte g = pixels[pixelIndex + 1];
|
||||
byte r = pixels[pixelIndex + 2];
|
||||
byte a = pixels[pixelIndex + 3];
|
||||
|
||||
// 只处理非透明的像素
|
||||
if (a > 0)
|
||||
{
|
||||
Color pixelColor = Color.FromArgb(a, r, g, b);
|
||||
double? temperature = GetTemperatureByColor(pixelColor);
|
||||
|
||||
// 如果找到对应的温度值,则写入文件
|
||||
if (temperature.HasValue)
|
||||
{
|
||||
writer.WriteLine($"{x},{y},{temperature.Value:F1}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
// 解锁位图
|
||||
bitmap.UnlockBits(bitmapData);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// 如果没有温差图像数据,只保存图例信息
|
||||
writer.WriteLine("无温差图像数据");
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine("温差数据已自动保存到默认路径: " + configPath);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("自动保存温差数据失败: " + ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步加载测温区配置
|
||||
/// </summary>
|
||||
@@ -4732,24 +4895,12 @@ namespace JoyD.Windows.CS
|
||||
_selectedRegionIndex = -1;
|
||||
txtRegionNumber.Text = "";
|
||||
|
||||
// 创建新的叠加层图像
|
||||
CreateRectangleOverlayImage();
|
||||
|
||||
// 添加加载的区域
|
||||
_drawnRectangles.AddRange(regions);
|
||||
_regionCounter = regions.Count;
|
||||
|
||||
// 绘制加载的区域到叠加层图像
|
||||
using (Graphics g = Graphics.FromImage(_rectangleOverlayImage))
|
||||
{
|
||||
foreach (var region in regions)
|
||||
{
|
||||
using (Pen pen = new Pen(region.Color, 2))
|
||||
{
|
||||
g.DrawRectangle(pen, region.ImageRectangle);
|
||||
}
|
||||
}
|
||||
}
|
||||
// 创建新的叠加层图像(会绘制所有区域和编号)
|
||||
CreateRectangleOverlayImage();
|
||||
|
||||
// 刷新图像显示
|
||||
picBoxTemp.Invalidate();
|
||||
|
||||
Reference in New Issue
Block a user