Chart Raster Image Export
In This Topic
NOV Chart for .NET has integrated support for raster image export that is exposed by the NChartRasterImageExporter class. It lets you export the content of the active page (or portions of it) to a raster image in one of the following file formats:
- Portable Network Graphics (PNG)
- JPEG File Interchange Format (JPEG)
- Bitmap (BMP)
- Nevron Raster Image (NRI)
Saving with Dialog
The following code shows how to display the default raster image export dialog provided by NOV:
Showing the default raster image export dialog |
Copy Code
|
NChartRasterImageExporter imageExporter = new NChartRasterImageExporter(chartView.Content);
imageExporter.ShowDialog(DisplayWindow, true);
|
The following code shows a save image file dialog that allows the user to create an image from a chart:
Saving with a save file dialog |
Copy Code
|
NChartRasterImageExporter imageExporter = new NChartRasterImageExporter(chartView.Content);
imageExporter.SaveAsImage(NImageFormat.Png); // Save to image with PNG file format selected by default
|
Saving to File or Stream
To export the chart to a raster image and save it directly to a file, without showing a save file dialog, you can use the SaveToFileAsync method. Note that the saving to file is an asynchronous (async) operation, so if you are interested in its completion, you should call the Then method of the promise it returns and handle the completion of the export in the passed delegate. Following is a code example that shows how to achieve this:
Saving to a file |
Copy Code
|
NChartRasterImageExporter imageExporter = new NChartRasterImageExporter(chartView.Content);
imageExporter.SaveToFileAsync("C:\\ExportedImage.png").Then(
delegate (NUndefined ud)
{
// Export to PNG completed
}
);
|
Exporting a chart to an arbitrary file works only on non-sandboxed platforms like WinForms, WPF, and Xamarin.Mac.
To export the chart to an image stream, you can use the SaveToStream method. Unlike the SaveToFileAsync method, the SaveToStream method runs synchronously and blocks code execution until it completes. Exporting charts to stream is useful when you wish to generate images in memory instead of saving them to the disc. The following code snippet shows how to export the active page of a chart to a stream:
Saving to a stream |
Copy Code
|
NChartRasterImageExporter imageExporter = new NChartRasterImageExporter(chartView.Content);
imageExporter.SaveToStream(stream, NImageFormat.Png);
|
Generating a Raster Image
To generate an image on the fly without saving it, use the CreateImage method:
Generating Image |
Copy Code
|
NChartRasterImageExporter imageExporter = new NChartRasterImageExporter(chartView.Content);
NImage image = imageExporter.CreateImage();
|
Copying to Clipboard
To copy the image to the clipboard, use the CopyToClipboard method:
Generating Image |
Copy Code
|
NChartRasterImageExporter imageExporter = new NChartRasterImageExporter(chartView.Content);
imageExporter.CopyToClipboard();
|
See Also