Telerik.Windows.Media.Imaging.ExportExtensions.ExportToImage Crash

1 Answer 219 Views
Chart ChartView
Francisco M.
Top achievements
Rank 1
Francisco M. asked on 19 Jul 2021, 05:04 PM | edited on 19 Jul 2021, 05:04 PM

When I try to generate a PNG file Exporting RadPieChart it crash without any message. The files is generate with 0KB size. I don't need the graph in the screen I just need to export the PNG to programatically insert it in a RichTexBox.

            Telerik.Windows.Controls.RadPieChart gr1 = new Telerik.Windows.Controls.RadPieChart();
            gr1.Series.Add(new Telerik.Windows.Controls.ChartView.PieSeries());
            foreach (KeyValuePair<string, int> g in dt1)
            {
                Telerik.Charting.PieDataPoint pA = new Telerik.Charting.PieDataPoint();
                pA.Label = g.Key;
                pA.Value = g.Value;
                gr1.Series[0].DataPoints.Add(pA);
            }
            string filename1 = Path.GetTempFileName();
            using (Stream fileStream = File.Open(filename1, FileMode.OpenOrCreate))
            { Telerik.Windows.Media.Imaging.ExportExtensions.ExportToImage(gr1, fileStream, new PngBitmapEncoder()); }

Any Advice will be welcome. Thanks in advance.

 

1 Answer, 1 is accepted

Sort by
0
Stenly
Telerik team
answered on 21 Jul 2021, 01:33 PM

Hello Francisco M.,

The issue here is that the element that is going to be exported is not yet in the visual tree. To tackle this, you could force the chart to render and then export it. Please refer to the following documentation regarding the matter:

Save RadCartesianChart to Image Without Adding it to View | Telerik UI for WPF 

As well as the sample project I am attaching. Please let me know if this resolves the crash and exports the chart successfully.

Regards,
Stenly
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Francisco M.
Top achievements
Rank 1
commented on 26 Jul 2021, 07:38 AM

It works! .. many thanks. It was enought by adding the following code:

var ssize = new System.Windows.Size(500, 500);
gr1.Measure(ssize);
gr1.Arrange(new Rect(ssize));
gr1.UpdateLayout();

Thanks again
Tags
Chart ChartView
Asked by
Francisco M.
Top achievements
Rank 1
Answers by
Stenly
Telerik team
Share this question
or