0

Trying to figure out how to take a memoryStream and return images using Ghost Script. Here's the code as well as the error I'm getting once I execute rasterizer.Open:

 public static System.Drawing.Image PDFToImage(MemoryStream inputMS)
    {
        GhostscriptRasterizer rasterizer = null;
        GhostscriptVersionInfo version = null;
        if (Environment.Is64BitProcess)
           version = new GhostscriptVersionInfo(
                new Version(0, 0, 0), @"C:\Program Files\gs\gs9.20\bin\gswin64.exe",
                string.Empty, GhostscriptLicense.GPL);
        else
            version = new GhostscriptVersionInfo( 
                new Version(0, 0, 0), @"C:\Program Files (x86)\gs\gs9.20\bin\gswin32.exe",
                string.Empty, GhostscriptLicense.GPL);

        int dpi = 96;
        System.Drawing.Image img = null;

        using (rasterizer = new GhostscriptRasterizer())
        {
            rasterizer.Open(inputMS, version, true);

            for (int i = 1; i <= rasterizer.PageCount; i++)
            {

                using (MemoryStream ms = new MemoryStream())
                {
                    img = rasterizer.GetPage(dpi, dpi, i);
                    img.Save(ms, ImageFormat.Jpeg);
                    ms.Close();
                }

            }
            rasterizer.Close();
        }
        return img;
    }

GhostScriptConsole

Crumblenautjs
  • 169
  • 1
  • 3
  • 24

2 Answers2

1

Well, Ghostscript is telling you it can't open the file '/config:C:\Users\Sean.McNary\ApexRemington\.vs\config\applicationhosts.config' because (not entirely unsurprisingly with such a garbled filename), it can't find the file.

I presume you are using some kind of wrapper around Ghostscript (which is a DLL written in C), because you are apparently using C#, it would help if you were to state what you are using there.

It 'looks like' whatever wrapper you are using, it expects to be given an input filename, and simply passes that to Ghostscript. While it is possible to pass data from memory to Ghostscript, and to have the rendered result returned in memory, you should be aware that if the input is a PDF file you aren't saving anything, because Ghostscript will spool the whole input to a temporary file before processing it You have to seek around a PDF file, so you need to have random access, hence its written out as a file.

You are going to have to debug into your wrapper and see what its doing.

KenS
  • 30,202
  • 3
  • 34
  • 51
  • Hey @KenS. I'm using GhostScript.NET. What I'm trying to accomplish is to pass in the memory stream of this PDF, and return an image. Then I want to convert this Image back into a stream, where I will add it to a stream queue, and print all of the streams. The reason I'm not just converting the PDF into a stream directly and doing stream.enqueue(PDF) is because when I do that, I get a GDI+ generic error. The original queue of streams are images. If you know of a better way to do this, then I'm all for it. – Crumblenautjs Feb 06 '17 at 21:29
  • You should add the Ghostscript.net tag to your question, that way the author might notice and comment. I'm not a C# programmer, so I have no idea what you are really trying to achieve, I'm afraid I don't understand your explanation :-( My main point was that passing a PDF file 'in memory' only means that Ghostscript writes it as a temporary file, it doesn't save you anything to do so. If you are looking at printing a PDF file to a printer, Ghostscript can already do that using the mswinpr2 device, probably better because it will render at printer resolution. – KenS Feb 07 '17 at 08:06
  • Ghostscript.NET doesn't work with ghostscript exe. You should point it to ghostscript dll. – HABJAN Feb 08 '17 at 09:47
1

The problem is that you are pointing to exe file instead of dll. When you replace 'gswin64.exe' to 'gsdll64.dll' (a same for 32 bit verion) your code should work.

HABJAN
  • 9,212
  • 3
  • 35
  • 59