Export SfChart as an image in Xamarin.Android

19 Oct 20204 minutes to read

The SfChart can be converted as an image and saved in the desired directory by creating Bitmap from drawing cache and save it using the FileStream.

The code sample demonstrated as follows.

  • c#
  • public void SaveAsImage(string filename) 
    {       
        //Enabled drawing cache
        chart.DrawingCacheEnabled = true; 
        
        chart.SaveEnabled = true; 
        Bitmap bitmap = null; 
        //Create bitmap from chart drawing cache.
        using (bitmap = chart.DrawingCache) 
        { 
            var extension = filename.Split('.'); 
            //Create an image path with existing environment with the provided file name.
            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 
                { 
                    string imageExtension = extension.Length > 1 ? extension[1].Trim().ToLower() : "jpg"; 
    
                    //Compressed bitmap to get needed format.
                    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; 
                } 
            } 
        } 
    }

    To save the image in Android, must grant device storage permission.

  • xaml
  • <manifest . . . 
              package="ExportChartAsImage.ExportChartAsImage">
    
    . . . 
    
    	<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    </manifest>

    See also

    How to export chart as an image in Xamarin.Android

    How to export chart to PDF in Xamarin.Android