- Export as an image
- Get the stream of Chart
Contact Support
Exporting in .NET MAUI Chart
10 Jan 20252 minutes to read
Export as an image
You can export the chart view as an image in the desired file format using the SaveAsImage method of SfCartesianChart. The supported image formats are JPEG and PNG. By default, i.e., if you didn’t mention any image format with the filename, the chart view will be exported as an image in the PNG format.
NOTE
The chart view can be exported as an image only when the chart view is added to the visual tree.
The following code snippet demonstrates the usage of this method:
// Create a new instance of SfCartesianChart
SfCartesianChart chart = new SfCartesianChart();
...
this.Content = chart;
// Dynamically save the chart as an image file named "ChartSample.jpeg"
chart.SaveAsImage("ChartSample.jpeg");
TIPS
We can change the image formats in above code by changing its extension as .jpg, .png.
The exported image will be saved in the different location across the platforms.
Windows Phone, Android and MAC – The image will be saved inside the ‘Pictures’ directory of the file system.
iOS – The image will be saved inside the ‘Photos/Album’ directory of the file system.
To save the image on Android and Windows Phone devices, you must enable file writing permissions on the device storage.
To save the image in the photo album on iOS devices, you must enable permission to access the device storage in the “Info” file.
Add the following code snippet to the “Info” file:
<dict>
...
<key>NSPhotoLibraryUsageDescription</key>
<string>This App needs permission to access the Photos</string>
<key>NSPhotoLibraryAddUsageDescription</key>
<string>This App needs permission to access the Photos</string>
...
</dict>
Get the stream of Chart
The GetStreamAsync method of SfCartesianChart is used to asynchronously get the chart view as a stream in the desired ImageFileFormat. The output stream can be passed as an input to other components that accept streams, such as PDF, Excel, and Word. The supported image file formats are JPEG and PNG.
NOTE
The charts stream can only be rendered when the chart view is added to the visual tree.
The following code snippet demonstrates the usage of this method:
// Create a new instance of SfCartesianChart
SfCartesianChart chart = new SfCartesianChart();
...
this.Content = chart;
// Export the chart as a JPEG image stream asynchronously
await chart.GetStreamAsync(ImageFileFormat.Jpeg);