Apply watermark on image using imagemagick.net in c#

扶醉桌前 提交于 2019-11-30 10:39:27

Your code is not working because image.ToBitmap() creates a new Bitmap. When you call image.write("G:/Images/ProcessedImage.JPG"); you are saving an unmodified version of the image instance. You should do the following instead.

using (MagickImage image = new MagickImage(response))
{
  MagickGeometry size = new MagickGeometry(imgWidth, imgHeight);
  size.IgnoreAspectRatiomaintainAspectRatio;                                   
  image.Resize(size);

  using (Bitmap watermarkObj = Bitmap)Bitmap.FromFile("G:/Images/watermark.png"))
  {
    using (Bitmap imageObj = image.ToBitmap())
    {
      using (Graphics imageGraphics = Graphics.FromImage(imageObj))
      {
        Point point = new Point(image.Width - 118, image.Height - 29);
        imageGraphics.DrawImage(watermarkObj, point);
        imageObj.Save("G:/Images/ProcessedImage.JPG");
      }
    }
  }
}

Also notice that I added the using statements. You should really use this when you are working with IDisposable classes.

You can also do this without using System.Drawing. I have created a new example in the documentation of Magick.NET for this: https://magick.codeplex.com/wikipage?title=Watermark&referringTitle=Documentation

You can use the following code in your situation:

using (MagickImage image = new MagickImage(response))
{
  MagickGeometry size = new MagickGeometry(imgWidth, imgHeight);
  size.IgnoreAspectRatiomaintainAspectRatio;                                   
  image.Resize(size);

  using (MagickImage watermark = new MagickImage("G:/Images/watermark.png"))
  {
    image.Composite(watermark, image.Width - 118, image.Height - 29, CompositeOperator.Over);
    image.Write("G:/Images/ProcessedImage.JPG");
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!