4

I am trying to implement a functionality using Magick.NET in C#.

Previously I was using:-

// Convert to a png.
Process p = new Process();

p.StartInfo.FileName = @"C:\Program Files\ImageMagick-6.2.8-Q16\convert.exe";
p.StartInfo.Arguments = "-scale 60% \"" + svg + "\" \"" + png + "\"";
p.StartInfo.CreateNoWindow = true;

p.Start();

p.WaitForExit();

TransmitFile(context, png);

I want to move away from having to store convert.exe on the server.....Now I want to use something that will be in code and doesn't need to reference an executable file on the server:-

// Pseudo-code:-
MagickImage img = new MagicImage();
Image.Add(svg);
Image.Format = MagickFormat.png;
Image.Scale = 60%;

But I cannot find enough documentation to implement the same functionality that I was using before. Is there a place with appropriate documentations? I have googled quite a bit, without success.

Han
  • 3,052
  • 2
  • 22
  • 31
Philo
  • 1,931
  • 12
  • 39
  • 77

1 Answers1

7

There are some examples of how to use Magick.NET available here.

An example of how to convert one image to another image can be found here. But there is no example for -scale 60%.

Most options from the command line have the same name in the MagickImage class. Your command convert input.svg -scale 60% output.png translates to this:

using (MagickImage image = new MagickImage("input.svg"))
{
  image.Scale(new Percentage(60));
  image.Write("output.png");
}
dlemstra
  • 7,813
  • 2
  • 27
  • 43
  • Thanks! I am getting a couple of errors though:- using - 'ImageMagickObject.MagickImage': type used in a using statement must be implicitly convertible to 'System.IDisposable' ............ 'ImageMagickObject.MagickImage' does not contain a definition for 'Write' and no extension method 'Write' accepting a first argument of type 'ImageMagickObject.MagickImage' could be found (are you missing a using directive or an assembly reference?............ The type or namespace name 'Percentage' could not be found (are you missing a using directive or an assembly reference?......... – Philo Aug 05 '15 at 17:02
  • I installed Magic.NET x64.dll and Core.dll and wrapper-x64.dll in Visual Studio – Philo Aug 05 '15 at 17:05
  • 1
    It looks like you are using some other library that has a class called MagickImage. The namespace of the MagickImage class is ImageMagick not ImageMagickOjbect – dlemstra Aug 05 '15 at 17:08
  • you are right, I have both ImageMagick and ImageMagickObject referenced at the namespace. But after removing the latter, now MagickImage has an issue - the type or namespace 'MagickImage' could not be found (missing reference??) – Philo Aug 05 '15 at 17:11
  • You should add a 'using' for ImageMagick. – dlemstra Aug 05 '15 at 17:14
  • I am using ImageMagick now, but in the statement MagickImage image = new MagickImage()... I am getting the reference error.... do I have to add the assembly to the web.config : – Philo Aug 05 '15 at 17:16
  • I looked in the object browser and my imageMagick namespace doesn't have a "magickImage" in it. – Philo Aug 05 '15 at 17:18
  • Found the solution, seems like the way I installed Magick.NET thru NuGet on my VS2012 was the issue... or maybe the version installed. I went to Magick.NET website and downloaded the dll manually and referenced it in my project and it worked. Thanks. – Philo Aug 05 '15 at 18:04
  • Instead of scale, how do I compress the image? Can you please share. – Krunal Jul 18 '19 at 07:40
  • Seems like your link is not working anymore... Could you update it please? – Bjørn Mar 20 '20 at 14:43