3

I have about 10000 png files of product images. I later learnt that the product images in production are jpeg and hence I need to convert my new 10000 png files to jpeg. What will happen to the quality of the image if I just change the extension from png to jpeg?

I work on C#/SQL Server environment and should I use

System.Drawing.Image image1 = System.Drawing.Image.FromFile(@"C:\test.png");
 // Save the image in JPEG format.
 image1.Save(@"C:\test.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

Any thoughts on this?

keenthinker
  • 7,645
  • 2
  • 35
  • 45
newbieCSharp
  • 181
  • 2
  • 22

1 Answers1

7

Jpeg, unlike PNG, is a lossy format. There will always be some decrease in quality.

You can make sure it's unnoticable or almost unnoticable by using the highest possible quality level when saving the Jpeg.

Taken from MSDN:

// Get a bitmap.
Bitmap bmp1 = new Bitmap(@"c:\TestPhoto.jpg");
ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);

// Create an Encoder object based on the GUID 
// for the Quality parameter category.
System.Drawing.Imaging.Encoder myEncoder =
    System.Drawing.Imaging.Encoder.Quality;

// Create an EncoderParameters object. 
// An EncoderParameters object has an array of EncoderParameter 
// objects. In this case, there is only one 
// EncoderParameter object in the array.
EncoderParameters myEncoderParameters = new EncoderParameters(1);

myEncoderParameter = new EncoderParameter(myEncoder, 100L);
myEncoderParameters.Param[0] = myEncoderParameter;
bmp1.Save(@"c:\TestPhotoQualityHundred.jpg", jgpEncoder, myEncoderParameters);

Also keep in mind that the Jpeg format does not save the alpha channel of the image.

This related question deals with the default quality level .NET uses: What quality level does Image.Save() use for jpeg files?

Community
  • 1
  • 1
Rotem
  • 21,452
  • 6
  • 62
  • 109