This project has moved and is read-only. For the latest updates, please go here.

Image conversion in web app

May 22, 2013 at 3:16 PM
Hi All

I'm having trouble getting the following code to run in an web application. This is a brand new web application with one aspx file and the code below is in the Page_Load method as a test.
string path = HostingEnvironment.MapPath(@"~\bin\ImageMagick");

using (MagickImage image = new MagickImage(@"c:\test\img_01.svg"))
I get an error like this "System.Runtime.InteropServices.SEHException: External component has thrown an exception". The stack trace looks like this
[SEHException (0x80004005): External component has thrown an exception.]
   Magick.Image.{ctor}(Image* ) +0
   ImageMagick.MagickImage..ctor(String fileName) +57
   WebApplication1.WebForm1.Page_Load(Object sender, EventArgs e) in C:\Users\rgo\Documents\Visual Studio 2010\Projects\WebApplication1\WebApplication1\WebForm1.aspx.cs:19
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +25
   System.Web.UI.Control.LoadRecursive() +71
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3064
Not sure what the problem is. I guess it's something to do with the location of the dlls but I can't work it out. The dlls have all been copied to the bin folder in a sub folder called "ImageMagick".

The same code in a console application works ok.

Has anyone got any ideas?


May 22, 2013 at 3:48 PM
  • What version of Magick.NET are you using? You can get the version from the Version property of MagickNET.
  • Is your application pool using the correct platform x86/x64?
  • Did you specify the width and height in your svg file?
May 22, 2013 at 3:51 PM

Don't worry. I just managed to get it working. Must have had the wrong path somewhere.


May 22, 2013 at 3:57 PM
When you are using .NET 4.0 you could use Magick.NET.Web.dll. It makes sure that the code below gets called at the start of the application.
string path = HostingEnvironment.MapPath(@"~\bin\ImageMagick");
May 29, 2013 at 3:29 PM
Is this available for .NET 2.0 ??
May 29, 2013 at 4:17 PM
You can use Magick.NET in a .NET 2.0 web application. But you cannot use Magick.NET.Web.dll because it uses 'PreApplicationStartMethodAttribute' which is only available since .NET 4.0. You need to call MagickNET.Initialize yourself.

And in response to the question you posted on the ImageMagick.NET discussions board. You could use this to remove a border from a image:
using (MagickImage image = new MagickImage("source.image"))
  image.Shave(borderWidth, borderWidth);
May 30, 2013 at 6:40 AM

Is there another way to remove black border, since I have a random width and height coming from scanner and i can't define where the black border is in the picture. Is there a way to know the border width and Height ?!!

Thanks in advanced
May 31, 2013 at 5:46 AM
Can you share two examples of an image with a black border? It seems that you have to check the color of the pixels of the image. Here is an example of how you can access them:
using (MagickImage image = new MagickImage())
  PixelCollection pixels = image.GetReadOnlyPixels();
  Pixel pixel = pixels[0, 0];
  // With pixel.GetChannel you can access the channels of the pixel, e.g. RGB:
  int red = pixel.GetChannel(0);
  int green = pixel.GetChannel(1);
  int blue = pixel.GetChannel(2);
May 31, 2013 at 6:41 AM

My images can have random black borders and even not black it may be gray following to the scanners.
your provided example can show me the width and height for the black border? Sorry but I don't know how to use it.

Below are the images i'm trying to remove there black borders:

Jun 1, 2013 at 5:33 PM
There is no easy way to do this with the examples you provided. You will have to write some code that calculates the edges of the image. My example shows how you can access the individual pixels of an image. You could use this to determine the edges of your images.