压缩图像而不损失其质量 c#?

huangapple go评论78阅读模式
英文:

Compress the image without sacrificing its quality c#?

问题

如何在不牺牲图像质量的情况下压缩图像,但在将压缩后的图像复制到另一个文件夹时需要减小文件大小..?

string compressedFolderPath = @"C:\Users\Administrator\Desktop\VR Tasks\CompressiMG";
string compressedImagePath = Path.Combine(compressedFolderPath, DateTime.Now.ToString("yyyyMMddhhmmssfff") + ".jpg");

using (Image originalImage = Image.FromFile(imagePath))
{
    EncoderParameters encoderParameters = new EncoderParameters(0);
    encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 50L);

    ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg");

    originalImage.Save(compressedImagePath, jpegCodec, encoderParameters);
}

return compressedImagePath;

但这里缺少质量,而且我已经尝试过使用Zip文件、Gzip文件和Base64作为文本文件,但文件大小并没有减小很多。

英文:

How I can compress image without sacrificing its quality but i need to reduce the size whenever i copied compressed image to the another folder..?

string compressedFolderPath = @"C:\Users\Administrator\Desktop\VR Tasks\CompressiMG";
string compressedImagePath = Path.Combine(compressedFolderPath, DateTime.Now.ToString("yyyyMMddhhmmssfff") + ".jpg");

             using (Image originalImage = Image.FromFile(imagePath))
             {
                 EncoderParameters encoderParameters = new EncoderParameters(0);
                 encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 50L);

                 ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg");

                 originalImage.Save(compressedImagePath, jpegCodec, encoderParameters);
             }

             return compressedImagePath; 

But here quality is missing And also i have tried As Zipfile and Gzip File and Base64 as text file but size didn't reduced much.

答案1

得分: 3

图像具有一定数量的固有信息,或称为“质量”。有时我们称之为。然而,在大多数图像中,实际熵要小于原始数据大小。

无损压缩方法,如zip或png,试图将文件大小减小到接近这个理论限制,同时保留所有实际数据完整。

有损压缩方法在压缩步骤之前丢弃数据。它试图聪明地选择要丢弃的数据,但无论如何,都无法避免你会丢弃数据的事实。早晚你会注意到质量下降。如果你告诉JPEG编码器使用低质量值,你几乎肯定会注意到。

所以你的选择是:

  1. 接受较低的质量
  2. 接受较大的文件大小
  3. 使用更智能的有损压缩算法。JPEG已经老旧,有许多更新、更好的算法,可以在给定的文件大小下提供更好的主观质量。这里是几种较新格式的比较。但不要期望有什么魔法。你可能需要找到合适的库来编码和解码图像,因为.NET只包括一套基本的编码器。

如果你想继续使用JPEG,你可以尝试降低图像的分辨率。JPEG不擅长处理高压缩级别,所以在压缩之前降低分辨率可能会获得更好的结果。

英文:

Images have some amount of inherent information, or "quality". We sometimes call this entropy. However, in most images the actual entropy is smaller than the raw data size.

Lossless Compression methods like zip or png tries to reduce the file size as close to this theoretical limit, while retaining all the actual data exactly.

Lossy compression methods throw away data before the compression step. It tries to be smart about what data it throws away, but there is no way to get around the fact that you are throwing away data. Sooner or later you are going to notice a quality degradation. And if you are telling the jpeg encoder to use a low quality value you will almost certainly notice.

So your options are:

  1. Accept a lower quality
  2. Accept the larger file size
  3. Use a smarter lossy compression algorithm. Jpeg is old, and there are many newer, better, algorithms that provide better subjective quality for a given file size. Here is a comparison between a few newer formats. But do not expect any magic. You likely need to find appropriate libraries to encode and decode the images, since .net only include a basic set of encoders.

If you want to stick with jpeg you might want to experiment with lowering the resolution of your image. Jpeg does not handle high compression levels well, so you will likely get better results by reducing the resolution before compression.

huangapple
  • 本文由 发表于 2023年6月5日 15:03:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76404149.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定