0

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.

dlemstra
  • 7,813
  • 2
  • 27
  • 43
Naresh
  • 5,073
  • 12
  • 67
  • 124

1 Answers1

4

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");
  }
}
dlemstra
  • 7,813
  • 2
  • 27
  • 43
  • First of all thanking you very much for the answer and the documentation. It would really help people whoever will struggle with it. I also want to ask one more thing that is if i use the following code `image = new MagickImage(new Bitmap(image.ToBitmap())); image.Write("G://Images/db.jpg"); ` . Why does the image object size reduces as soon as i write it on the disk. As i have mentioned in the above code. Why does the writing an object on disk changes it's size? – Naresh Aug 01 '15 at 17:26
  • I guess there is a bug in the MagickGeometry class if i pass the value of height and width of double type. Although, it takes it with any error but when finally it resizes the image. It will be of random size not the value which you passed in the object. So, typecasting from double to int is not proper in the object. – Naresh Aug 06 '15 at 07:32
  • Can you report the problem in more detail here: https://magick.codeplex.com/discussions? – dlemstra Aug 06 '15 at 07:56
  • I am getting the error of "could not load file or assembly 'Magick.NET-x64' or one of its dependencies" in windows 10 where as same code is working in windows 8. Even though i have installed (Visual C++ Redistributable) Is there any issue with Magick.NET. – Naresh Nov 10 '15 at 09:09
  • hey, is that a bug in the existing library `http://stackoverflow.com/questions/35270389/gif-images-looses-its-animation-in-magick-net` – Naresh Feb 08 '16 at 13:08