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/
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();
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With