locked
Image processed has wrong colours RRS feed

  • Question

  • User1173753381 posted

    Hi!

    I have a piece of code that draws an image onto a canvas and then saves it. I need to re-save the image in this way to clear the image of all image data (exif, xmp etc.).

    The code is:

     

    Image original = (Image)Image.FromFile(Path);
    
    Bitmap b = new Bitmap(original.Width, original.Height, PixelFormat.Format24bppRgb);
    Graphics g = Graphics.FromImage(b);
    
    g.FillRectangle(Brushes.Gray, 0, 0, original.Width, original.Height);
    g.DrawImage(original, 0, 0, original.Width, original.Height);
    
    b.Save(TargetPath, ImageFormat.Tiff);




    Now, the problem is that the processed, saved, image has the wrong colors, as can be viewed below:

    Color 

     

    The left image is pre-process and the image to the right is the processed image which has the wrong colors, as you can see.


    What causes this and what can I do to prevent it?

     

    I've tried to have the source image as both jpeg and tiff, but that doesn't matter.

     

    Images, in the future, may have different colour profiles and I need the color-profiles preserved.

     

     

    Thanks in advance,

    Fredrik

    Wednesday, November 5, 2008 3:56 PM

Answers

  • User-1190926628 posted

    Is your original file a jpeg?  If so, I found this:

    http://www.sentex.ca/~mwandel/jhead/

    http://www.irfanview.com/

    jhead is a command line tool, which you may be able to embed in your application.

    Irfanview has a full blown UI and does batch processing as well.

    I haven't used them myself so I don't know how they'd work for you.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 7, 2008 3:14 PM

All replies

  • User-1190926628 posted

    Have you tried it with other PixelFormats, like PixelFormat.Max?

    Wednesday, November 5, 2008 4:28 PM
  • User1173753381 posted

    Yes, I have.

    When using PixelFormat.Max, i.e., I get Exception Details: System.ArgumentException: Parameter is not valid.

    The same happens with all the PixelFormats except the FormatXXRgb etc. ones.

     

    Thanks for your reply.

    Wednesday, November 5, 2008 4:34 PM
  • User-1190926628 posted

    What about setting the Image.Palette for the new image to be equal to that of the original?  Perhaps the pallette for the orginial isn't the default palette for the image class?

    http://msdn.microsoft.com/en-us/library/system.drawing.imaging.colorpalette_members.aspx

    Wednesday, November 5, 2008 4:55 PM
  • User1173753381 posted

     Thank you for the reply.

     "Transferring" the palette is a good idea, but I can't get it to work with the examples I've found online.

     if I do:

      

    Bitmap original = (Bitmap)Image.FromFile(Path);
    ColorPalette palette = original.Palette;
    
    Bitmap b = new Bitmap(original.Width, original.Height);
    Graphics g = Graphics.FromImage(b);
    
    g.FillRectangle(Brushes.Gray, 0, 0, original.Width, original.Height);
    g.DrawImage(original, 0, 0, original.Width, original.Height);
    
    b.Palette = palette;
     

    I get:

    Exception Details: System.ArgumentException: Parameter is not valid.

    on b.Palette = palette;

     

    I've tried having original as a System.Drawing.Image as well. Same issue.

     

    Would you mind giving me an example of how I can do this?

     

    Thanks,

    Fredrik

    Wednesday, November 5, 2008 7:07 PM
  • User-1190926628 posted

    I'm sort of at a loss here.  I suppose that the definition of a palette for a tiff palette is different than a bmp palette

    Here's something from a graphics smart person on codeproject:

    http://www.codeproject.com/KB/security/steganodotnet11.aspx

    I think I'm getting out of my depth graphics-wise here.  Could you maybe change your color settings on your desktop and see if you notice any changes?

    Gotta go on-site for a few hours, good luck!

     

    Thursday, November 6, 2008 9:56 AM
  • User1173753381 posted

     Thank you for your suggestions though!

     I did have a look in PhotoShop and noticed that C# only makes images in the sRGB workspace. My photos are Adobe RGB and need to stay that way. I'm trying to find a way to make it AdobeRGB But I can't find a good solution for it.

     

    Thank you!

     

    Fredrik

    Thursday, November 6, 2008 10:00 AM
  • User-1190926628 posted

    This is a very interesting topic, isn't it?  Check this out:

    http://www.earthboundlight.com/phototips/srgb-versus-adobe-rgb-debate.html

    What Adobe RGB does have then is colors that are further apart from one another than are those in sRGB. The colors must be more widely spaced, since the quantity available is the same as in sRGB but they have to cover a wider gamut.

    And since they do stretch to fill a different sized gamut, specific sets of red, green and blue values represent different colors in Adobe RGB than they do in sRGB. To cover that wider gamut in fact, specific values in Adobe RGB end up being used for more highly saturated colors. Take green for example. Since Adobe RGB can represent more saturated green colors yet both color spaces are limited to 255 for the most blazing green possible, 255 must get used for a more saturated green in Adobe RGB than in sRGB. The highest numbers have to represent colors at the edges of the gamut and that edge is further out in Adobe RGB.

    That would explain why the green in your original AdobeRGB motorcycle picture is more vivid than the green in the sRGB version after it is converted to a tiff.  The green values for those pixels are the same numerically but on the sRGB pallette that number is associated with a less saturated green.  I'm sure there is a mathematical transform that we can do to adjust your palettes.  It would explain why you get invalid parameters when you apply a AdobeRGB palette to a bmp - the saturation levels are too high at the extremes of the palette's chromatograph.

    And here's something!  I love Google!

    http://www.codeproject.com/KB/GDI-plus/colorclass.aspx

     

    Thursday, November 6, 2008 5:28 PM
  • User1173753381 posted

    Hi!

     

    It is indeed very interesting.

    Thanks for the tip but I still need to keep the AdobeRGB profile in the image. This isn't a question of having the picture LOOK the same - it needs to be the exact same. Since it's professional photography I need to be able to maintain the profile so it's ready for the customers - who works with AdobeRGB to CMYK and never sRGB.

     

    I did find a way to maintain the same colors too by the way. I do this by adding this to the image:

    System.Drawing.Image original = System.Drawing.Image.FromFile(Path, true); <----- "true" = "useEmbeddedColorManagement".

     

    This converts the colors of the sRGB to the ones in AdobeRGB so that it maintains the same "look".

     

    Still not the same profile though! ;-)

     

     

    Cheers,

    Fredrik

    Thursday, November 6, 2008 5:36 PM
  • User-1190926628 posted

    Maybe you can use the Adobe developers kit to work that:

    http://www.adobe.com/devnet/xmp/

    Thursday, November 6, 2008 5:51 PM
  • User1173753381 posted

    Hi!


    I am using the xmp-toolkit as well (the c# one found at www.halligang.se/xmp). Though this toolkit is used for storing metadata (descriptions etc.) in i.e. images. It cannot change the color profile itself, it can only specify the details _about_ the color management like bits per channel.

     

     

    Thank you,

    Fredrik

    Thursday, November 6, 2008 6:07 PM
  • User-1190926628 posted

    Is your original file a jpeg?  If so, I found this:

    http://www.sentex.ca/~mwandel/jhead/

    http://www.irfanview.com/

    jhead is a command line tool, which you may be able to embed in your application.

    Irfanview has a full blown UI and does batch processing as well.

    I haven't used them myself so I don't know how they'd work for you.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 7, 2008 3:14 PM