We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to export the charts in Xamarin.Android

Platform: Xamarin.Android |
Control: SfChart |
Published Date: September 17, 2020 |
Last Revised Date: September 24, 2020

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>

 

 

View the sample in GitHub.

 

See also

 

How to export chart to PDF in Xamarin.Android

 

What are the types of SfChart

 

 

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile