Bmp to jpg/png in C#

前端 未结 7 1859
无人共我
无人共我 2020-11-29 01:41

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

相关标签:
7条回答
  • 2020-11-29 02:13

    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.

    0 讨论(0)
  • 2020-11-29 02:19

    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.

    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...perhaps the quality has improved in subsequent framework releases.

    0 讨论(0)
  • 2020-11-29 02:24

    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.

    If bit-accurate quality is really important, consider using png, which has some modes which are lossless.

    0 讨论(0)
  • 2020-11-29 02:25
    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
    
    0 讨论(0)
  • 2020-11-29 02:28
    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;
        }
    }
    
    0 讨论(0)
  • 2020-11-29 02:32

    You can try:

    Bitmap.InterpolationMode = InterpolationMode.HighQualityBicubic;
    

    and

    Bitmap.CompositingQuality = CompositingQuality.HighQuality;
    

    Which does keep the quality fairly high, but not the highest possible.

    0 讨论(0)
提交回复
热议问题