2

How can I add a png image as a watermark to a larger image using Xamarin.iOS c# and save the output to the device?

I figured out the Xamarin.Android version from another question posted here.

Thanks in Advance!!

1 Answers1

3

Using an image context, you can draw the original, then the watermark at the necessary location and obtain a new image from the context.

ImageContext example:

var originalImage = UIImage.FromBundle("buymore.jpg");
var watermarkImage = UIImage.FromFile("vs.png");

UIGraphics.BeginImageContextWithOptions(originalImage.Size, true, 1.0f);
originalImage.Draw(CGPoint.Empty);
watermarkImage.Draw(new CGRect(new CGPoint(200, 200), watermarkImage.Size));

var processedImage = UIGraphics.GetImageFromCurrentImageContext();

If your original and watermark images are the same size, you can use a CIFilter (CISourceOverCompositing) to "overlay" one image on top of another (assuming your watermark has a white or alpha background. This is my preferred method due to the speed.

CISourceOverCompositing example:

UIImage processedimage;
using (var filter = new CISourceOverCompositing())
{
    filter.Image = new CIImage(UIImage.FromBundle("vs.png"));
    filter.BackgroundImage = new CIImage(UIImage.FromBundle("buymore.jpg"));

    processedimage = UIImage.FromImage(filter.OutputImage);
}
SushiHangover
  • 73,120
  • 10
  • 106
  • 165