Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use imageMagick with C#

Could you explain how I can use ImageMagick with C# . I am trying to convert PDF to pages into images.

I want to run imageMagick command "convert -density 300 $input.pdf $output.png"

like image 291
yohan.jayarathna Avatar asked Feb 07 '11 05:02

yohan.jayarathna


People also ask

What is MagickWand?

The MagickWand API is the recommended interface between the C programming language and the ImageMagick image processing libraries. Unlike the MagickCore C API, MagickWand uses only a few opaque types. Accessors are available to set or get important wand properties.

Is ImageMagick free?

ImageMagick is free software delivered as a ready-to-run binary distribution or as source code that you may use, copy, modify, and distribute in both open and proprietary applications. It is distributed under a derived Apache 2.0 license.

What is ImageMagick library?

ImageMagick, invoked from the command line as magick , is a free and open-source cross-platform software suite for displaying, creating, converting, modifying, and editing raster images. Created in 1987 by John Cristy, it can read and write over 200 image file formats.


2 Answers

string arguments = string.Format(@"-density 300 {0}.pdf {1}.png", intputFileName, outputFileName");
var startInfo = new ProcessStartInfo {
    Arguments = arguments,
    Filename = @"C:\path\to\imagick\convert.exe"
};
Process.Start(startInfo).WaitForExit();

References:

  • ProcessStartInfo
  • Process
like image 87
jgauffin Avatar answered Sep 20 '22 23:09

jgauffin


Magic.Net is a C# port for popular library ImageMagick. Install Magick.Net using Nuget package from url https://www.nuget.org/packages/Magick.NET-Q16-AnyCPU/ . Note that there are many versions of Magick.Net so select as per your need. This way you can use C#. See code below

Note it will append images vertically. Similarly you can append horizontally.

using ImageMagick;

string inputPdf= @"C:\my docs\input.pdf";
string outputPng= @"C:\my docs\output.png";

using (MagickImageCollection images = new MagickImageCollection())
{
    images.Read(inputPdf);
    using (IMagickImage vertical = images.AppendVertically())
        {
            vertical.Format = MagickFormat.Png;
            vertical.Density = new Density(300);  
            vertical.Write(outputPng);
        }
}
like image 37
Sujit Singh Avatar answered Sep 22 '22 23:09

Sujit Singh