Bradley Mountford
Bradley Mountford

Reputation: 8283

C# Magick.Net "no decode delegate for this image format" with SVG

I have ImageMagick-6.8.9-Q16 installed and can successfully run convert test.svg test.pdf and get a valid pdf output.

In my project I have installed Magick.NET-Q16-x86 version 7.0.0.003 and I can successfully convert other formats using it.

However, when I run the following code, I get an "iisexpress.exe: no decode delegate for this image format" error:

using (var image = new MagickImage(File.OpenRead("C:\\Temp\\SvgToPdf\\test.svg"))) //error here
{
    image.Format = MagickFormat.Pdf;
    image.Write("C:\\Temp\\SvgToPdf\\test.pdf");
}

Any ideas?

Upvotes: 4

Views: 7262

Answers (2)

David
David

Reputation: 91

Ufff, I had similar problem. Magick.NET throws "ImageMagick.MagickMissingDelegateErrorException" on creation of MagickImage object from Stream with error message like "no decode delegate for this image format `' @ error/blob.c/CustomStreamToImage/761". When I saved the Stream into file, picture is absolutely OK..

... After almost 2 days of debugging/trying I recognized, that this error with the same Stream sometimes throws, sometimes not... -> the problem is in Stream state. Before MagickImage creation it has to be Seek into beginning!! Maybe this is error in ImageMagick, because... :-/

    public ResizedImageWithMetadata GetResizedImageWithMetadata(Stream content,..)
        {
            try
            {
                if (content == null)
                {
                    throw new ArgumentNullException($"Image content is empty!");
                }
                using (MagickImage image = new MagickImage(content))
                {
// unexpected exception...

Correct:

public ResizedImageWithMetadata GetResizedImageWithMetadata(Stream content,..)
    {
        try
        {
            if (content == null)
            {
                throw new ArgumentNullException($"Image content is empty!");
            }
            content.Seek(0, SeekOrigin.Begin); //THIS IS NEEDED!!!
            using (MagickImage image = new MagickImage(content))
            {

Upvotes: 5

Bradley Mountford
Bradley Mountford

Reputation: 8283

Crap...wouldn't you know I would figure it out within minutes of posting...this is why I don't ask many questions!

Anyway, for those wondering, the problem was with opening the file using File.OpenRead (probably does not have all of the necessary info in the FileStream). changing my code to this works:

using (var image = new MagickImage("C:\\Temp\\SvgToPdf\\test.svg"))
{
    image.Format = MagickFormat.Pdf;
    image.Write("C:\\Temp\\SvgToPdf\\test.pdf");
}

Thanks to dlemstra for the alternative solution for when you do need to use a Stream!

var readSettings = new MagickReadSettings() { Format = MagickFormat.Svg };
var image = new MagickImage(svgStream, readSettings);

Upvotes: 5

Related Questions