diff --git a/NImageMagick/FilterType.cs b/NImageMagick/FilterType.cs new file mode 100644 index 0000000..e52de11 --- /dev/null +++ b/NImageMagick/FilterType.cs @@ -0,0 +1,40 @@ +using System; +using System.Linq; + +namespace NImageMagick +{ + public enum FilterType + { + UndefinedFilter, + PointFilter, + BoxFilter, + TriangleFilter, + HermiteFilter, + HanningFilter, + HammingFilter, + BlackmanFilter, + GaussianFilter, + QuadraticFilter, + CubicFilter, + CatromFilter, + MitchellFilter, + JincFilter, + SincFilter, + SincFastFilter, + KaiserFilter, + WelshFilter, + ParzenFilter, + BohmanFilter, + BartlettFilter, + LagrangeFilter, + LanczosFilter, + LanczosSharpFilter, + Lanczos2Filter, + Lanczos2SharpFilter, + RobidouxFilter, + RobidouxSharpFilter, + CosineFilter, + SplineFilter, + SentinelFilter /* a count of all the filters, not a real filter */ + } +} diff --git a/NImageMagick/Image.cs b/NImageMagick/Image.cs index 539d01a..a70d286 100644 --- a/NImageMagick/Image.cs +++ b/NImageMagick/Image.cs @@ -1,34 +1,87 @@ using System; using System.Linq; -using System.Runtime.InteropServices; namespace NImageMagick { - public class Image + public class Image : IDisposable { - private IntPtr handle; + public IntPtr Handle { get; private set; } - public IntPtr Handle + public int ImageCompressionQuality { - get { return this.handle; } + get + { + return (int)ImageMagick.MagickGetImageCompressionQuality(this.Handle); + } + set + { + this.ExecuteChecked(ImageMagick.MagickSetImageCompressionQuality, (uint)value); + } + } + + public int Width + { + get + { + return (int)ImageMagick.MagickGetImageWidth(this.Handle); + } + } + + public int Height + { + get + { + return (int)ImageMagick.MagickGetImageHeight(this.Handle); + } } public Image(string path) { ImageMagick.EnsureInitialized(); - handle = ImageMagick.NewMagickWand(); - if (handle == IntPtr.Zero) + Handle = ImageMagick.NewMagickWand(); + if (Handle == IntPtr.Zero) { throw new Exception("Error acquiring wand."); } - IntPtr str = ImageMagick.AllocateUTF8String(path); - int result = ImageMagick.MagickReadImage(this.handle, str); - ImageMagick.FreeString(str); - if (result == 0) + using (NativeString pathString = new NativeString(path)) { - throw new ImageMagickException(this.handle); + this.ExecuteChecked(ImageMagick.MagickReadImage, pathString.Pointer); } } + + public void ExecuteChecked(Func action, T param1) + { + if (action(this.Handle, param1) != 1) + { + throw new ImageMagickException(this.Handle); + } + } + + public void ExecuteChecked(Func action, T1 param1, T2 param2, T3 param3, T4 param4) + { + if (action(this.Handle, param1, param2, param3, param4) != 1) + { + throw new ImageMagickException(this.Handle); + } + } + + public void Save(string path) + { + using (NativeString pathString = new NativeString(path)) + { + this.ExecuteChecked(ImageMagick.MagickWriteImage, pathString.Pointer); + } + } + + public void Resize(int width, int height, FilterType filterType, double blur) + { + this.ExecuteChecked(ImageMagick.MagickResizeImage, (uint)width, (uint)height, (int)filterType, blur); + } + + public void Dispose() + { + this.Handle = ImageMagick.DestroyMagickWand(this.Handle); + } } } diff --git a/NImageMagick/ImageMagick.cs b/NImageMagick/ImageMagick.cs index b6543b5..9cec1ab 100644 --- a/NImageMagick/ImageMagick.cs +++ b/NImageMagick/ImageMagick.cs @@ -32,7 +32,25 @@ static class ImageMagick //public static extern int MagickGetExceptionType(IntPtr ptr); [DllImport(WandDll, CallingConvention = WandConvention)] - public static extern IntPtr MagickGetException(IntPtr ptr, ref int exceptionType); + public static extern IntPtr MagickGetException(IntPtr ptr, out int exceptionType); + + [DllImport(WandDll, CallingConvention = WandConvention)] + public static extern int MagickClearException(IntPtr ptr); + + [DllImport(WandDll, CallingConvention = WandConvention)] + public static extern uint MagickGetImageCompressionQuality(IntPtr ptr); + + [DllImport(WandDll, CallingConvention = WandConvention)] + public static extern int MagickSetImageCompressionQuality(IntPtr ptr, uint quality); + + [DllImport(WandDll, CallingConvention = WandConvention)] + public static extern uint MagickGetImageWidth(IntPtr ptr); + + [DllImport(WandDll, CallingConvention = WandConvention)] + public static extern uint MagickGetImageHeight(IntPtr ptr); + + [DllImport(WandDll, CallingConvention = WandConvention)] + public static extern int MagickResizeImage(IntPtr ptr, uint columns, uint rows, int filterType, double blur); private static object isInitializedSyncRoot = new object(); private static bool isInitialized = false; diff --git a/NImageMagick/ImageMagickException.cs b/NImageMagick/ImageMagickException.cs index ecb2198..aeda9ef 100644 --- a/NImageMagick/ImageMagickException.cs +++ b/NImageMagick/ImageMagickException.cs @@ -1,6 +1,5 @@ using System; using System.Linq; -using System.Runtime.InteropServices; namespace NImageMagick { @@ -13,9 +12,10 @@ public ImageMagickException(IntPtr wandHandle) private static string DecodeException(IntPtr wandHandle) { - int exceptionSeverity = 0; - IntPtr exceptionPtr = ImageMagick.MagickGetException(wandHandle, ref exceptionSeverity); - return Marshal.PtrToStringAnsi(exceptionPtr); + int exceptionSeverity; + IntPtr exceptionPtr = ImageMagick.MagickGetException(wandHandle, out exceptionSeverity); + ImageMagick.MagickClearException(wandHandle); + return NativeString.Load(exceptionPtr); } } } diff --git a/NImageMagick/NImageMagick.csproj b/NImageMagick/NImageMagick.csproj index da285c3..c94726d 100644 --- a/NImageMagick/NImageMagick.csproj +++ b/NImageMagick/NImageMagick.csproj @@ -39,8 +39,11 @@ + + + diff --git a/NImageMagick/NImageMagick.csproj.user b/NImageMagick/NImageMagick.csproj.user new file mode 100644 index 0000000..76fe5a5 --- /dev/null +++ b/NImageMagick/NImageMagick.csproj.user @@ -0,0 +1,6 @@ + + + + ProjectFiles + + \ No newline at end of file diff --git a/NImageMagick/NativeString.cs b/NImageMagick/NativeString.cs index 23a3e66..0ab71e7 100644 --- a/NImageMagick/NativeString.cs +++ b/NImageMagick/NativeString.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; @@ -23,5 +24,23 @@ public void Dispose() { Marshal.FreeHGlobal(this.Pointer); } + + public static string Load(IntPtr pointer) + { + List bytes = new List(); + byte[] buf = new byte[1]; + int index = 0; + while (true) + { + Marshal.Copy(pointer + index, buf, 0, 1); + if (buf[0] == 0) + { + break; + } + bytes.Add(buf[0]); + ++index; + } + return Encoding.UTF8.GetString(bytes.ToArray()); + } } } diff --git a/Test/Program.cs b/Test/Program.cs index d10debe..3f322f1 100644 --- a/Test/Program.cs +++ b/Test/Program.cs @@ -8,7 +8,9 @@ class Program { static void Main(string[] args) { - var image = new Image(@"C:\test.png"); + var image = new Image(@"Z:\Temp\dcd\folder.jpg"); + image.Resize(800, 600, FilterType.CubicFilter, 1); + image.Save(@"C:\Temp\test.jpg"); } } }