在 C# 中 Bmp 到 jpg/png

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 09:51:17  来源:igfitidea点击:

Bmp to jpg/png in C#

提问by Ramesh Soni

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?

有没有办法将 bmp 图像转换为 jpg/png 而不损失 C# 中的质量?使用 Image 类我们可以将 bmp 转换为 jpg 但输出图像的质量很差。我们能否获得与使用最高质量的 photoshop 转换为 jpg 的图像一样好的质量水平?

采纳答案by aku

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

回答by GateKiller

You can try:

你可以试试:

Bitmap.InterpolationMode = InterpolationMode.HighQualityBicubic;

and

Bitmap.CompositingQuality = CompositingQuality.HighQuality;

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

这确实保持了相当高的质量,但不是最高的。

回答by James Ogden

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.

从根本上讲,您将无法保持相同的质量,因为 jpg(据我所知)即使使用最高的质量设置也总是有损的。

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

如果位精确质量真的很重要,请考虑使用 png,它具有一些无损模式。

回答by paan

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.

只想说 JPEG 本质上是一种有损格式。因此,理论上即使在最高设置下,您也会丢失一些信息,但这在很大程度上取决于图像。但 png 是无损的。

回答by jestro

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;
    }
}

回答by net_prog

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:

如果 jestro 的 BitmapExtensions 很棒,我就使用它们。但是想显示更正后的版本 - 适用于 Image 父类,我认为它更方便,并提供了一种提供质量的方法:

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);
    }
}

回答by Jeff R LangBoost

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.

我正在开发一个费用报告应用程序,当从位图对象保存时,我对 JPG(和 PNG)的默认质量设置非常满意。

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

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.

我在 .NET 4.6 上……也许在随后的框架版本中质量有所提高。