Parameter is not valid

May 29, 2014 at 10:01 PM
I am having an issue with a certain PDF when trying to convert it to Tiff. Here is the PDF.

When I run the program it gives me an error of "Parameter is not valid" when trying to convert the last page of the PDF (the page with a image on it) at the line:
img.SaveAdd(page, GetAddPageEncoderParameters());
Where
        private EncoderParameters GetAddPageEncoderParameters()
        {
            var encoderParams = new EncoderParameters(2);
            encoderParams.Param[0] = new EncoderParameter(Encoder.Compression, GetEncoderCompressionValue(_settings.Compression));
            encoderParams.Param[1] = new EncoderParameter(Encoder.SaveFlag, (long)EncoderValue.FrameDimensionPage);
            return encoderParams;
        }
Thanks
Coordinator
May 30, 2014 at 5:56 AM
Hi,

What version of Ghostscript.NET do you use?
What version of the native Ghostscript library do you use?

Is you marchine 32bit or 64bit?

Can you please post the complete code that you use to convert PDF to Tiff ?

Cheers,
Josip
May 30, 2014 at 12:51 PM
Ghostscript.NET Version: 1.1.8
Ghostscript Lib Version: 9.14 gsdll32
Machine: 64-bit

Code here.

And I've tried the 64-bit Lib and it gives me the same error.
Coordinator
May 30, 2014 at 6:45 PM
Hi,

I found the problem, but I'm still not sure what's causing it and if this is a Ghostscript.NET bug / problem.

The workaround would be to clone an Image returned by the GhostscriptRasterizer and then add it to the Tiff.

Here is the code sample that implements Image cloning and this way I find it works correctly:
private MemoryStream CreateTiffStream(GhostscriptRasterizer rasterizer)
{
    var stream = new MemoryStream();
    var encoder = ImageCodecInfo.GetImageEncoders().First(a => a.MimeType == "image/tiff");

    Image img = null;
    Image page;

    for (int pageNumber = 1; pageNumber <= rasterizer.PageCount; pageNumber++)
    {
        if (pageNumber == 1)
        {
            img = rasterizer.GetPage(_settings.Resolution.X, _settings.Resolution.Y, pageNumber);
            img = img.Clone() as Image;
            img.Save(stream, encoder, GetNewImageEncoderParameters());
        }
        else
        {
            page = rasterizer.GetPage(_settings.Resolution.X, _settings.Resolution.Y, pageNumber);
            page = page.Clone() as Image;
            img.SaveAdd(page, GetAddPageEncoderParameters());
        }
    }

    img.SaveAdd(GetFlushImageEncoderParameters());
    stream.Seek(0, SeekOrigin.Begin);
    return stream;
}
I hope this workaround helps until I find a proper solution.

Cheers,
Josip
May 30, 2014 at 7:47 PM
Hmm that's strange. Thanks for the workaround, it works as expected. If you find out what the proper solution is let me know. Seems like it's something to do with the page having a different width and height, but I could be wrong.