嘗試創建可寫入的位圖表單Silverlight工具箱圖表時出現問題。創建WriteableBitmap表單圖表問題
當使用文本塊,一切都很好,但試圖用圖表後,生成位圖是空的:(。
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);
所有這些代碼創建空Bitmap.But當我使用的TextBlock或一些類似的圖元橢圓,一切正常。我確信,該代碼生成的圖表是罰款,原因是生成的圖表在Silverlight控件的罰款。
編輯: 我試圖以這種方式創建位圖,但它不幫助。
chart_.InvalidateMeasure();
bitmap = new WriteableBitmap(512, 512);
bitmap.Render(chart_, null);
bitmap.Invalidate();
編輯2: 我不想圖表是在視覺樹。我只需要生成它的圖像,而不是在我的應用程序的XNA部分中使用它。
感謝您的幫助,但沒有再次成功。我嘗試使用NuGet的Charting包,但是使用這個包我沒有圖表(整個網格是白色的)。當我使用從已安裝的SL工具包引用'System.Windows.Controls.DataVisualization.Toolkit'時,Chart看起來很好,但生成的圖像是完全透明的。 你怎麼看運行的代碼是什麼時候?:) 嘗試將此代碼添加到你的,加上斷點調試級別 '的foreach(在bitmap.Pixels VAR PIX) { 如果排隊(PIX!= 0) Debug.WriteLine(「Not Zero」); }' – 2011-12-30 11:59:51