开发者

Creating Images in WPF without XAML and dispaying

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
                            {
                开发者_Go百科                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 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜