我正在尝试将图表导出为PNG文件(无需在UI中渲染(,并为LiveCharts v0实现了以下代码,该代码运行良好。。。
LiveCharts.Wpf.CartesianChart control = new LiveCharts.Wpf.CartesianChart()
{
// Series = ...
Width = 700,
Height = 700,
DisableAnimations = true
};
var viewbox = new Viewbox();
viewbox.Child = control;
viewbox.Measure(control.RenderSize);
viewbox.Arrange(new Rect(new System.Windows.Point(0, 0), control.RenderSize));
control.Update(true, true); //force chart redraw
viewbox.UpdateLayout();
var encoder = new PngBitmapEncoder();
var bitmap = new RenderTargetBitmap((int)control.ActualWidth, (int)control.ActualHeight, 96, 96, PixelFormats.Pbgra32);
bitmap.Render(control);
var frame = BitmapFrame.Create(bitmap);
encoder.Frames.Add(frame);
using (Stream stm = File.Create("filename.png"))
encoder.Save(stm);
然而,当我尝试用以下代码为LiveCharts2实现这一点时,我没有看到任何图表输出为PNG。。。
CartesianChart visual = new CartesianChart() {
// Series = series,
Height = 700,
Width = 700,
EasingFunction = null
};
var viewbox2 = new Viewbox();
viewbox2.Child = visual;
viewbox2.Measure(visual.RenderSize);
viewbox2.Arrange(new Rect(new System.Windows.Point(0, 0), visual.RenderSize));
visual.CoreChart.Update(new LiveChartsCore.Kernel.ChartUpdateParams() { IsAutomaticUpdate = false, Throttling = false });
viewbox2.UpdateLayout();
var encoder2 = new PngBitmapEncoder();
RenderTargetBitmap bmp = new(700, 700, 96, 96, PixelFormats.Pbgra32);
bmp.Render(visual);
encoder2.Frames.Add(BitmapFrame.Create(bmp));
using (Stream stm = File.Create("filename.png"))
encoder2.Save(stm);
有人能帮助解决这个问题吗?提前谢谢。
在这里找到答案:https://github.com/beto-rodriguez/LiveCharts2/discussions/725#discussioncomment-4030202
var skChart = new SKCartesianChart(chartControl) { Width = 900, Height = 600, };
skChart.SaveImage("CartesianImageFromControl.png");