在 WPF 中创建图像而不使用 XAML 并显示
我正在尝试在 C# 代码中创建 WPF 图表并将其保存到文件而不在屏幕上显示。这将位于 WCF 服务中,其中数据被发送到服务,创建图像并返回图像的路径。
到目前为止,我已经将图像保存到文件中以及 X & 上的数据。显示 Y 轴,但未绘制图表上的列。
有谁知道为什么图表上的列没有绘制...这是我的代码:
public void DoWork() { var newThread = 新线程(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);
}
}
I am trying to create a WPF Chart in c# code and saving it to file without displaying it on screen. This will be in a WCF Service where data is sent to the service, an image is created and and path to the image is returned.
So far I have got the image to save to file and the data on the X & Y axis is displayed but the columns on the graph are not drawn.
Does anyone know why the columns on the chart are not drawing... here is my code:
public void 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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在使用 Telerik 的 RadChart 时就遇到了这个问题。解释正如您在上面的评论中所写的那样 - 该系列正在动画化,快照是在它们可见之前拍摄的。
我的解决方案是在渲染之前将 ChartArea.EnableAnimations 属性的值设置为 false。我不熟悉 WPF Toolkit Chart,但可能有一个类似的设置可用于禁用动画。
请注意,Telerik 的 BarSeries 还存在一个相关问题 - 有必要将条形的不透明度设置为 1。请参阅 Telerik 文档 了解详细信息。
I had exactly this problem with Telerik's RadChart. The explanation was as you wrote in your comment above - the series was being animated in and the snapshot was being taken before they were visible.
My solution was to set the value of the
ChartArea.EnableAnimations
property to false before rendering it. I am not familiar with the WPF Toolkit Chart but it is likely that there is a similar setting that you can use to disable the animations.Note that there is a further, related issue with Telerik's BarSeries - it is necessary to set the opacity of the bars to 1. See the Telerik documentation for details.