【发布时间】:2010-09-07 09:21:27
【问题描述】:
我正在尝试在 c# 代码中创建一个 WPF 图表并将其保存到文件而不显示在屏幕上。这将在 WCF 服务中,其中数据被发送到服务,创建图像并返回图像的路径。
到目前为止,我已经将图像保存到文件中,并且显示了 X 和 Y 轴上的数据,但未绘制图表上的列。
有谁知道为什么图表上的列没有绘制...这是我的代码:
公共无效 DoWork() { var newThread = new Thread(CreateImage); newThread.SetApartmentState(ApartmentState.STA); newThread.Start(); }
public void CreateImage()
{
var grid = new Grid {Width = 800, Height = 600, Background = new SolidColorBrush(Colors.LightBlue)};
var chart = new Chart();
grid.Children.Add(chart);
var renderTarget = new RenderTargetBitmap((int)grid.Width, (int)grid.Height, 96d, 96d, PixelFormats.Default);
var barSeries = new BarSeries
{
Title = "Fruit in Cupboard",
ItemsSource = new[]
{
new KeyValuePair<string, int>("Oranges", 18),
new KeyValuePair<string, int>("Apples", 15),
new KeyValuePair<string, int>("Melons", 2),
new KeyValuePair<string, int>("Pineapples", 4),
new KeyValuePair<string, int>("Plums", 25)
},
IndependentValueBinding = new Binding("Key"),
DependentValueBinding = new Binding("Value")
};
chart.Series.Add(barSeries);
grid.Measure(new Size(grid.Width, grid.Height));
grid.Arrange(new Rect(new Size(grid.Width, grid.Height)));
grid.UpdateLayout();
renderTarget.Render(grid);
var png = new PngBitmapEncoder();
png.Frames.Add(BitmapFrame.Create(renderTarget));
using (Stream stm = File.Create(string.Format(@"C:/Images/{0}.png", Guid.NewGuid())))
{
png.Save(stm);
}
}
【问题讨论】:
-
我认为这与以下事实有关动画发生前图表的快照。
标签: wpf charts wpftoolkit