ArtifexSoftware / Ghostscript.NET

Ghostscript.NET - managed wrapper around the Ghostscript library (32-bit & 64-bit). Tested with Ghostscript versions < 10.
https://ghostscript.com
GNU Affero General Public License v3.0
403 stars 155 forks source link

GhostscriptRasterizer.GetPage Setting DPI has no impact on the resulting raster #108

Open MarcChasse opened 1 year ago

MarcChasse commented 1 year ago

In the following demo program I would expect the HorizontalResolution and VerticalResolution to both be set to the dpi value passed into GhostscriptRasterizer.GetPage but it seems ot have no impact and resulting document always have 96 as the Horizontal and Vertical Resolution.

static void Main(string[] args)
{
    using (FileStream fs = File.OpenRead(@"C:\temp\annots.pdf"))
    {
        var mytiff = ProcessMemoryStream(fs);
        FileStream file = new FileStream(@"C:\temp\ghostscript.tif", FileMode.Create, FileAccess.Write);
        mytiff.WriteTo(file);
        file.Close();
        mytiff.Close();
    }
    Console.ReadKey();
}
static MemoryStream ProcessMemoryStream(FileStream inputMS)
{
    var gv = new GhostscriptVersionInfo(@"C:\Program Files\gs\gs10.00.0\bin\gsdll64.dll");
    using (var rasterizer = new GhostscriptRasterizer())
    {
        rasterizer.Open(inputMS, gv, false);

        MemoryStream tiffStream = new MemoryStream();

        System.Drawing.Image img = rasterizer.GetPage(300, 2);
        Console.WriteLine($"{img.Width}x{img.Height}");
        Console.WriteLine($"{img.HorizontalResolution}x{img.VerticalResolution}"); // <---- Always prints 96x96 regardless of the dpi value above
        img.Save(tiffStream, ImageFormat.Tiff);

        rasterizer.Close();
        return tiffStream;
    }
}

I'm usign the latest version of Ghostscript, 10.0.0.0 and of Ghostscript.NET, 1.2.3. I've also tried using the latest version of Ghostscript.NET from main and changing the hardcoded default values tform 96 to 300 with no impact:

    public class GhostscriptViewer : IDisposable
    {
        ...
        private int _zoom_xDpi = 300;
        private int _zoom_yDpi = 300;
        ...
    }

Thanks for building a great wrapper Ultimately I would like to emulate this command in code and i think all i need is the Resolution now

gswin64c -dSAFER -dBATCH -dNOPAUSE -sDEVICE=tiff24nc -dGraphicsAlphaBits=4 -sCompression=lzw -r300 -dMaxStripSize=0 -sOutputFile=tiger.tif Original.pdf

FYI there was a change to fix this issue but that seems to not have worked: x and y dpi ignores the set value #75