This is a migrated thread and some comments may be shown as answers.

ExportToImage function throw null reference exception

1 Answer 73 Views
Chart
This is a migrated thread and some comments may be shown as answers.
Thierry
Top achievements
Rank 1
Thierry asked on 07 Feb 2011, 10:32 PM
I try to use the function ExportToImage(Stream) from a RadChart object and it always throw an null reference exception whatever the chart I try. 

Here is a sample code I try to test the function :

List<int> data = new List<int>();
Random rand = new Random(123456);
for (int i = 0; i < 50; i++)
{
    data.Add(rand.Next(10, 100));
}
 
chartTest.DefaultSeriesDefinition = new LineSeriesDefinition() { ShowItemLabels = false, ShowPointMarks = false };
chartTest.DefaultView.ChartArea.AxisX.LabelStep = 25;
chartTest.DefaultView.ChartArea.EnableAnimations = false;
chartTest.ItemsSource = data;
 
Stream ms = new MemoryStream();
chartTest.ExportToImage(ms);
/*BitmapImage src = new BitmapImage();
src.BeginInit();
src.StreamSource = ms;
src.EndInit();
 
img1.Source = src;*/


Exception throw at this line :

chartTest.ExportToImage(ms);

Exception message :

{"Object reference not set to an instance of an object."}

Exception call stack :

   at Telerik.Windows.Media.Imaging.ExportHelper.get_TransformToDevice() in c:\Builds\WPF_Scrum\Release_WPF_2010_Q2_SP1\Sources\Development\Core\Controls\ExportExtensions\ExportHelper.cs:line 50
   at Telerik.Windows.Media.Imaging.ExportHelper.get_TransformToDeviceMatrix() in c:\Builds\WPF_Scrum\Release_WPF_2010_Q2_SP1\Sources\Development\Core\Controls\ExportExtensions\ExportHelper.cs:line 43
   at Telerik.Windows.Media.Imaging.ExportHelper.get_SystemDpiX() in c:\Builds\WPF_Scrum\Release_WPF_2010_Q2_SP1\Sources\Development\Core\Controls\ExportExtensions\ExportHelper.cs:line 16
   at Telerik.Windows.Media.Imaging.ExportHelper.GetElementImage(FrameworkElement element) in c:\Builds\WPF_Scrum\Release_WPF_2010_Q2_SP1\Sources\Development\Core\Controls\ExportExtensions\ExportHelper.cs:line 62
   at Telerik.Windows.Media.Imaging.ImageExporter.Export(FrameworkElement element, Stream stream, BitmapEncoder encoder) in c:\Builds\WPF_Scrum\Release_WPF_2010_Q2_SP1\Sources\Development\Core\Controls\ExportExtensions\ImageExporter.cs:line 12
   at Telerik.Windows.Media.Imaging.ExportExtensions.ExportToImage(FrameworkElement element, Stream stream, BitmapEncoder encoder) in c:\Builds\WPF_Scrum\Release_WPF_2010_Q2_SP1\Sources\Development\Core\Controls\ExportExtensions\ExportExtensions.cs:line 100
   at Telerik.Windows.Controls.RadChart.ExportToImage(Stream stream) in c:\Builds\WPF_Scrum\Release_WPF_2010_Q2_SP1\Sources\Development\Controls\Chart\Chart\RadChart.Exporting.cs:line 168
   at TestChart.MainWindow..ctor() in C:\Dev\TestChart\TestChart\MainWindow.xaml.cs:line 41

1 Answer, 1 is accepted

Sort by
0
Missing User
answered on 10 Feb 2011, 05:45 PM
Hi Thierry,

Try calling the ExportToImage(Stream) method when all components are loaded and initialized. For example on window loaded or button click event.

public MainWindow()
{
    InitializeComponent();
 
    List<int> data = new List<int>();
    Random rand = new Random(123456);
    for (int i = 0; i < 50; i++)
    {
        data.Add(rand.Next(10, 100));
    }
 
    chartTest.DefaultSeriesDefinition = new LineSeriesDefinition() { ShowItemLabels = false, ShowPointMarks = false };
    chartTest.DefaultView.ChartArea.AxisX.LabelStep = 25;
    chartTest.DefaultView.ChartArea.EnableAnimations = false;
    chartTest.ItemsSource = data; 
             
}


private void Window_Loaded(object sender, RoutedEventArgs e)
{
    Stream memoryStream = new MemoryStream();
    chartTest.ExportToImage(memoryStream);
    BitmapImage bmpImage = new BitmapImage();
    bmpImage.BeginInit();
    bmpImage.StreamSource = memoryStream;
    bmpImage.EndInit();
    img1.Source = bmpImage;
}


Let me know whether the proposed solution helps.

Kind regards,
Polina
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
Tags
Chart
Asked by
Thierry
Top achievements
Rank 1
Answers by
Missing User
Share this question
or