Files
huadongmocaceshiyi/CurveCaptureHelper.cs
2026-03-25 21:19:50 +08:00

99 lines
3.0 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace PLCDataMonitor
{
public static class CurveCaptureHelper
{
/// <summary>
/// 捕获CurvePage的曲线Canvas并转为BitmapImage
/// </summary>
public static BitmapImage CaptureCurve(Canvas curveCanvas)
{
try
{
// 创建与Canvas大小一致的渲染目标
RenderTargetBitmap renderBitmap = new RenderTargetBitmap(
(int)curveCanvas.ActualWidth,
(int)curveCanvas.ActualHeight,
96, 96,
PixelFormats.Pbgra32);
// 渲染Canvas到Bitmap
renderBitmap.Render(curveCanvas);
// 转为PNG格式图片
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
// 保存到内存流
MemoryStream ms = new MemoryStream();
encoder.Save(ms);
ms.Position = 0;
// 转为BitmapImage返回供Excel插入
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.StreamSource = ms;
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.EndInit();
bitmap.Freeze(); // 冻结图片,支持跨线程访问
return bitmap;
}
catch (Exception ex)
{
MessageBox.Show($"曲线截图失败:{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
return null;
}
}
}
public class ReportData
{
// 记录时间
public string Time { get; set; }
// 工位1压力MPa
public string Pressure1 { get; set; }
// 工位2压力MPa
public string Pressure2 { get; set; }
// 工位1最大摩擦力N
public string Friction1 { get; set; }
// 工位2最大摩擦力N
public string Friction2 { get; set; }
public float? t1 { get; set; }
public float? t2 { get; set; }
public float? D { get; set; }
// 【新增】用于存储净运行时间(秒),用于导出和绘图
public double ElapsedSeconds { get; set; }
public string Id { get; set; }
// 构造函数(用于创建数据实例)
public ReportData(string time, string pressure1, string pressure2, string friction1, string friction2, float? t1, float? t2, float? d)
{
Time = time;
Pressure1 = pressure1;
Pressure2 = pressure2;
Friction1 = friction1;
Friction2 = friction2;
this.t1 = t1;
this.t2 = t2;
D = d;
}
// 无参构造函数供XAML示例数据使用
public ReportData() { }
}
}