Page 1 of 1

Force Density Units on Read?

Posted: 2017-12-13T10:42:36-07:00
by danburke
I am attempting to create a unit test around some dynamically generated images and wanted to ensure that images are being created at 300x300 dpi. In certain formats, such as png, they are being read back in metric, which fails my asserts. Is there a way to force it to dpi on read?

Some reproduce code:

Code: Select all

            string lvFile = @"C:\Temp\test.png";

            using (MagickImage lvImage = new MagickImage(new MagickColor(Color.Black), 2550, 3300))
            {
                lvImage.Settings.Format = MagickFormat.Png;
                lvImage.Settings.Compression = Compression.Group4;
                lvImage.Settings.Density = new Density(300, 300, DensityUnit.PixelsPerInch); 
                lvImage.Settings.Debug = true;
                lvImage.Settings.Verbose = true;
                lvImage.Density = new Density(300, 300, DensityUnit.PixelsPerInch);

                lvImage.Write(lvFile);
            }

            using (MagickImage lvWrittenImage = new MagickImage(lvFile))
            {
                Trace.WriteLine(lvWrittenImage.Density); // Outputs 118.11x118.11 cm
            }
I am running .Net 4.5.1 with nuget package Magick.NET-Q8-AnyCPU version 7.2.0.

Thanks

Re: Force Density Units on Read?

Posted: 2017-12-13T11:14:35-07:00
by snibgo
I believe the PNG standard allows for integer numbers, "units unknown" and "per metre" only. So it doesn't record density as "per inches". See http://www.w3.org/TR/2003/REC-PNG-20031110/#11pHYs

EDIT: I'll add that, at the command line, we can change the units to get what we want after the read, eg:

Code: Select all

convert r.png -units pixelsperinch -verbose info: