This project has moved. For the latest updates, please go here.

Color Profile is null. I might be missing something

May 15, 2014 at 11:37 PM
Edited May 15, 2014 at 11:39 PM
Using the following image, the color profile at the end(cp1) is null when reading after a conversion.

https://www.dropbox.com/s/hntk98bk2qp4gan/8632_168.jpg

The code is:
        var destinationStream = new FileStream("test.png", FileMode.Create);

        //this is the image in dropbox
        string sourceFilePath = "sourceFile"

        MagickImage magickImage1 = new MagickImage(sourceFilePath);

        magickImage1.Strip();
        magickImage1.Format = MagickFormat.Png;
        magickImage1.Density = new MagickGeometry(300, 300);

        destinationStream.SetLength(0);
        magickImage1.Write(destinationStream);

        MagickImage magickImageDestination;

        destinationStream.Seek(0, SeekOrigin.Begin);
        magickImageDestination = new MagickImage(destinationStream);

        MagickImage magickImageSource = new MagickImage(sourceFilePath);

        ColorProfile cp = magickImageSource.GetColorProfile();

        if (cp != null)
        {
            magickImageDestination.AddProfile(cp);
        }

        destinationStream.SetLength(0);

        try
        {
            magickImageDestination.Write(destinationStream);
        }
        catch (Exception e)
        {

        }

        destinationStream.Seek(0, SeekOrigin.Begin);
        MagickImage mi = new MagickImage(destinationStream);

        ColorProfile cp1 = mi.GetColorProfile();
Coordinator
May 16, 2014 at 1:39 PM
The png coder automatically removes the color profile from your image. If you want to keep it you should set the following define before reading and before writing the image.
try
{
  magickImageDestination.SetDefine(MagickFormat.Png, "preserve-iCCP", true);
  magickImageDestination.Write(destinationStream);
}
catch (Exception e)
{
}

destinationStream.Seek(0, SeekOrigin.Begin);
MagickImage mi = new MagickImage();
mi.SetDefine(MagickFormat.Png, "preserve-iCCP", true);
mi.Read(destinationStream);

ColorProfile cp1 = mi.GetColorProfile();
p.s. You will get color coding when you put 'C#' behind ``` that starts a code block.
May 16, 2014 at 1:45 PM
Thanks.

Do I need to use that only when converting to png? Are there similar things I need to do/set when converting/reading other file types, like jpg, pdf, etc.?
Coordinator
May 17, 2014 at 5:07 PM
I am only aware of the png coder doing this. I know jpg and pdf don't do this but there might be other formats that also do this.
May 17, 2014 at 11:06 PM
Great. And for my own knowledge, do you know why the png coder does this? And not a problem if you don't know. It's more of just a curiosity on my part.
Coordinator
May 19, 2014 at 6:35 AM
I did not know the answer to that question so I contacted the original developer of the png coder and I got the following answer:
It only removes them when it discovers that the profile can be replaced with the sRGB chunk. If the profile represents something
other than the sRGB profile, it will be preserved, unless it is being discarded for another reason such as Strip().