如何在c#中压缩png图像 [英] How to compress png image in c#

查看:1127
本文介绍了如何在c#中压缩png图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想压缩PNG图像输出只是在png



我使用下面的代码:(以下代码适用于jpeg,jpg)但不适用于png。



我的尝试:



I want to compress PNG image output just be in png

I am using below code: (Below code is working fine for jpeg,jpg) but not for png.

What I have tried:

private void VaryQualityLevel()
{
    // Get a bitmap.
    Bitmap bmp1 = new Bitmap(@"c:\TestPhoto.jpg");
    ImageCodecInfo jpgEncoder = 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);

    EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 50L);
    myEncoderParameters.Param[0] = myEncoderParameter;
    bmp1.Save(@"c:\TestPhotoQualityFifty.jpg", jpgEncoder, myEncoderParameters);
}

private ImageCodecInfo GetEncoder(ImageFormat format)
{

    ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();

    foreach (ImageCodecInfo codec in codecs)
    {
        if (codec.FormatID == format.Guid)
        {
            return codec;
        }
    }
    return null;
}

推荐答案

JPEG使用有损压缩,而便携式网络图形 - 维基百科,免费的百科全书 [ ^ ]使用无损压缩(DEFLATE算法)。因此,它没有质量参数(质量总是100%)。



但是,PNG图像的大小可能会通过一些方法来优化,比如使用颜色调色板而不是RGB值。但.NET类不支持此类操作。另请参阅列出所有编码器的参数和值(Windows) [<}的结尾处的输出a href =https://msdn.microsoft.com/en-us/library/ms533846.aspxtarget =_ blanktitle =New Window> ^ ]。
JPEG uses lossy compression while Portable Network Graphics - Wikipedia, the free encyclopedia[^] uses a lossless compression (DEFLATE algorithm). Therefore, it has no quality parameter (quality is always 100 %).

However, the size of PNG images may be optimised by some methods like using a palette of colours instead of RGB values. But such operations are not supported by the .NET classes. See also the output at the end of Listing Parameters and Values for All Encoders (Windows)[^].


这篇关于如何在c#中压缩png图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆