views:

384

answers:

2

In Windows Forms Application, I'm trying to open the image (CMYK tiff), add text, and then save back to CMYK tiff image, but when I opened the output image in Photoshop, it was RGB image (the colors looked different from the input image). Following is the code and I appreciate if you could help me.

Image^ chartImg = Image::FromFile( "user_chart.tif" );   
Graphics^ g = System::Drawing::Graphics::FromImage(chartImg);

String^ drawString = "Test test test test";
System::Drawing::Font^ drawFont = gcnew System::Drawing::Font("Arial", 9);
System::Drawing::SolidBrush^ drawBrush = gcnew             
System::Drawing::SolidBrush(System::Drawing::Color::Black);
float x = 100.0F;
float y = 10.0F;
System::Drawing::StringFormat^ strFormat = gcnew System::Drawing::StringFormat();
g->DrawString(drawString, drawFont, drawBrush, x, y, strFormat);

chartImg->Save("user_chart2.tif", System::Drawing::Imaging::ImageFormat::Tiff);
A: 

This is how I tried, but the result was the same. Your comment is appreciated.

In my main:

chartImg->Save("user_chart2.tif", getEncoderInfo(), GetTiffSaveAsCMYK());

static ImageCodecInfo^ getEncoderInfo() {

   array<ImageCodecInfo^>^ codecs = ImageCodecInfo::GetImageEncoders();

   int numCodecs = codecs->GetLength(0);

   for (int i=0; i < numCodecs; i++)

       if (codecs[i]->MimeType == "image/tiff") 

         return codecs[i];
}

static EncoderParameters^ GetTiffSaveAsCMYK() {

   EncoderParameters^ retval = gcnew EncoderParameters(1);
   Encoder^ cmyk = gcnew Encoder(Guid("a219bbc9-0a9d-4005-a3ee-3a421b8bb06c"));
   retval->Param[0] = gcnew EncoderParameter(cmyk, (short)1);
   return retval;
}
Bongsun Lee
The cast in the EncoderParameter constructor is wrong. It must be Int64. Is there any way to see a TIFF is saved in CMYK with standard tools?
Hans Passant
A: 

What you are attempting to do is impossible with WinForms and GDI+. It does not contain the proper encoders/decoders to handle CMYK TIFFs.

When a CMYK TIFF is opened it has to be converted to RGB, changes made - and then converted back to CMYK.

The CMYK color space is device dependent and thus you would need to use LibTIFF and LittleCMS to read the CMYK TIFF into a buffer, convert it to RGB using an appropriate profile, add the text and convert it back. All of this requires the use ICC or ICM profiles.

It is possible to write code that will write directly to a CMYK TIFF however they are stored in "planes" of C, M, Y, K data.

Resort to a solution that uses C++ and LibTIFF/LittleCMS and you will have better luck.

Perhaps another option is WPF which I believe opens additional doors with various image encoder/decoders but I'm not expert on that.

According to nobugz there is now a CMYK encoder/decoder. I haven't tried it myself. I'll stick with LibTIFF and LittleCMS.