write text on an image xamarin android

hossein tavakoli 471 Reputation points
2021-03-19T07:54:16.827+00:00

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);
                }
            }

            bitmap.Save(picturePath);
        }
Xamarin
Xamarin
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
    2021-03-19T12:23:36.207+00:00

    Hello,​

    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)  
               {  
                   base.OnCreate(savedInstanceState);  
                   Xamarin.Essentials.Platform.Init(this, savedInstanceState);  
                   // Set our view from the "main" layout resource  
                   SetContentView(Resource.Layout.activity_main);  
                   Watermark();  
                   ImageView imageView1 = FindViewById<ImageView>(Resource.Id.imageView1);  
                   imageView1.SetImageBitmap(mutableBitmap);  
               }  
                
               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;  
                   paint.TextSize=50;  
                   canvas.DrawText("Some", 50, 50, paint);  
         
               }  
    

    Here is running screenshot.

    79520-image.png

    ======================
    Update========================

    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.

    https://stackoverflow.com/questions/66490122/how-to-save-db-to-external-storage-so-user-can-see-it-in-xamarin-forms/66491973#66491973

    Here is my test code.

       private void Button1_Click(object sender, System.EventArgs e)  
               {  
                    
         
                  var sdCardPath = GetExternalFilesDir(string.Empty).Path;  
                   //"/storage/emulated/0/Download/"  
                   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);  
                   stream.Close();  
               }  
    

    Here is running screenshot.

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

    80141-image.png

    80142-image.png

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

    80066-image.png

    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