tags:

views:

379

answers:

2

I am using the Silverlight WriteableBitmap command to render a 'Pie Chart' using the following code.

    Chart GetChart()
    {
        Chart newChart = new Chart() { Width = 100.0, 
                                       Height = 100.0  };

        PieSeries pieSeries = new PieSeries();
        pieSeries.SetBinding(PieSeries.ItemsSourceProperty, new Binding());
        pieSeries.DependentValueBinding = new Binding("Value");
        pieSeries.IndependentValueBinding = new Binding("Key");
        pieSeries.AnimationSequence = AnimationSequence.FirstToLast;
        pieSeries.IsSelectionEnabled = true;
        newChart.Series.Add(pieSeries);

        newChart.SetValue(Chart.DataContextProperty, new KeyValuePair<string, int>[] 
                            {
                                new KeyValuePair<string, int>("Work", 9), 
                                new KeyValuePair<string, int>("Driving", 2), 
                                new KeyValuePair<string, int>("Family", 4), 
                                new KeyValuePair<string, int>("Sleep", 8), 
                                new KeyValuePair<string, int>("Friends", 1) 
                            });
        return newChart;
    }

    WriteableBitmap bmapPreviewCanvas = new WriteableBitmap(GetChart, null);

The result I was expecting is a Bitmap with a PieChart. What I got was a Bitmap with the background without any PieChart.

Question is : What should I do to get the Pie Chart rendered in the variable 'bmapPreviewCanvas'?

Edit : Does this have something to do with the ANIMATIONSEQUENCE ?

+3  A: 

I believe your problem is that the instance of the Chart control you are trying to create a bitmap of is not on the visual tree.

I have not actually tried this myself, but I believe that you have to explicitly use the Render() method of the WritableBitmap class if the UIElement is not part of the visual tree. You will also have to call InvalidateMeasure() on the element before calling Render(), and Invalidate() on the WritableBitmap afterward to actually render the bitmap.

From the MSDN documentation:

After calling this method, you must call Invalidate in order to render the bitmap.

This method does support UIElement objects that are not part of the visual tree. You are required to call Measure and Arrange for UIElement objects that are not in the visual tree, before calling Render.

I would try the following instead:

var chart = GetChart();
chart.InvalidateMeasure();
WriteableBitmap bmapPreviewCanvas = new WriteableBitmap(chart.Width, chart.Height);
bmpaPreviewCanvas.Render(chart, null);
bmapPreviewCanvas.Invalidate();
Dan Auclair
Dan, You are correct Again. I see the Bitmap being generated if I add it to a Canvas or to the Visual Tree. Thank you.I am taking this question one step further. I am using the rendered Image in an ImageBrush and trying to fill the rectangle with that. Any Idea why the Rectangle will not display the image ?
kanchirk
I have no idea without seeing more code, but I did find a post on a possible WritableBitmap/ImageBrush bug:http://forums.silverlight.net/forums/p/110869/261935.aspx
Dan Auclair
Dan, Thanks for the Link. I also encountered the same link around the same time you posted. Seems like it is a known issue in SL3. Thank you for the help.
kanchirk
A: 

I see same problem

http://ebayvietnam.net

aladin