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

[{'answer_id': 41672, 'author': 'GateKiller', 'author_id': 383, 'author_profile': 'https://Stackoverflow.com/users/383', 'pm_score': 0, 'selected': False, 'text': '<p>You can try:</p>\n\n<pre><code>Bitmap.InterpolationMode = InterpolationMode.HighQualityBicubic;\n</code></pre>\n\n<p>and</p>\n\n<pre><code>Bitmap.CompositingQuality = CompositingQuality.HighQuality;\n</code></pre>\n\n<p>Which does keep the quality fairly high, but not the highest possible.</p>\n'}, {'answer_id': 41677, 'author': 'James Ogden', 'author_id': 3198, 'author_profile': 'https://Stackoverflow.com/users/3198', 'pm_score': 1, 'selected': False, 'text': "<p>Fundamentally you won't be able to keep the same quality because jpg is (so far as I'm aware) always lossy even with the highest possible quality settings.</p>\n\n<p>If bit-accurate quality is really important, consider using png, which has some modes which are lossless.</p>\n"}, {'answer_id': 41684, 'author': 'aku', 'author_id': 1196, 'author_profile': 'https://Stackoverflow.com/users/1196', 'pm_score': 7, 'selected': True, 'text': '<pre><code>var qualityEncoder = Encoder.Quality;\nvar quality = (long)&lt;desired quality&gt;;\nvar ratio = new EncoderParameter(qualityEncoder, quality );\nvar codecParams = new EncoderParameters(1);\ncodecParams.Param[0] = ratio;\nvar jpegCodecInfo = &lt;one of the codec infos from ImageCodecInfo.GetImageEncoders() with mime type = "image/jpeg"&gt;;\nbmp.Save(fileName, jpegCodecInfo, codecParams); // Save to JPG\n</code></pre>\n'}, {'answer_id': 43258, 'author': 'paan', 'author_id': 2976, 'author_profile': 'https://Stackoverflow.com/users/2976', 'pm_score': 1, 'selected': False, 'text': '<p>Just want to say that JPEG is by nature a lossy format. So in thoery even at the highest settings you are going to have some information loss, but it depends a lot on the image.But png is lossless.</p>\n'}, {'answer_id': 2412375, 'author': 'jestro', 'author_id': 417811, 'author_profile': 'https://Stackoverflow.com/users/417811', 'pm_score': 5, 'selected': False, 'text': '<pre><code>public static class BitmapExtensions\n{\n    public static void SaveJPG100(this Bitmap bmp, string filename)\n    {            \n        EncoderParameters encoderParameters = new EncoderParameters(1);\n        encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);\n        bmp.Save(filename, GetEncoder(ImageFormat.Jpeg), encoderParameters);\n    }\n\n    public static void SaveJPG100(this Bitmap bmp, Stream stream)\n    {\n        EncoderParameters encoderParameters = new EncoderParameters(1);\n        encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);\n        bmp.Save(stream, GetEncoder(ImageFormat.Jpeg), encoderParameters);\n    }\n\n    public static ImageCodecInfo GetEncoder(ImageFormat format)\n    {\n        ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();\n\n        foreach (ImageCodecInfo codec in codecs)\n        {\n            if (codec.FormatID == format.Guid)\n            {\n                return codec;\n            }\n        }\n\n        return null;\n    }\n}\n</code></pre>\n'}, {'answer_id': 8703634, 'author': 'net_prog', 'author_id': 355264, 'author_profile': 'https://Stackoverflow.com/users/355264', 'pm_score': 4, 'selected': False, 'text': '<p>Provided BitmapExtensions by jestro are great, I used them. However would like to show the corrected version - works for Image parent class which is more convenient as I think and provides a way to supply quality:</p>\n\n<pre><code>public static class ImageExtensions\n{\n    public static void SaveJpeg(this Image img, string filePath, long quality)\n    {\n        var encoderParameters = new EncoderParameters(1);\n        encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, quality);\n        img.Save(filePath, GetEncoder(ImageFormat.Jpeg), encoderParameters);\n    }\n\n    public static void SaveJpeg(this Image img, Stream stream, long quality)\n    {\n        var encoderParameters = new EncoderParameters(1);\n        encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, quality);\n        img.Save(stream, GetEncoder(ImageFormat.Jpeg), encoderParameters);\n    }\n\n    static ImageCodecInfo GetEncoder(ImageFormat format)\n    {\n        ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();\n        return codecs.Single(codec =&gt; codec.FormatID == format.Guid);\n    }\n}\n</code></pre>\n'}, {'answer_id': 32166157, 'author': 'Jeff R', 'author_id': 4161426, 'author_profile': 'https://Stackoverflow.com/users/4161426', 'pm_score': 1, 'selected': False, 'text': '<p>I am working on an expense report app, and I am really pleased with the default quality settings for JPG (and PNG) when saving from a Bitmap object.</p>\n\n<p><a href="https://msdn.microsoft.com/en-us/library/9t4syfhh%28v=vs.110%29.aspx" rel="nofollow">https://msdn.microsoft.com/en-us/library/9t4syfhh%28v=vs.110%29.aspx</a></p>\n\n<pre><code>Bitmap finalBitmap = ....; //from disk or whatever\nfinalBitmap.Save(xpsFileName + ".final.jpg", ImageFormat.Jpeg);\nfinalBitmap.Save(xpsFileName + ".final.png", ImageFormat.Png);\n</code></pre>\n\n<p>I\'m on .NET 4.6...perhaps the quality has improved in subsequent framework releases.</p>\n'}]
['https://Stackoverflow.com/questions/41665', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/191/']