Skip to content

Instantly share code, notes, and snippets.

@codebutler
Created May 30, 2010 03:34
Show Gist options
  • Select an option

  • Save codebutler/418756 to your computer and use it in GitHub Desktop.

Select an option

Save codebutler/418756 to your computer and use it in GitHub Desktop.
//
// C# wrapper around the ImageMagick Wand API
//
// Authors:
// Eric Butler <eric@extremeboredom.net>
//
// (C) 2006 Eric Butler
//
// See http://www.imagemagick.org/script/magick-wand.php
//
using System;
using System.Runtime.InteropServices;
public class MagickWand : IDisposable
{
public static void Main ()
{
MagickWand foo = new MagickWand ();
foo.ReadImage ("example.png");
foo.ResizeImage (20, 20);
}
IntPtr magick_wand;
public MagickWand ()
{
MagickWandInterop.MagickWandGenesis();
magick_wand = MagickWandInterop.NewMagickWand();
}
public void ReadImage (string filename)
{
IntPtr filename_ptr = Marshal.StringToHGlobalAuto(filename);
int status = MagickWandInterop.MagickReadImage(magick_wand, filename_ptr);
if (status == MagickWandInterop.MagickFalse) {
throw new Exception ("Failed to read image.");
}
}
public void ResizeImage (uint width, uint height)
{
MagickWandInterop.FilterTypes type = MagickWandInterop.FilterTypes.LanczosFilter;
MagickWandInterop.MagickResizeImage(magick_wand, width, height, type, 0.0);
}
public void Dispose ()
{
magick_wand = MagickWandInterop.DestroyMagickWand(magick_wand);
MagickWandInterop.MagickWandTerminus();
}
private static class MagickWandInterop
{
[DllImport("libWand")]
public static extern void MagickWandGenesis ();
[DllImport("libWand")]
public static extern void MagickWandTerminus ();
[DllImport("libWand")]
public static extern IntPtr NewMagickWand ();
[DllImport("libWand")]
public static extern int MagickReadImage (IntPtr magick_wand, IntPtr file_name);
[DllImport("libWand")]
public static extern int MagickResizeImage (IntPtr magick_wand, ulong columns, ulong rows, FilterTypes filter, double blur);
[DllImport("libWand")]
public static extern IntPtr MagickWriteImage (IntPtr magick_wand, IntPtr file_name, IntPtr data);
[DllImport("libWand")]
public static extern IntPtr DestroyMagickWand (IntPtr magick_wand);
public static readonly int MagickFalse = 0;
public enum FilterTypes : int
{
UndefinedFilter,
PointFilter,
BoxFilter,
TriangleFilter,
HermiteFilter,
HanningFilter,
HammingFilter,
BlackmanFilter,
GaussianFilter,
QuadraticFilter,
CubicFilter,
CatromFilter,
MitchellFilter,
LanczosFilter,
BesselFilter,
SincFilter
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment