简体   繁体   English

缩小图像尺寸,同时保持质量

[英]Reduce image size while maintaining quality

How to reduce the size of images to 40 KB while maintaining quality, and the dimensions of images are greater than 400 px , in C# ? 在C#中,如何在保持质量的同时将图像大小减小到40 KB,并且图像尺寸大于400 px?

Note, was used: 注意,被使用:

  • image.GetThumbnailImage : (The size is appropriate but the dimensions are small, and the larger the dimensions the bigger the size) image.GetThumbnailImage :(大小合适,但尺寸很小,尺寸越大,尺寸越大)

  • Drawing : (Large size, larger than 700 KB) 绘图:(大尺寸,大于700 KB)

Code: 码:

System.Drawing.Bitmap newImage = new System.Drawing.Bitmap(newWidth, newHeight); 
using (System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(newImage))
{
    gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
    gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
    gr.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
    gr.DrawImage(im3age, new System.Drawing.Rectangle(new System.Drawing.Point(0, 0), new System.Drawing.Size(newWidth, newHeight)));
    newImage.Save(FullPhat, System.Drawing.Imaging.ImageFormat.Jpeg);
}

Your solution is going to be based purely on what image format you choose, what compression options you select, and most importantly, the nature of the image itself. 您的解决方案将完全基于您选择的图像格式,选择的压缩选项以及最重要的是图像本身的性质。

The popular image formats for this task would be either JPEG or PNG. 用于此任务的流行图像格式为JPEG或PNG。 Both of those formats use, among other things, clusters of color in the images to achieve their compression. 除别的以外,这两种格式都使用图像中的颜色簇来实现其压缩。 That means if your image is largely the same or similar colors, you will get small file sizes, and if your image has a lot of distributed colors, the file sizes are going to be larger. 这意味着,如果您的图像颜色大致相同或相似,则文件大小会变小;如果图像的颜色分散很多,则文件大小会变大。

Here are some examples: 这里有些例子:

(Images saved with Photoshop. PNGs saved using highest compression levels, JPEGs saved using default level of quality "8", highest level of quality "12", and lowest level of quality "1".) (使用Photoshop保存的图像。使用最高压缩级别保存的PNG,使用默认质量级别“ 8”,最高质量级别“ 12”和最低质量级别“ 1”保存的JPEG。)

单色

  • PNG: 4 KB PNG:4 KB
  • JPEG (8): 9 KB JPEG(8):9 KB
  • JPEG (12): 15 KB JPEG(12):15 KB
  • JPEG (1): 5 KB JPEG(1):5 KB

纯净的噪音

  • PNG: 141 KB PNG:141 KB
  • JPEG (8): 378 KB JPEG(8):378 KB
  • JPEG (12): 590 KB JPEG(12):590 KB
  • JPEG (1): 146 KB JPEG(1):146 KB

性质

  • PNG: 341 KB PNG:341 KB
  • JPEG (8): 84 KB JPEG(8):84 KB
  • JPEG (12): 274 KB JPEG(12):274 KB
  • JPEG (1): 22 KB JPEG(1):22 KB

As you can see, different kinds of images at the same resolution produce wildly different file sizes, and JPEG is not always better than PNG or vice versa. 如您所见,相同分辨率的不同种类的图像产生的文件大小差异很大,JPEG并不总是比PNG更好,反之亦然。 Some of the JPEG images dip below your criteria, but keep in mind that they are saved with essentially the poorest quality settings, so the image is going to come out looking like garbage. 有些JPEG图像低于您的标准,但是请记住,它们的保存本质上是最差的质量设置,因此图像看起来像垃圾。

The general rule, then, is that a 400x400 px image, saved with perfect lossless quality, in a file under 40 KB... unless the image is mostly a single flat color, that isn't going to happen. 那么,通常的规则是,以40 KB以下的文件格式保存400x400 px的图像,并以完美的无损质量保存……除非该图像主要是单一的纯色,否则不会发生这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM