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:

 if(MagickReadImage(mw,"logo_gel.png") == MagickFalse) {
        // Handle the error
 }

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:

      logo_1.c     A simple starter. Convert the builtin logo: image to JPG.

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:

      MagickCore.c     The simple starter program 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

Resize an image to 50% using a Lanczos filter and save as high-quality JPG
      resize.c
      Image

Extend the canvas around an image.
      extent.c
      Image

Simple floodfill to change an image's white background to transparent.
      floodfill.c
      Image

Slightly more complex floodfill (similar to third example in Masking Simple Backgrounds)
      cyclops.c
      Image

Using a Clip Mask to Limit the Composed Area
      clipmask.c

Change *all* white pixels to be transparent - we also get the wizard's beard!.
      trans_paint.c
      Image

Draw shapes: circle, polygon, rounded rectangle, rotated and translated ellipse, line
      draw_shapes.c
      Image

Eight text effects in one function
      text_effects.c
      Shadow
      Pattern
      Arc
      Bevel
      Plain
      Barrel
      Polar
      Shepards

Fun with fontmetrics!
      fontmetrics.c
      Image

Create rounded rectangle mask similar to the ROUNDED BORDERS example.
      round_mask.c
      Image

Create a tileable image from an input image.
      make_tile.c
      Plasma
      Random

The Better 3-D Logo Generation Example
      3dlogo.c
      Shadow
      Background

The "Gel" Effects Example
      gel.c
      Image

Add a reflection below an image - fade the reflection to transparent using CopyOpacity.
      reflect.c
      Image

Four examples of using MagickDistortImage to do affine transformations
      affine.c
      Image
      Image
      Image
      Image

Using PixelIterators to create a grayscale gradient
      grayscale.c
      Image

Using PixelIterators to read and create images and also do HSL/HSB colourspace conversion
      modulate.c
      Image
      Image

Convert a 2D image to pseudo-3D
      landscape_3d.c
      Image

Anthony's tilt-shift example
      tilt_shift.c
      Input Image
      Output Image

Change the colour of one pixel in the logo: built-in image
      pixel_mod.c
      logo_pixel.gif

Adding a Static Background to a Cleared Frame Animation
      bunny.c
      Background Image
      Animation
      Output Image


 

 

 

If you have any comments, send them to:

or send me a private message on the ImageMagick Forum

 

 This Web Page Created with PageBreeze Free Website Builder
and two TCL scripts that I wrote