Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more: , +
Dear Experts, below is my code. I'm saving an image with add some contents to the image from DrawString. My problem is, once I saved the image, CMYK become RGB, image size is different, image dpi is different. Like that several input image configurations were changed.

I just want to keep the same input configuration and quality with modifying the content only. Please help me.

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(date.Text, myFont, Brushes.Black, new Point(700, 798));
gr.DrawString(shape.Text, myFont, Brushes.Black, new Point(700, 908));
gr.DrawString(meas.Text, myFont, Brushes.Black, new Point(700, 1031));
gr.DrawString(weight.Text, myFont, Brushes.Black, new Point(700, 1160));
}
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, 300L);
myEncoderParameters.Param[0] = myEncoderParameter;
bmp.Save(@"D:\test.jpg", jgpEncoder, myEncoderParameters);
Posted

1 solution

This is weird, but, first of all, eliminate new Bitmap(Image). Use
C#
Bitmap bmp = new Bitmap(fileName);

And if you really wanted to initialize a bitmap from some abstract Image instance when a file is not accessible, and if you also wanted to change the size and PixelFormat, you would need to initialize an empty bitmap with required properties, and then open an instance of Image separately, and draw in on your bitmap, using the Graphics instance you would obtain exactly the way you already did (called gr in your code). In this particular case, you just don't need it, because you can open a bitmap directly from a file, which should preserver the pixel format, size and all other properties. Please see:
https://msdn.microsoft.com/en-us/library/system.drawing.bitmap%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/0cbhe98f(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawimage%28v=vs.110%29.aspx[^].

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900