SciChart WPF 2D Charts > Advanced 2D Charting Topics > Screenshots, Printing and Export to XPS > Troubleshooting and Caveats when Exporting Chart Images
Troubleshooting and Caveats when Exporting Chart Images

How the Export Process works

The SciChartSurface uses WPFs built in mechanisms to output to Bitmap, Png, Jpeg or XPS file. Internally to SciChart there are two mechanisms for exporting to image or XPS. These are as follows:

1. Direct Render to Bitmap

This method is used when your code calls SciChartSurface.ExportToBitmapSource() or SciChartSurface.ExportToFile() specifying flag useXamlRenderSurface=false AND Size=null or unspecified.

In this method the current chart on screen is rendered to bitmap using WPF's built in export methods.

The result is a simple, clean bitmap and you see exactly what you see on screen.

2. Clone of Chart before Render to Bitmap/XPS

When you have a SciChartSurface on screen, it’s size is constrained by the parent and the WPF Layout system, so you can only export the current visual on screen. If you want to export at any other resolution, then we have to build a copy of your chart in memory (without showing it) and give it a size (Width, Height) then export it.

ScIChart does this internally for you. When property useXamlRenderSurface=true, or Size is specified, or the export type is XPS, the SciChartSurface must be cloned and recreated in memory in entirety before export.

Problems when Exporting with Clone/Copy

Cloning the SciChartSurface for an export at size (Option 2 above), or Export to XPS or export using intermediary XamlRenderSurface requires that we copy every property of the SciChartSurface into a new SciChartSurface off screen, which is then rendered to Bitmap.

This internally uses the Serialization engine of SciChart. By Serializing to XML we can deserialize the chart to recreate it with all settings and render the copied chart to bitmap.

However, this is not without its problems. Sometimes there are issues exporting a chart in-memory when the Clone/Copy is involved. Issues tend to occur when your code does one of the following:

  • Has custom types, custom series, modifiers
  • Has custom styles, templates or style bindings

It's quite common that users report problems 'when I export, the legend is not shown'. This typically happens when there is a custom template or style applied to the legend, or some other customisation. The Serialization Process is not able to pick it up and the export fails.

Possible Workarounds

1. Export without Size / without useXamlRenderSurface

The simplest possible workaround to any export problems is to export to BitmapSource, Png, Jpeg or Bitmap without specifying any size and without usng the useXamlRenderSurface flag.

Exporting a Bitmap without Clone/Copy
Copy Code
// When this flag is false, and size is null (unspecified), then the chart does not need
// to be cloned in memory and what you see on screen is what you get
bool useXamlRenderSurface = false;
Size? exportedSize = null;

// Export to BitmapSource at size
var bitmapSource = surface.ExportToBitmapSource(useXamlRenderSurface, exportedSize);
Export to Png/Jpeg/Bmp File
Copy Code
// When this flag is false, and size is null (unspecified), and ExportType is not XPS
// then the chart does not need to be cloned in memory
// and what you see on screen is what you get
bool useXamlRenderSurface = false;
Size? exportedSize = null;
ExportType exportType = ExportType.Png;

// Assumes the SciChartSurface is on screen and currently visible
// What you see is what will be exported to file
sciChartSurface.ExportToFile(string filename, exportType, useXamlRenderSurface, exportedSize);

2. Handling Serialization Errors

If you use any objects that extend SciChart types and need serialization, you must ensure that such objects are serializable, otherwise exception will be thrown during serialization process. In case of any serialization exceptions, InnerException usually contains enough details to detect the cause.

Most of such exceptions occur due to the XmlSerializer limitations. Please find the most common of them that usually cause serialization issues:

  • Objects must have public default constructor
  • Only public properties that have both getter and setter can be serialized
  • Only collections that implement IEnumerable can be serialized
  • Dictionaries cannot be serialized
  • Other limitations

Submitting Bug reports to Support

If you have a bug report to submit related to export of a chart to bitmap, XPS or printing, then package up a solution or code to reproduce the issue and send it to support [at] scichart [dot] com.

Make sure before submitting you have tested the latest version / build of SciChart as we frequently release updates with critical fixes.