You can reset the page (repage) like this:
image.Page = new MagickGeometry(0, 0);
image.Page = new MagickGeometry(0, 0);
MagickReadSettings settings = new MagickReadSettings(); settings.Density = new MagickGeometry(288, 288); using (MagickImageCollection images = new MagickImageCollection("image.emf", settings)) { using (MagickImage image = images.Flatten()) { image.Quality = 100; // You are missing a 1? image.ColorSpace = ColorSpace.RGB; image.Resize(0.5); // implicit cast to Percentage image.ColorSpace = ColorSpace.sRGB; image.Write("image.png"); } }
PNG compression
When used with PNG output, quality is regarded as two decimal figures. The first digit (tens) is the zlib compression level, 1-9. However if a setting of '0' is used you will get Huffman compression rather than 'zlib' compression, which is often better! Weird but true!
The second digit is the PNG data encoding filtering (before it is comressed) type: 0 is none, 1 is "sub", 2 is "up", 3 is "average", 4 is "Paeth", and 5 is "adaptive". So for images with solid sequences of color a "none" filter (-quality 00) is typically better. For images of natural landscapes an "adaptive" filtering (-quality 05) is generally better.
using (MagickImage image = images.Flatten()) { image.ColorSpace = ColorSpace.RGB; image.Resize(0.5); // implicit cast to Percentage image.ColorSpace = ColorSpace.sRGB; image.SetDefine(MagickFormat.Png, "compression-strategy", "0"); image.SetDefine(MagickFormat.Png, "compression-filter", "0"); image.Write("image.png"); }
using (MagickImageCollection new_images = new MagickImageCollection(),
old_images = new MagickImageCollection(),
diff_images = new MagickImageCollection())
{
new_images.Read(@"C:\Users\username\Pictures\projects\pdfcompare\new_drawing.pdf", settings);
old_images.Read(@"C:\Users\username\Pictures\projects\pdfcompare\old_drawing.pdf", settings);
int i = 1;
int j = 1;
foreach (MagickImage image in new_images)
{
image.Alpha(AlphaOption.Opaque);
i++;
}
foreach (MagickImage image in old_images)
{
image.Alpha(AlphaOption.Opaque);
j++;
}
// Compare both new and old
if (new_images.Count == old_images.Count )
{
for (int shtNum = 0; shtNum < new_images.Count; shtNum++)
{
MagickImage diff_image = new MagickImage();
MagickImage new_image = new_images.ElementAt(shtNum);
MagickImage old_image = old_images.ElementAt(shtNum);
new_image.Compare(old_image, Metric.FuzzError, diff_image);
diff_images.Add(diff_image);
}
}
// Output image diff
diff_images.Write(@"C:\Users\username\Pictures\projects\pdfcompare\output\diff-drawings" + ".pdf");
Here's the command line equivalent:gswin64c.exe -q -dQUIET -dBATCH -dNOPAUSE -NOPROMPT -sDEVICE=png16m -r150 -sOutputFile="sheet-%d.png" "input.pdf"
Then I do the comparison and output the pdfconvert.exe sheet-1.png sheet-2.png output.pdf
I'm guessing the command line is smaller because I'm turning some pngs into a pdf. I'm not sure what file format gets wrapped by the pdf in Magick.NET + ghostscript. Could also be that I don't have an alpha channel in the command line version and I do with the code. diff_image.HasAlpha = false;
diff_image.Format = MagickFormat.Png;
diff_image.Quality = 95;
diff_images.Add(diff_image);
The original pdf was 2.53MB and after the above addition it became 1.38MB. However the odd part is when I changed the image quality to 5 the pdf was the same 1.38MB. I also output the png diff_image by itself and compared the difference between the output at quality=5 and quality=95. The quality 5 was 674KB and quality 95 was 167KB. So the pdf shrunk by 45% and the png shrunk by 75%. I'm wondering why the png shrunk so much, but the pdf didn't.