PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Printing RGB images as CMYK images into PDF
https://forum.pdfsharp.net/viewtopic.php?f=2&t=3064
Page 1 of 1

Author:  funkl [ Wed Feb 25, 2015 8:31 am ]
Post subject:  Printing RGB images as CMYK images into PDF

Hello,

in the wiki article http://www.pdfsharp.net/wiki/ColorsCMYK-sample.ashx?HL=cmyk you show how to print some primitive shapes into a PDF using the CMYK color space.
I have not yet found a way how to print images (not saved as physical files in the file system) from System.Drawing.Image into a PDF using the CMYK color space. Is there a way to do so after all? If yes, could you please add the necessary lines to the wiki entry from above?
The most confortable way would be telling PDFSharp to print an entire PDF converting RGB colors into the CMYK color space using a specific ICC profile for the conversion (well,... one may dream...)
How can i bring CMYK images from the memory into the PDF?

Regards,
f

Author:  funkl [ Tue Mar 10, 2015 10:21 am ]
Post subject:  Re: Printing RGB images as CMYK images into PDF

Hello again,
i found out, that embedding CMYK-Images into a PDF by using the command XGraphics.DrawImage(...) somehow converts the CMYK image (jpg) back into a RGB-Image.
Is there a way to embed CMYK images into a PDF without converting their CMYK colors (color space?) back into RGB colors (color space?) after all? Am i just too blind to find the possibility?

I use PDFSharp 1.31 (GDI) for generating PDFs (not upgraded yet because i have not found any reason to do so.)

Regards, funkl

Author:  Thomas Hoevel [ Tue Mar 10, 2015 11:03 am ]
Post subject:  Re: Printing RGB images as CMYK images into PDF

Hi!

Theoretically JPEG files will be embedded as they are without any conversions.
However some code that worked fine with Windows XP no longer works with Windows 8.1.
Open a CMYK JPEG with Windows XP and Windows will indicate the image is CMYK. Do the same with Windows 8.1 and Windows will tell you it is RGB. :?

Probably we will have to add code to test whether a JPEG file is CMYK or RGB. With PDFsharp 1.50 beta this is not yet implemented.
Information about how we can tell RGB, grayscale, or CMYK from the JPEG file headers (if available) are welcome.

We didn't find many CMYK JPEG images. Maybe there are different flavors that behave differently.
If you think there is an error in PDFsharp, feel free to provide an SSCCE with some code and a CMYK JPEG image for investigation.

Author:  funkl [ Tue Mar 10, 2015 12:35 pm ]
Post subject:  Re: Printing RGB images as CMYK images into PDF

The thing is, that i need to create PDFs converted to the CMYK color space (best case: by specifying an ICC profile for the color conversion).
My idea so far:
Code:
private void RenderImage(Image image, XGraphics graphics, double x, double y, double width, double height)
{
   Bitmap bitmap = new Bitmap(image);

   MemoryStream inStream = new MemoryStream();
   bitmap.Save(inStream, ImageFormat.Png);
   inStream.Position = 0;

   BitmapSource source = BitmapFrame.Create(inStream);

   FormatConvertedBitmap convertingImage = new FormatConvertedBitmap();
   convertingImage.BeginInit();
   convertingImage.Source = source;
   convertingImage.DestinationFormat = PixelFormats.Cmyk32;
   convertingImage.EndInit();

   BitmapEncoder encoder = new JpegBitmapEncoder();
   encoder.Frames.Add(BitmapFrame.Create(convertingImage));
   MemoryStream outStream = new MemoryStream();

   encoder.Save(outStream);
         
   XImage xImage = new XImage(outStream);
   xImage.Interpolate = false;
   graphics.DrawImage(xImage, x - 1, y - 1);
}

Writing the image like this results in some sort of "mashed" RGB image in the PDF. I already found some advice you offer in order to obtain a "regular" image, which i suppose would still be RGB.
When i use a FileStream instead of the MemoryStream in line 19, the image is exported as a CMYK image.
Is there eventually sg. wrong inside the XImage or the XGraphics?

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/