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

Adding installed Image Color Profiles (.icm) into an image in C#

Sep 22, 2014 at 5:25 PM
In one of my C# project (.net framework 4.0), I need to add the installed color profiles in windows, into images if it already have no color profiles in it. I Google and found nothing in .net for the color profile management, So I adopt the imagemagick library and use its "addprofile" method but after adding and saving the image whenever i ready the image it doesn't contain the color profile. While searching on the internet I found to apply colorprofile.sRGB before applying your custom color profile and I do the same and when I read the image it contain the color profile "icc" in the image. Following is color profile adding code
using (MagickImage image = new MagickImage(outputFilePath))
        {
            ImageMagick.ColorProfile colorProfile = image.GetColorProfile();
            if (colorProfile == null)
            {
                //Add sRGB color profile before adding your custom color profile
                image.AddProfile(ImageMagick.ColorProfile.SRGB);
                //ICSPrfile is the object containing the name and path of availabe prfiles
                //_profilesRGB is the list containing the ICSProfiles
                foreach (ICSProfile prof in _profilesRGB)
                    if (prof.Name.Equals(comboBoxRGBProfile.SelectedItem))
                        image.AddProfile(new ImageMagick.ImageProfile(prof.Name, File.ReadAllBytes(prof.FilePath)));

                //Write update profile file
                image.Write(outputFilePath);
            }//End if
        }//End using
Now, I am not sure, after writing this code whether my image contain the custom color profile or not.Did I wrote a correct code? and how can I check the color profiles in an image.
Coordinator
Sep 22, 2014 at 7:28 PM
Thank you for posting your message here also, I was wondering what you are trying to attempt.

Do you want to apply a color profile to your image or do you want to embed a color profile in your image? You should not also add the SRGB color profile if you just want to embed a color profile in your image. Removing the call 'image.AddProfile(ImageMagick.ColorProfile.SRGB);' should solve you issue:
using (MagickImage image = new MagickImage(outputFilePath))
{
  ImageMagick.ColorProfile colorProfile = image.GetColorProfile();
  if (colorProfile == null)
  {
    //ICSProfile is the object containing the name and path of available profiles
    //_profilesRGB is the list containing the ICSProfiles
    foreach (ICSProfile prof in _profilesRGB)
      if (prof.Name.Equals(comboBoxRGBProfile.SelectedItem))
        image.AddProfile(new ImageMagick.ColorProfile (prof.FilePath));

    //Write update profile file
    image.Write(outputFilePath);
  }//End if
}//End using

using (MagickImage image = new MagickImage(outputFilePath))
{
  ImageMagick.ColorProfile colorProfile = image.GetColorProfile();
  if (colorProfile != null)
    Console.WriteLine("Image has a color profile");
}
Sep 22, 2014 at 7:45 PM
Thank you for the reply.
I did the same but when I load the image in another test application using the same code and tried to get the profile it returned null. Following is the code of the test application
            try
            {
                using (MagickImage image = new MagickImage(openFileDialog1.FileName))
                {
                    ColorProfile profile = image.GetColorProfile();
                    if (profile != null)
                    {
                        MessageBox.Show(profile.Name);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
            }
Sep 22, 2014 at 7:47 PM
If I apply a color profile from available profiles in ColorProfile class like SRGB then the image.GetColorProfile(); did return a profile in the ColorProfile object in the test application
Coordinator
Sep 22, 2014 at 8:42 PM
Is it possible that your test if (prof.Name.Equals(comboBoxRGBProfile.SelectedItem)) is not correct? And image.AddProfile(new ImageMagick.ColorProfile (prof.FilePath)); never gets called?
Sep 22, 2014 at 8:57 PM
Its absolutely not the case. I just test it out and took a screen shot of ProfileNames property which shows the profile added in it. What I believe that the profile get embedded in the image but there is problem in image.GetColorProfile(); part of the test application, I think it didn't load the profiles along with the image, because I tested out many other images that include color profiles but it didn't show any name just display 'icc' as the color profile name
Coordinator
Sep 22, 2014 at 9:08 PM
Edited Sep 22, 2014 at 9:08 PM
Are you testing with JPEG or PNG? The PNG reader applies the profile when it reads the image and removes it.
Sep 23, 2014 at 5:09 AM
I am testing it with JPEG.
Sep 23, 2014 at 6:37 AM
Edited Sep 23, 2014 at 6:39 AM
Lets make it simple
  1. Image Loaded
    MagickImage image = new MagickImage(outputFilePath
  2. Profile added to image
    image.AddProfile(new ImageMagick.ColorProfile (prof.FilePath));
  3. Image saved
    image.Write(outputFilePath);
    
    
  4. Saved Image loaded in another application
      MagickImage image = new MagickImage(openFileDialog1.FileName)
  5. Get profile called
      ColorProfile profile = image.GetColorProfile();
  6. Nothing returned "profile" object is null
Please tell me whats wrong with the code? or anything missing?
Coordinator
Sep 23, 2014 at 6:52 AM
Can you add a link to the image and color profile you are using?
Sep 23, 2014 at 7:04 AM
Coordinator
Sep 23, 2014 at 10:54 AM
I just tried the following:
using (MagickImage before = new MagickImage(@"C:\test\jpg\image_1.jpg"))
{
  ColorProfile colorProfile = new ColorProfile(@"C:\test\jpg\sRGB_Color_Space_Profile.icm");
  before.AddProfile(colorProfile);
  before.Write(@"C:\test\jpg\image_1_with_profile.jpg");

  using (MagickImage after = new MagickImage(@"C:\test\jpg\image_1_with_profile.jpg"))
  {
    Console.WriteLine(colorProfile.Equals(after.GetColorProfile()));
  }
}
The output I am getting is 'True'. Are you using an older version of Magick.NET?
Sep 23, 2014 at 11:51 AM
Yes, I am using the 6.0.0 version of ImageMagick.net, I download the latest version (7.0.0 x64) and integrate it into the same project, First I got an error "error loading ImageMagick" it resolved when I set the project architecture to x64. After the configuration the result become "True" and at last I found a profile in an image.

Now why I was using the older version, the reason is that I have to provide backward compatibility to some of my customers. Here raised another question

"Is there any support of color profile in the older versions (supporting 32bit)"
Coordinator
Sep 23, 2014 at 1:27 PM
Edited Sep 23, 2014 at 1:28 PM
I cannot help you with older version of Magick.NET it was probably a bug that has been fixed in the mean time. Are you using the latest 6 version? It will probably work in that version, otherwise you could try until you find a version that works? And there is also a 32bit version of the 7 version available on the download page.
Sep 23, 2014 at 1:53 PM
Thank you so much for your help. I found the 32 bit version as well