C# WPF LiveCharts LineSerie 图形隐藏后 保存图片 背景为白色
要在C# WPF LiveCharts LineSerie图形隐藏后保存图片并使背景为白色,可以按照以下步骤操作:
- 创建一个新的Chart对象并设置其属性,如下所示:
var chart = new LiveCharts.Wpf.CartesianChart
{
Width = 800,
Height = 600,
Background = new SolidColorBrush(Colors.White),
Series = new SeriesCollection
{
new LineSeries
{
Title = "Series Title",
Values = new ChartValues<double> { 1, 3, 2, 4 },
LineSmoothness = 0.3,
PointGeometrySize = 10,
PointForeground = Brushes.Red,
Stroke = Brushes.Blue
}
},
LegendLocation = LegendLocation.Right,
DisableAnimations = true
};
- 将图表的系列隐藏:
chart.Series[0].Visibility = System.Windows.Visibility.Hidden;
- 创建一个RenderTargetBitmap对象,并使用Render方法将Chart对象呈现到该位图中:
var renderTargetBitmap = new RenderTargetBitmap((int)chart.Width, (int)chart.Height, 96, 96, PixelFormats.Pbgra32);
renderTargetBitmap.Render(chart);
- 创建一个PngBitmapEncoder对象,将RenderTargetBitmap对象编码为PNG格式的图像:
var pngEncoder = new PngBitmapEncoder();
pngEncoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
- 将图像保存到文件中:
using (var stream = new FileStream("output.png", FileMode.Create))
{
pngEncoder.Save(stream);
}
这将保存一个白色背景的PNG图像,其中LineSerie图形是隐藏的
原文地址: https://www.cveoy.top/t/topic/hug9 著作权归作者所有。请勿转载和采集!