question

6666666 avatar image
0 Votes"
6666666 asked CherryBu-MSFT answered

How to add watermark in xamarin.forms?

I am using xamarin.forms can add a watermark to image? I have read the image to FileStream.

I do not want to add image in android or ios but only in xamarin.forms

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

CherryBu-MSFT avatar image
0 Votes"
CherryBu-MSFT answered CherryBu-MSFT edited

Hello,

Welcome to our Microsoft Q&A platform!

If you just want to add watermark on image on Xamarin.forms, you can consider to use Xamarin.Forms DependencyService to do this. The DependencyService class is a service locator that enables Xamarin.Forms applications to invoke native platform functionality from shared code.


Firstly, create an interface.

 public interface IImageFile
     {
         //void SaveImage(string name, byte[] data, string location = "temp");
    
         byte[] AddWatermark(byte[] imgByteArray);
     }

then implement the interface on Android platform. Don't forget to register the platform implementations

 [assembly: Dependency(typeof(ImageFile))]
 namespace FormsSample.Droid
 {
     public class ImageFile : IImageFile
     {
            
         public byte[] AddWatermark(byte[] imgByteArray)
         {
             bitmap = BitmapFactory.DecodeByteArray(imgByteArray, 0, imgByteArray.Length);
    
             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("hellod world", 50, 50, paint);
    
             MemoryStream stream = new MemoryStream();
             mutableBitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);
             byte[] bitmapData = stream.ToArray();
             return bitmapData;
         }
     }
 }

Now adding watermark in image by calling the Get<T> method to resolve the IImageFile interface, and then invoking its AddWatermark method:

 private void water_Clicked(object sender, EventArgs e)
         {
             var image = new Xamarin.Forms.Image();
    
             var assembly = this.GetType().GetTypeInfo().Assembly;
             byte[] imgByteArray = null;
             byte[] watermark = null;
    
             var s = assembly.GetManifestResourceStream("FormsSample.f19.png");
    
             if (s != null)
             {
                 var length = s.Length;
                 imgByteArray = new byte[length];
                 s.Read(imgByteArray, 0, (int)length);
                 watermark = DependencyService.Get<IImageFile>().AddWatermark(imgByteArray);
    
                   
                 var stream1 = new MemoryStream(watermark);
                 image.Source = ImageSource.FromStream(() => stream1);
    
                 image.WidthRequest = 50;
                 image.HeightRequest = 50;
                 imagestack.Children.Add(image);
             }
         }

The screenshot:
95084-image-7.png

Adding watermark on image for ios, you can take a look:

https://stackoverflow.com/questions/48513275/xamarin-ios-image-on-image-watermark

Best Regards,
Cherry Bu


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.



image-7.png (68.2 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.