title: Ghostscript.NET Rasterizer Out of Memory
body: <p>I'm using <a href="https://ghostscriptnet.codeplex.com" rel="nofollow noreferrer">Ghostscript.NET</a>, a handy C# wrapper for Ghostscript functionality. I have a batch of PDFs being sent from the clientside to be converted to images on the ASP .NET WebAPI server and returned to the client.</p>
  
  <pre><code>public static IEnumerable&lt;Image&gt; PdfToImagesGhostscript(byte[] binaryPdfData, int dpi)
  {
      List&lt;Image&gt; pagesAsImages = new List&lt;Image&gt;();
  
      GhostscriptVersionInfo gvi = new GhostscriptVersionInfo(AppDomain.CurrentDomain.BaseDirectory + @"\bin\gsdll32.dll");
  
      using (var pdfDataStream = new MemoryStream(binaryPdfData))
      using (var rasterizer = new Ghostscript.NET.Rasterizer.GhostscriptRasterizer())
      {
          rasterizer.Open(pdfDataStream, gvi, true);
  
          for (int i = 1; i &lt;= rasterizer.PageCount; i++)
          {
              Image pageAsImage = rasterizer.GetPage(dpi, dpi, i); // Out of Memory Exception on this line
              pagesAsImages.Add(pageAsImage);
          }
      }
      return pagesAsImages;
  }
  </code></pre>
  
  <p>This generally works fine (I generally use 500 dpi, which I know is high, but even dropping to 300 I can reproduce this error). But if I give it many PDFs from the clientside (150 1-page PDFs, for example) it will often hit an Out of Memory Exception in Ghostscript.NET Rasterizer. How can I overcome this? Should this be threaded? If so how would that work? Would it help to use the 64 bit version of GhostScript? Thanks in advance.</p>