C# Image Save as CMYK

2.5k views Asked by At

Please see my code below. I'm saving an image. My input image color mode is CMYK, but once I saved, it become RGB and rest. I just want to do following things:

  1. I just want to keep it to CMYK color mode.
  2. I just want to keep same input quality.
  3. I just want to keep same width and height as input.

        Bitmap bmp = new Bitmap(Image.FromFile(@"D:\input.jpg"));            
        Graphics gr = Graphics.FromImage(bmp);
    
        using (Font myFont = new Font("Arial", 42))
        {
            gr.DrawString("Hello!", myFont, Brushes.Green, new Point(2, 2));
            gr.DrawString(textBox1.Text, myFont, Brushes.Green, new Point(700, 750));
        }
        ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);
        System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
        EncoderParameters myEncoderParameters = new EncoderParameters(1);
    
        EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 100L);
        myEncoderParameters.Param[0] = myEncoderParameter;
        bmp.Save(@"D:\test.jpg", jgpEncoder, myEncoderParameters);
    
1

There are 1 answers

1
Kevin On

The act of converting the image to a bitmap so that you can manipulate it

Bitmap bmp = new Bitmap(Image.FromFile(@"D:\input.jpg"));

Does the conversion internally (the color pallette in a Bitmap is RGB). You will have to use a conversion algorithm (one of the ones in the link suggested by ArgentFire, or something else) before the save back to your file.

In response to your comment:

  1. Not going to happen. To perform the manipulation of the image you need, you will have to use a Bitmap, which does the conversion internally. (in .Net the Bitmap is the standard in-memory representation of an image)

  2. The image quality is going to be determined by the jpg encoder when you re-encode the Bitmap for saving. Jpeg uses a lossy compression so keeping the original image quality is doubtful.

  3. I don't see where you are doing anything that should alter the aspect ratio of the image.

You would probably be better off using a lossless image format like tiff. Start from a tiff, convert to Bitmap to manipulate, encode back to tiff to save. All lossless with respect to the image quality.