write text on an image xamarin android

hossein tavakoli 471 Reputation points

Hi dears,
I want to write text on an image then save it, but my code has runtime error.

error : System.PlatformNotSupportedException: 'Operation is not supported on this platform.'

using System.Drawing;

public void Watermark(string picturePath, string text)

            PointF firstLocation = new PointF(10f, 10f);
            PointF secondLocation = new PointF(10f, 50f);

            Bitmap bitmap = (Bitmap)Image.FromFile(picturePath);//load the image file

            using (Graphics graphics = Graphics.FromImage(bitmap))
                using (Font arialFont = new Font("Arial", 10))
                    graphics.DrawString(text, arialFont, Brushes.Blue, firstLocation);

A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,323 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 71,836 Reputation points Microsoft Vendor


    Welcome to our Microsoft Q&A platform!

    I used the following code to write text to image. You can make a test(I do not save the image to output path, I set it to imageview directly).

       public class MainActivity : AppCompatActivity  
               Android.Graphics.Bitmap bitmap;  
               Android.Graphics.Bitmap mutableBitmap;  
               protected override void OnCreate(Bundle savedInstanceState)  
                   Xamarin.Essentials.Platform.Init(this, savedInstanceState);  
                   // Set our view from the "main" layout resource  
                   ImageView imageView1 = FindViewById<ImageView>(Resource.Id.imageView1);  
               public void Watermark()  
                   bitmap = BitmapFactory.DecodeResource(Resources, Resource.Drawable.hamburger); // Load your bitmap here  
                  mutableBitmap = bitmap.Copy(Android.Graphics.Bitmap.Config.Argb8888, true);  
                   Canvas canvas = new Canvas(mutableBitmap);  
                  // Canvas canvas = new Canvas(bitmap);  
                   Paint paint = new Paint();  
                   paint.Color= Android.Graphics.Color.Black;  
                   canvas.DrawText("Some", 50, 50, paint);  

    Here is running screenshot.



    I need to save picture after watermark, Is there any way?

    If you want to write picture to external path,

    If you want to support Android 11 or later devices, t you can var sdCardPath = GetExternalFilesDir(string.Empty).Path; path

    If you want to support Android 10 or before, you can use "/storage/emulated/0/Download/" this path, and add android:requestLegacyExternalStorage="true" to <application> tag in your AndroidManifest.xml

    For these details, you can refer to the following code.


    Here is my test code.

       private void Button1_Click(object sender, System.EventArgs e)  
                  var sdCardPath = GetExternalFilesDir(string.Empty).Path;  
                   var filePath = System.IO.Path.Combine(sdCardPath, "test.png");  
                   var stream = new FileStream(filePath, FileMode.Create);  
                   mutableBitmap.Compress(Android.Graphics.Bitmap.CompressFormat.Png, 100, stream);  

    Here is running screenshot.

    for `` var sdCardPath = GetExternalFilesDir(string.Empty).Path;



    for "/storage/emulated/0/Download/"


    Best Regards,

    Leon Lu

    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

0 additional answers

Sort by: Most helpful