How to display a chart (from the wpf toolkit) behind the scenes?

I want to display a chart control (from WPF toolkit http://wpf.codeplex.com ) in a console application (I never show it to the user), but I continue to get an empty image. If I select some other control (for example, TextBlock), then everything will work as expected. Here is the code to reproduce the problem:

// BEGIN

Chart chart = new Chart { Width = 300, Height = 230 };
// I am not adding a series yet because I thought it might have caused problems ...
chart.Background = Brushes.Blue;
chart.BorderBrush = Brushes.Red;
chart.BorderThickness = new Thickness(10);
chart.Title = "Test chart";
chart.Measure(new Size(chart.Width, chart.Height));
chart.Arrange(new Rect(new Size(chart.Width, chart.Height)));

RenderTargetBitmap rtb = new RenderTargetBitmap((int)chart.Width, (int)chart.Height, 96, 96, PixelFormats.Pbgra32);
rtb.Render(chart);
PngBitmapEncoder png = new PngBitmapEncoder();
png.Frames.Add(BitmapFrame.Create(rtb));
using (System.IO.Stream stream = System.IO.File.Create("C:\\chart.png"))
{
png.Save(stream);
}

// END

If I run this code not in the console, but in the WPF application, where the diagram is the control shown on the form, everything works fine. People mention something about the animations used to draw the control in different streams. I tried to hide my main thread, hoping that another would then have time to draw a graph. No luck here ...

Any ideas? How can I do this control off screen?

+4
source share
1 answer

Got a solution today:

  • Deactivate all chart animations
  • call method of UpdateLayout() chart after Measure() and Arrange()

What is it!

+1
source

Source: https://habr.com/ru/post/1332505/


All Articles