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
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
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:
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.
8 people are following this question.