0

I have an image with the color format YCbCr (CMYK). When I load this with the .NET bitmap and save the result, the color format will change to RBG and the filze increase from ~1.600 KB to ~7.500 KB.

using (Image bitmap = Image.FromFile(@"C:\Test\Original.tif"))
{
    bitmap.Save(@"C:\Test\result.tif");
}

The sample file is a multi frame Tiff file with the color format YCbCr (CMYK) and it is 24 bit color deep. Its a Tiff container that includes two JPEG files with no compression.

Here you can download the sample file: https://ufile.io/9x21n

I already tried a OpenCV wrapper Nuget Package (https://github.com/shimat/opencvsharp), but the result is the same.

Mat imageForProcessing = Cv2.ImRead(@"C:\Test\Original.tif");
Cv2.ImWrite(@"C:\Test\result.tif", imageForProcessing);

Is there a free third party library, a .NET or OpenCV way to read and write the image without lossing the color format and increasing the file size five times?

Update 1

I tried the TiffBitmapEncoder class with the following source code. The bit deep is now correct (decreases the result from ~7.500 KB to ~6.400 KB), but the output is againg in RGB.

FileStream stream = new FileStream(@"C:\Test\original.tif", FileMode.Open);
TiffBitmapEncoder encoder = new TiffBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(stream));
encoder.Save(stream);

I would appreciate any help!

Regards Sascha

Sago
  • 75
  • 8
  • 3
    I think you should look into the [tif encoder parameters](https://learn.microsoft.com/en-us/dotnet/api/system.windows.media.imaging.tiffbitmapencoder?view=netframework-4.7.2). - As it stands the question is off-topic. – TaW Sep 04 '18 at 09:18
  • I checked this, but had the same issue. See update above. Any other suggestion? – Sago Sep 04 '18 at 09:29
  • 1
    You basically leave the encoder with its default values. I think to write in [CMYK](https://learn.microsoft.com/en-us/dotnet/api/system.windows.media.pixelformats.cmyk32?view=netframework-4.7.2#System_Windows_Media_PixelFormats_Cmyk32) you also need to set the [colorcontext](https://learn.microsoft.com/en-us/dotnet/api/system.windows.media.colorcontext?view=netframework-4.7.2) – TaW Sep 04 '18 at 09:36
  • I tried to change the color context, but I got the exception "The method is not supported" when I try to initalizes the following source code with the PixelFormat Cmyk32. **ColorContext colorContext = new ColorContext(PixelFormats.Cmyk32);** Changing the PixelFormat to RGB works. Any idea why I got this exception? Can't find a hint by my researches. – Sago Sep 04 '18 at 10:01
  • What are you targetting: Winforms, WPF, ASP..? The link were for Windows.Media. Do you use this or GDI+ ? Did yopu see [this](https://stackoverflow.com/questions/623636/net-tiff-file-rgb-to-cmyk-conversion-possible-without-a-third-party-library) ? – TaW Sep 04 '18 at 10:05
  • I neither target Winforms, WPF or ASP. In our company we have a custom C# .NET framework with a lot of image functionality. All of them have the above described problem, that the filesize expand and the CMYK color format will be lost. At the moment we are using GDI+ but we are open for new ways. I already tried the source code of the other topic, but it only changed the color of the image itself. If you check the details of the result it has no color info. – Sago Sep 04 '18 at 10:59
  • I don't know what you did wrong, but my test showed that the code in the link (Jorg's answer) works fine. I set up a Winforms program, added a few references and fed in a cmyk. The saved version is small and cmyk, just as expected. The size even got smaller, but that depends on the params of the original save. I had used a medium jpg qualtiy compression in photoshop.. – TaW Sep 04 '18 at 13:10

0 Answers0