Apply watermark on image using imagemagick.net in c#

主宰稳场 提交于 2019-11-29 15:52:03

问题


I am using the following code to resize the image. Now, i need to apply the watermark on this image using Magick.NET.

        var response = client.GetObject(request).ResponseStream;
        MagickImage image = new MagickImage(response);
        MagickGeometry size = new MagickGeometry(imgWidth, imgHeight);
        size.IgnoreAspectRatio = maintainAspectRatio;                                                       
        image.Resize(size);   


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

Working Code :

            MagickGeometry size = new MagickGeometry(imgWidth, imgHeight);
            size.IgnoreAspectRatio = maintainAspectRatio;                                                       
            image.Resize(size);   


            Bitmap watermarkObj = (Bitmap)Bitmap.FromFile("G:/Images/watermark.png");
            Bitmap objImg = new Bitmap("G:/Images/OriginalImage.jpg");
            Graphics imageGraphics = Graphics.FromImage(objImg);
            Point point = new Point(image.Width - 118, image.Height - 29);                            
            imageGraphics.DrawImage(watermarkObj, point); 
            objImg.save("G:/Images/ProcessedImage.JPG");

So, Can anyone help me how to do it with using imagemagick? Reason being when i pass imageObject in graphics it doesn't apply the watermark where as when i pass the .net image object it apply the watermark.


回答1:


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");
  }
}


来源:https://stackoverflow.com/questions/31701190/apply-watermark-on-image-using-imagemagick-net-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!