1

Attempting to open an image, set its resolution, and save it, and I'm running into an issue where the ColorSpace is being modified without any input on my end.

The input image is a blank sRGB/8 image, checking img.ColorSpace after creation shows sRGB, however after setting the density, writing the image to disk, and opening the output, the ColorSpace is set to Gray.

using (var img = new MagickImage(inputImagePath))
{
    img.Density = new Density(parameters.HorizontalResolution, parameters.VerticalResolution);
    img.Write(outputImagePath);
    // img.ColorSpace is sRGB
}

using (var imgCheck = new MagickImage(outputImagePath))
{
    // imgCheck.ColorSpace is gray
}

The downstream code requires the colorspace on the output image to be the same as the input image's. Is there a parameter for MagickImage.Write() that can preserve the color space?

Harrison Paine
  • 611
  • 5
  • 14

1 Answers1

0

After reviewing this related question: ImageMagick: convert keeps changing the colorspace to Gray. How to preserve sRGB colorspace? it appears that if the image's format is set to MagickFormat.Png, and its content is grayscale, ImageMagick sets the color space to grayscale.

using (var img = new MagickImage(inputImagePath))
{
    img.Density = new Density(parameters.HorizontalResolution, parameters.VerticalResolution);
    img.Write(outputImagePath, MagickFormat.Png00);
    // img.ColorSpace is sRGB
}

using (var imgCheck = new MagickImage(outputImagePath))
{
    // imgCheck.ColorSpace is gray
}

Manually setting the MagickFormat on Write() to Png00 persists the input's color space and bits per pixel setting.

Harrison Paine
  • 611
  • 5
  • 14