C#-WPF-LiveChart大数据时图标绘制(曲线图)并支持图片保存
C#-WPF-LiveChart图片保存
C#-WPF-LiveChart图片保存
这个方法绝对有用!
private void ExecuteSavePicture(CartesianChart chart)
{
//背景色,chart是前台按钮绑定参数传过来的
chart.Background =System.Windows.Media.Brushes.White;
//找到lvc的父级控件
ContentControl gridData = (ContentControl)chart.Parent;
//获取控件大小
gridData.Measure(chart.RenderSize);
//二次布局更新,看是否需要
//gridData.Arrange(new System.Windows.Rect(new System.Windows.Point(0, 0), gridData.RenderSize));
chart.Update(true, true);
gridData.UpdateLayout();
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Title = “图片另存为”;
saveFileDialog.Filter = “jpg图片|.JPG|png图片|.PNG|jpeg图片|*.JPEG”;
saveFileDialog.FilterIndex = 2;
saveFileDialog.RestoreDirectory = true;//保存对话框是否记忆上次打开的目录
Nullable result = saveFileDialog.ShowDialog();//因为是WPF,相当于OK
if (result == true)
{
FileStream fs = new FileStream($“{saveFileDialog.FileName}”, FileMode.Create);//要保存的路径
//对象转换成位图
RenderTargetBitmap bmp = new RenderTargetBitmap((int)chart.ActualWidth + 10, (int)chart.ActualHeight + 10, 96, 96, PixelFormats.Pbgra32);
bmp.Render(chart);
//对象的集合编码转成图像流
BitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bmp));
//保存到路径中
encoder.Save(fs);
//释放资源
fs.Close();
fs.Dispose();
MessageBox.Show(“保存成功”);
}
}
更多推荐


所有评论(0)