[Question post] compresses the image size to smaller than the specified Kb

Source: Internet
Author: User

This function is available for PS and QQ images. The image size is compressed to smaller than the specified kb.

So far, I have also come to shanzhai to control the image size. The usual solution is to control the resolution and quality.

Assuming that the size of the last compression is kb, the image quality is as high as possible while ensuring that the size is not greater than kb.The higher the image quality, the larger the image size.. However, there is no fixed formula for the relationship between size and quality, such as y = nx, and I have also tried to save the pictures saved by the win7 system ten times each, from 10 to 100, we can only draw the conclusion that the image quality is high and the occupied size is large.

In this case, we can only find the most suitable quality parameter that satisfies the size of 1 ookb. The binary method is used for searching.

                                                                         MemoryStream Zip(Image img, ImageFormat format,  targetLen,  srcLen =               nearlyLen =              ms =  ( ===            targetLen *=  (targetLen >==              exitLen = targetLen -             quality = ()Math.Floor( * targetLen / parms =  EncoderParameters(            ImageCodecInfo formatInfo =  encoders = (ImageCodecInfo icf  (icf.FormatID ===             startQuality = endQuality = = (startQuality + endQuality) /  (                parms.Param[] =                 ms.SetLength(=                  (ms.Length >= exitLen && ms.Length <=  (startQuality >= endQuality)   (ms.Length < exitLen) = =                 newQuality = (startQuality + endQuality) /  (newQuality ===

During the test, it is found that the processing time of each graph is almost 1 s. There is no problem in completing the requirement, but I always feel that there should be a more appropriate solution. If you have any friends, please kindly advise and discuss it together!

Download test code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.