Search code examples
c#watermarkimageresizer

How to align watermark to top right using Image Resizer Watermark Plugin?


I am using Image Resizer's Watermark Plugin (https://imageresizing.net/docs/v4/plugins/watermark) to add Logo to all images of the website. But the logo that is added is always in the centre. I want to append it to anywhere on the image. Please note that I am not configuring watermark image (logo) in the config file as the documentation suggests.

Can you please tell me what should be the ResizeSettings query parameter to align the logo to top/bottom? Here is the code that I tried but didn't work:

 ImageLayer i = new ImageLayer(c);      
 i.Path = "Logo Path";
 wp.NamedWatermarks["img"] = new Layer[] { i };
 byte[] buffer = File.ReadAllBytes("Orignal Image");
 Stream sourceStream = new MemoryStream(buffer);
 Stream destinationStream = new MemoryStream();
 ImageBuilder.Current.Build(sourceStream, destinationStream, new ResizeSettings("watermark=img;align=BottomLeft"));
 Image img = System.Drawing.Image.FromStream(destinationStream);
 img.Save("Path");

Thanks.


Solution

  • Hello Fortunately I got the answer by myself. I found that ImageLayer class has the properties Left/Top/Bottom/Right and it expects parameter of type DistanceUnit class.

    So here is what I have done:

    Before doing this wp.NamedWatermarks["img"] = new Layer[] { i }; register the distanceunit.

      DistanceUnit distanceUnit = new DistanceUnit(35, DistanceUnit.Units.Percentage);
      i.Left = distanceUnit;
      i.Top = distanceUnit;
    

    This worked like a charm.