Force Density Units on Read?

Magick.NET is an object-oriented C# interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick.NET
Posts: 1
Joined: 2017-12-13T10:38:21-07:00
Authentication code: 1152

Force Density Units on Read?

Post 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);


            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.


Posts: 13034
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Force Density Units on Read?

Post 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

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:
snibgo's IM pages: