Bmp to jpg / png in C #

Is there a way to convert a BMP image to jpg / png without losing quality in C #? Using the Image class, we can convert bmp to jpg, but the quality of the output image is very poor. Can we get a quality level as good as an image converted to jpg using Photoshop with the highest quality?

+48
c # image-manipulation
03 Sep '08 at 13:29
source share
8 answers
var qualityEncoder = Encoder.Quality; var quality = (long)<desired quality>; var ratio = new EncoderParameter(qualityEncoder, quality ); var codecParams = new EncoderParameters(1); codecParams.Param[0] = ratio; var jpegCodecInfo = <one of the codec infos from ImageCodecInfo.GetImageEncoders() with mime type = "image/jpeg">; bmp.Save(fileName, jpegCodecInfo, codecParams); // Save to JPG 
+53
03 Sep '08 at 13:39
source share
 public static class BitmapExtensions { public static void SaveJPG100(this Bitmap bmp, string filename) { EncoderParameters encoderParameters = new EncoderParameters(1); encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L); bmp.Save(filename, GetEncoder(ImageFormat.Jpeg), encoderParameters); } public static void SaveJPG100(this Bitmap bmp, Stream stream) { EncoderParameters encoderParameters = new EncoderParameters(1); encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L); bmp.Save(stream, GetEncoder(ImageFormat.Jpeg), encoderParameters); } public static ImageCodecInfo GetEncoder(ImageFormat format) { ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders(); foreach (ImageCodecInfo codec in codecs) { if (codec.FormatID == format.Guid) { return codec; } } return null; } } 
+30
Mar 09 '10 at 20:37
source share

Assuming jestro's BitmapExtensions are great, I used them. However, I would like to show a fixed version - it works for the parent class Image, which is more convenient, as I think, and provides a way to ensure quality:

 public static class ImageExtensions { public static void SaveJpeg(this Image img, string filePath, long quality) { var encoderParameters = new EncoderParameters(1); encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, quality); img.Save(filePath, GetEncoder(ImageFormat.Jpeg), encoderParameters); } public static void SaveJpeg(this Image img, Stream stream, long quality) { var encoderParameters = new EncoderParameters(1); encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, quality); img.Save(stream, GetEncoder(ImageFormat.Jpeg), encoderParameters); } static ImageCodecInfo GetEncoder(ImageFormat format) { ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders(); return codecs.Single(codec => codec.FormatID == format.Guid); } } 
+10
Jan 02 '12 at 17:34
source share

In principle, you cannot maintain the same quality, because jpg (as far as I know) always loses even with the highest possible quality settings.

If the quality of bit accuracy is really important, consider using png, which has some lossless modes.

+1
Sep 03 '08 at 13:36
source share

I just want to say that JPEG is by its nature a lossy format. Thus, even with the highest settings, you will have some loss of information, but it depends a lot on the image. But png is lossless.

+1
Sep 04 '08 at 6:52
source share

You can try:

 Bitmap.InterpolationMode = InterpolationMode.HighQualityBicubic; 

and

 Bitmap.CompositingQuality = CompositingQuality.HighQuality; 

What makes the quality high enough, but not the maximum possible.

0
Sep 03 '08 at 13:33
source share

I am working on an expense report application and I am very pleased with the default quality settings for JPG (and PNG) when saving from a Bitmap object.

https://msdn.microsoft.com/en-us/library/9t4syfhh%28v=vs.110%29.aspx

 Bitmap finalBitmap = ....; //from disk or whatever finalBitmap.Save(xpsFileName + ".final.jpg", ImageFormat.Jpeg); finalBitmap.Save(xpsFileName + ".final.png", ImageFormat.Png); 

I'm on .NET 4.6 ... maybe the quality has improved in subsequent releases of frames.

0
Aug 23 '15 at 11:29
source share



All Articles