Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use imagemagick.net in .net ? [closed]

I'm looking almost hour for examples of using imagemagick.net in c# and I can't find antything.

All what I need is resize image (.jpg) to new size image (jpg, too) and would be great if you known how to add watermark.

I downloaded imagemagick.net from

http://imagemagick.codeplex.com/

like image 353
user278618 Avatar asked Jun 08 '10 11:06

user278618


2 Answers

Download this: http://magick.codeplex.com Update link: https://github.com/dlemstra/Magick.NET also available via NuGet package manager.

In your project, make a reference to the ImageMagickNET.dll

You may need to set the platform to x86 in you configuration

Now you can use this code to resize an image:

ImageMagickNET.MagickNet.InitializeMagick();
var image = new ImageMagickNET.Image("test.jpg");
image.Resize(new ImageMagickNET.Geometry("50%"));
image.Write("result.jpg");

Instead of using the ImageMagick.Net library you could also use the program directly:

var proc = new Process
{
    StartInfo = new ProcessStartInfo
    {
        FileName = "convert.exe",
        Arguments = "-resize 50% -draw \"gravity south fill black text 0,0 'Watermark' \" test.jpg result.jpg",
        UseShellExecute = false,
        RedirectStandardError = true,
        CreateNoWindow = true
    }
};

proc.Start();
string error = proc.StandardError.ReadToEnd();
proc.WaitForExit();
like image 131
roeland Avatar answered Oct 24 '22 11:10

roeland


Do you have to use ImageMagick? You can use GDI+ if your aim is to redeliver an image in another size. http://www.switchonthecode.com/tutorials/csharp-tutorial-image-editing-saving-cropping-and-resizing gives this function for resizing. I've used this tutorial in the past for watermarking: http://www.codeproject.com/KB/GDI-plus/watermark.aspx

private  static Image resizeImage(Image imgToResize, Size size)
{
  int sourceWidth = imgToResize.Width;
  int sourceHeight = imgToResize.Height;

  float nPercent = 0;
  float nPercentW = 0;
  float nPercentH = 0;

  nPercentW = ((float)size.Width / (float)sourceWidth);
  nPercentH = ((float)size.Height / (float)sourceHeight);

  if (nPercentH < nPercentW)
    nPercent = nPercentH;
  else
    nPercent = nPercentW;

  int destWidth = (int)(sourceWidth * nPercent);
  int destHeight = (int)(sourceHeight * nPercent);

  Bitmap b = new Bitmap(destWidth, destHeight);
  Graphics g = Graphics.FromImage((Image)b);
  g.InterpolationMode = InterpolationMode.HighQualityBicubic;

  g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
  g.Dispose();

  return (Image)b;
}
like image 24
David Fox Avatar answered Oct 24 '22 11:10

David Fox