Category / Section
How to export the charts in Xamarin.Android
1 min read
This article explains how to export the Syncfusion Xamarin.Android SfChart as an image.
SfChart has been saved as image and stored in the picture directory of the deployed device as shown in the following code sample.
[C#]
public void SaveAsImage(string filename)
{
// create bitmap screen capture
chart.DrawingCacheEnabled = true;
// view will save its state
chart.SaveEnabled = true;
Bitmap bitmap = null;
using (bitmap = chart.DrawingCache)
{
var extension = filename.Split('.');
// create an image path with existing environment with the provided file name without having the extension
var imagePath =
new Java.IO.File(Android.OS.Environment.ExternalStorageDirectory + "/Pictures/" + filename);
imagePath.CreateNewFile();
using (Stream fileStream = new FileStream(imagePath.AbsolutePath, System.IO.FileMode.OpenOrCreate))
{
try
{
//Compressed bitmap to get needed format
string imageExtension = extension.Length > 1 ? extension[1].Trim().ToLower() : "jpg";
switch (imageExtension)
{
case "png":
bitmap.Compress(Bitmap.CompressFormat.Png, 100, fileStream);
break;
case "jpg": case "jpeg": default:
bitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, fileStream);
break;
}
}
finally
{
//Closing stream and disabled drawing cache.
fileStream.Flush();
fileStream.Close();
chart.DrawingCacheEnabled = false;
}
}
}
}
In order to save the image, you have to enable the permission in the Android.Manifest file for device storage and also enable the storage permission in your device.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1"
android:versionName="1.0"
package="ExportChartAsImage.ExportChartAsImage">
…
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
See also
How to export chart to PDF in Xamarin.Android
Did not find the solution
Contact Support