correct usage of method ChartPlotter.CreateScreenshot()

Aug 6, 2010 at 3:04 PM
Edited Aug 6, 2010 at 3:28 PM

I have a question conc. correct usage of method ChartPlotter.CreateScreenshot() of library DynamicDataDisplay
========================================================================

I need a screenhot of my chart, which is stored to a bitmap in order to integrate the retrieved picture into a document.
The whole procedure should be executed via batch job.

I tried the following source code (see below), the chart will be displayed and works fine,
but when I try to execute 'tryChart.CreateScreenshot()' in order to save the results I will get the following exception :
    System.ArgumentOutOfRangeException        the param value must not be NULL.
    current value  of 'pixelWidth'                       = null

Without this statement the window will be displayed and contains the chart, how can pixelWidth be null in this case ?
What should be added here ?

When I substitute the main part of the source code by XAML-statements I have the same success and also the same problem.

Without the statement 'tryChart.CreateScreenshot()' everything works fine.
Finally I get the same error when the statement 'tryChart.CreateScreenshot()' will be executed within BuildGraph().


.. my environment
-----------------------------------------------------
    C#
    Visual Studio 2010
    .NET 4.0

DynamicDataDisplay.dll
    run time version     v2.0.50727
    version                  0.3.0.0
    date                      2009-apr-27


.. and now my source code :
-----------------------------------------------------
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;

using System.IO;

//  needed for DDD-feature, functionality encapsulated in 'DynamicDataDisplay.dll'
using Microsoft.Research.DynamicDataDisplay;
using Microsoft.Research.DynamicDataDisplay.DataSources;
//


namespace HelloWorldManual
{
    public class MyWindow : Window
    {

        public MyWindow()
        {
            //  .. set the size of the main window here ..
            Width = 525;
            Height = 350;

            //  make a grid area, where a DDD-chart will be added to
            Grid grid = new Grid();
            //  .. and add the grid area to the main window
            Content = grid;

            //  ---------------------------
            //  make a chart    - this source code is normally written to XAML-statements
            ChartPlotter    tryChart;
            tryChart = new ChartPlotter();
           
            tryChart.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
            tryChart.VerticalAlignment = System.Windows.VerticalAlignment.Center;

            //  .. add the chart to the grid
            grid.Children.Add(tryChart);

            //  ---------------------------
            //  .. and display the values now within the chart
            BuildGraph(tryChart);

            //  .. keep the resulting chart in mind as a bitmap
            BitmapSource bitmap;
            bitmap = tryChart.CreateScreenshot();
        }


        /// <summary>
        /// @see    build and display the graph
        /// </summary>
        void BuildGraph(ChartPlotter tryChart)
        {
            Console.WriteLine("starting BuildGraph()");

            // Prepare data in arrays
            int iN = 10;
            double[] x = new double[iN];
            double[] y = new double[iN];

            for (int iAux = 0; iAux < iN; iAux++)
            {
                x[iAux] = (double)iAux;
                y[iAux] = (double)iAux;
            }


            // Create data sources:
            var xDataSource = x.AsXDataSource();
            var yDataSource = y.AsYDataSource();

            CompositeDataSource compositeDataSource = new CompositeDataSource(xDataSource, yDataSource);

            // adding graph to plotter
            tryChart.AddLineGraph(compositeDataSource, Colors.CadetBlue, 3, ".. DDD trial using No XAML ..");

            tryChart.Visibility = Visibility.Visible;

            // force evertyhing plotted to be visible
            tryChart.FitToView();
        }

        /// <summary>
        /// @see    http://msdn.microsoft.com/en-us/library/system.windows.application.aspx
        /// </summary>
        [STAThread]
        public static void Main()
        {
            Application app = new Application();

            app.Run(new MyWindow());
        }
    }
}
-----------------------------------------------------

 

Thanks in advance,

cstrial

Aug 8, 2010 at 10:14 PM

It could be a timing issue.  You could try doing the screen capture after the window has loaded.

			Loaded += new RoutedEventHandler(MyWindow_Loaded);
		}

		private void MyWindow_Loaded(object sender, RoutedEventArgs e)
		{			
	            //  .. keep the resulting chart in mind as a bitmap
        	    BitmapSource bitmap;
	            bitmap = tryChart.CreateScreenshot(); 
}

Grant.

Aug 9, 2010 at 9:16 AM

Thanks a lot, Dear grantnz !


Indeed, it was a timing issue (at least when executing within VisualStudio 2010 dev env) !

As proposed I made the following changes .. and : its working now :

            //  ---------------------------
            //  .. and display the values now within the chart
            BuildGraph(_tryChart);

            //  .. keep the chart in mind as a bitmap
            Loaded += new RoutedEventHandler(MyWindow_Loaded);
        }   //  MyWindow()


        /// <summary>
        /// @see    introduced due to solving timing problems as mentioned within
        /// @see    http://dynamicdatadisplay.codeplex.com/Thread/View.aspx?ThreadId=222724
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MyWindow_Loaded(object sender, RoutedEventArgs e)
        {           
                //  .. keep the resulting chart in mind as a bitmap
                BitmapSource bitmap;
                bitmap = _tryChart.CreateScreenshot();
        }

        /// <summary>
        /// @see    build and display the graph
        /// </summary>
        void BuildGraph( ...