尝试从 Silverlight 工具包 Graph 创建可写位图时遇到问题。
使用 textBlock 时,一切正常,但尝试使用 Chart 后,生成的位图为空 :( 。
var data = new List<Point>(100);
for (int i = 0; i < 100; i++)
{
data.Add(new Point(i, Math.Sin(i * Math.PI / 50)));
}
Chart chart_ = new Chart()
{
Name = "Chart",
Width = 512,
Height = 512
};
LineSeries line = new LineSeries()
{
Name = "Line",
Title = "test",
IndependentValuePath = "X",
DependentValuePath = "Y",
ItemsSource = data
};
chart_.Series.Add(line);
此代码在其中创建带有正弦曲线的图表。然后我试图从中创建位图。
LayoutRoot.Children.Add(chart_); // I tried to add chart_ to visual tree, It doesn't help
//creates bitmap
ScaleTransform t = new ScaleTransform() { ScaleX = 1.0, ScaleY = 1.0 };
//bitmap = new WriteableBitmap(chart_, t); Tried it also with this way
bitmap = new WriteableBitmap(512, 512);
bitmap.Render(chart_, t);
texture = new Texture2D(GraphicsDeviceManager.Current.GraphicsDevice, bitmap.PixelWidth, bitmap.PixelHeight, false, SurfaceFormat.Color);
bitmap.CopyTo(texture);
所有这些代码都会创建空位图。但是当我使用 TextBlock 或 Ellipse 等一些原语时,一切正常。我确定,代码生成图表很好,因为在 Silverlight 控件中生成图表很好。
编辑: 我试图以这种方式创建位图,但它没有帮助。
chart_.InvalidateMeasure();
bitmap = new WriteableBitmap(512, 512);
bitmap.Render(chart_, null);
bitmap.Invalidate();
编辑 2: 我不希望图形出现在可视树中。我只需要生成它的图像,然后在我的应用程序的 XNA 部分中使用它。