如何在屏幕外渲染Chart(来自wpf toolkit)控件?

时间:2010-12-17 14:04:29

标签: c# wpf console controls render

我想在控制台应用程序中渲染一个Chart控件(来自WPF工具包http://wpf.codeplex.com)(我实际上从未向用户显示它),但我一直在获取一个空图像。如果我选择一些其他控件(例如TextBlock),那么一切都按预期工作。 以下是重现问题的代码:

// BEGIN

Chart chart = new Chart { Width = 300, Height = 230 };
//我还没有添加系列,因为我认为这可能会导致问题...... 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

如果我在非控制台而不是WPF应用程序中运行此代码,其中图表是表单上显示的控件,一切正常。人们提到了用于在不同线程上绘制控件的动画。我试着让我的主线程处于睡眠状态,希望其他人有时间绘制图形。这里没有运气......

任何想法?如何在屏幕外渲染此控件?

1 个答案:

答案 0 :(得分:1)

立即获得解决方案:

  1. 停用图表的所有动画
  2. UpdateLayout()Measure()
  3. 之后调用图表的方法Arrange()

    就是这样!

相关问题