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

Magick.NET- Freezing System

Feb 4, 2015 at 3:31 PM
This could easily be something I'm doing, I have the identical below, just for .NET 3.5, and this one is built against .NET 4.5 - so when I run the code below, I completely freeze my system. It's so bad, all I can do is turn the computer off and back on. Event Viewer isn't capturing anything and there are some partial files left behind, but no other error message I can see. Any help would appreciated.
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using TDCI.BuyDesign.Configurator.Engine;
using TDCI.BuyDesign.Configurator.Engine.Expressions;
using TDCI.BuyDesign.Configurator.Engine.RuleEngine;

using ImageMagick;

namespace SVGtoRaster
    public class SVGtoRaster : IExternalProgram
        public Value Execute(IExecutionState executionState, ReadOnlyRuleParameterCollection parameters,
            ReadOnlyComponentAttributeCollection componentAttributes)
                //do work
                RuleParameter sourceName;
                RuleParameter targetName;
                RuleParameter fileType;
                parameters.TryGetValue("SourceName", out sourceName);
                parameters.TryGetValue("TargetName", out targetName);
                parameters.TryGetValue("FileType", out fileType);
                // For DebugView
                Trace.WriteLine("SourceName" + sourceName.Value);
                Trace.WriteLine("TargetName" + targetName.Value);
                Trace.WriteLine("FileType" + fileType);
                // Read SVG image from file
                using (MagickImage image = new MagickImage(sourceName.Value.ToString()))
                    image.Format = (MagickFormat)Enum.Parse(typeof(MagickFormat), fileType.Value.ToString(), true);
                    //image.Format = MagickFormat.Png;
                    image.Write(string.Concat(targetName.Value.ToString(), ".", fileType.Value.ToString()));

                var returnCollection = new SimpleExpressionValueCollection
                    {"returnValue", new Value("Success: EP called")}

                return new Value(returnCollection);
            catch (Exception ex)
                executionState.Trace("SVGtoPNG: Error - " + ex.StackTrace + ", " + ex.Message);
                Trace.WriteLine("SVGtoPNG: Error - " + ex.StackTrace + ", " + ex.Message);

                var errorCollection = new SimpleExpressionValueCollection
                    {"Description", new Value("Error: " + ex.Message)}

                return new Value(errorCollection);

            //throw new NotImplementedException();
Feb 4, 2015 at 3:44 PM
Does this problem only happen in the .NET 4.5 build? Does this happen with a specific svg file or does it happen with any svg file? Are you executing the code in a multi threaded environment?
Feb 4, 2015 at 7:30 PM
Okay - glad you asked about the various SVG's. It does appear it's just with this one particular model. I'll test the older build and see if there's an issue I wasn't aware of, and yes it will be executed in a multi threaded environment, I just happen to be the only one using/testing it right now.

Feb 4, 2015 at 9:46 PM
Edited Feb 4, 2015 at 9:47 PM
Can you provide a link to the file that is causing this issue? It might be a bug in ImageMagick or librsvg. Contact me through codeplex if you don't want to share the file publicly.
Feb 5, 2015 at 12:54 AM
I can share the file, there's nothing to it really - I just can't seem to figure out how to upload it....
Feb 5, 2015 at 12:59 AM
Man I'm getting old - I'm now wondering if it's the same issue I posted before - which is incidentally I used the same file for testing.... - I have to admit - I thought maybe the newest version would better handle such large images....

Here's the link to the image:
Feb 5, 2015 at 2:27 PM
This size issue is definitely my problem. Anything done in millimeters makes the size huge.

So I attempted to use the Density setting, but I'm getting this error:

Error 1 Cannot implicitly convert type 'ImageMagick.MagickGeometry' to 'ImageMagick.PointD?'

But my issue seems to be dynamically figuring out when to resize/scale or set the density is there a methodology anyone would recommend?

Thanks for the help.
Feb 5, 2015 at 7:40 PM
The type of the Density property has been changed from MagickGeometry to a Nullable<PointD>. My old example no longer works.

You could determine the size by looking at the viewBox/width/height properties of the svg document. And then you could calculate the best suitable density if you know the size of the image you want to create.
Feb 11, 2015 at 6:43 PM
I've been trying to resize the image - I have read the image size - and yes they're huge. I can read the info, but the minute I try and get the image, everything freezes - I'm clearly doing something wrong, I'm just not sure what, maybe there's a size limitation I don't know about - here's my code:

// Read SVG image from file
            MagickImageInfo info = new MagickImageInfo(sourceName.Value.ToString());
            debugLoc = "Reading Image Info";

            iwide = info.Width;
            iheight = info.Height;

            debugLoc = "Getting image";
            using (MagickImage image = new MagickImage(sourceName.Value.ToString()))
                //resize if any dimension is larger than 5,000
                debugLoc = "Checking Size";
                if (iwide > 5000 || iheight > 5000)
                    dWide = iwide / 25;
                    dHeight = iheight / 25;

                    dWide = Math.Round(dWide, 0, MidpointRounding.AwayFromZero);
                    dHeight = Math.Round(dHeight, 0, MidpointRounding.AwayFromZero);

                    wide = Convert.ToInt32(dWide);
                    height = Convert.ToInt32(dHeight);
                    debugLoc = "Resizing Image";
                    image.Resize(wide, height);

                image.Format = (MagickFormat)Enum.Parse(typeof(MagickFormat), fileType.Value.ToString(), true);
                image.Write(string.Concat(targetName.Value.ToString(), ".", fileType.Value.ToString()));
``` Not sure what I need to do - the file is an SVG file if that makes any difference.

Feb 11, 2015 at 8:41 PM
Edited Feb 11, 2015 at 8:42 PM
The reason it still 'hangs' is because you are resizing the image after you read it. It will still allocate the memory for an image that is 37017x31865. This worked on my machine but took almost all my 16GB of memory.

You should specify the density before you load the image to get a smaller memory footprint. The following example calculates the best density for an image that fits inside 5000x5000:
string sourceName = "SVG_Image_1.svg";
MagickImageInfo info = new MagickImageInfo(sourceName);

// Assume ResolutionX and ResolutionY are the same
double density = info.ResolutionX;

// Maximum width or height
int maxSize = 5000;

// Calculate best density
while (density > 0.0 &&
         (density * (info.Width / info.ResolutionX) > maxSize) ||
         (density * (info.Height / info.ResolutionX) > maxSize))

MagickReadSettings settings = new MagickReadSettings()
  Density = new PointD(density)

// Load image with the specified settings that will provide you with a smaller image
using (MagickImage image = new MagickImage(sourceName, settings))
Feb 11, 2015 at 8:48 PM

I thought you had to load it first and then resize it. I knew I was doing something wrong - thanks a million for all the help!
Feb 11, 2015 at 8:50 PM
Do you need my paypal account to send me that million? :P
Feb 11, 2015 at 8:51 PM
LOL! - Nope I just Fed-Ex'd a large brief case full of cash! ;-)