MagickWand Examples in C
The programs on these pages show how to use the ImageMagick MagickWand API. Each example is presented as a C function, complete with headers, so that it can be copied to a file and then included in your own C project. The code is written for Windows and was compiled using MSVC V7 but, with a little tweaking, it should also run on Linux.
Some of the examples show a command line "program" and how to achieve the equivalent output using MagickWand. By "equivalent" I mean that the program produces the same image as the command line example - it won't necessarily be exactly how it is achieved by the convert program. In some cases there is only one command line example to replicate but in others there can be 3 or 4 commands which build on each other to produce a final result. In most of these cases I have created one C function which replicates all the commands but each section of code will show how it relates to the original commands.
There is little or no error checking in the examples but you should add your own where needed. For example, the MagickReadImage function returns a true or false value depending upon whether the read succeeded and you should check this using code similar to this:
You should note that there are a lot of MagickWand functions which are declared as returning a boolean result but which will always return MagickTrue and are therefore not worth checking. This is particularly true with the DrawingWand commands. For example, even if DrawSetFont doesn't find the font you specify, it will return MagickTrue because it will set the font to your default system font.
If you haven't used MagickWand
before, you should start with something easy such as this:
It is easy to see that to make this convert one of your own input files to another format you just replace "logo:" with your input filename and replace "logo.jpg" with whatever output filename you wish.
The MagickReadImage function is very versatile. It will accept a website URL which links to an image somewhere on the internet or it can generate a plain coloured canvas using the "xc:" filename convention. E.G. "xc:red" will create a red canvas and "xc:none" will create a transparent canvas. If you use the "xc:" format you will usually precede the read function with a call to MagickSetSize otherwise you will get a 1x1 pixel image. It is worthwhile to read the "pseudo Image" and "Built-in Images" sections near the bottom of the ImageMagick Formats page to see what other pseudo files are available.
Just in case you are deciding whether to use MagickCore instead of MagickWand, first
look at the simple example I showed in the previous paragraph and then look at
the equivalent written for MagickCore:
I think you'll agree that even in this simple example, the MagickWand version is much easier to read and write.
All of this code is provided AS IS with NO warranty or guarantee of any kind
Each image is shown on a gray background to show transparent images better
Here's an INDEX of the MagickWand functions used in each example
If you have any comments, send them to:
or send me a private message on the ImageMagick Forum