简体   繁体   中英

Make sure that an image larger than 5 mb is reduced below that size c#

I'm using ImageProcessor to reduce the resolution or quality of an image, but I'm don't know how to make sure that the image resultant size it's below 5 megabytes. I tried setting the image dimensions to 3840-2160 but I want to use a better option.

Here it's my code:

private static byte[] redimensionImage(ref byte[] photoBytes)
    {
        var byteCuantity = ConvertBytesToMegabytes(photoBytes.Count());
        ISupportedImageFormat format = new JpegFormat();

        using (MemoryStream inStream = new MemoryStream(photoBytes))
        {
            using (MemoryStream outStream = new MemoryStream())
            {
                // Initialize the ImageFactory using the overload to preserve EXIF metadata.
                using (ImageFactory imageFactory = new ImageFactory(preserveExifData: true))
                {
                    // Load, resize, set the format and quality and save an image.
                    using (var imageProcessor = imageFactory.Load(inStream))
                    {
                        var originalHeight = imageProcessor.Image.Size.Height;
                        var originalWidth = imageProcessor.Image.Size.Width;

                        //calculate aspect ratio
                        var aspect = originalWidth / (float)originalHeight;
                        int newWidth, newHeight;

                        var dimenssionTooSmall = false;
                        if (originalWidth <= originalHeight && originalWidth < 100)
                        {
                            //calculate new dimensions based on aspect ratio
                            newHeight = (int)(100 / aspect);
                            var resizeLayer = new ResizeLayer(new Size(100, newHeight), ResizeMode.Min);
                            imageProcessor.Resize(resizeLayer);
                            dimenssionTooSmall = true;
                        }
                        else if (originalHeight < originalWidth && originalHeight < 100)
                        {
                            //calculate new dimensions based on aspect ratio
                            newWidth = (int)(100 / aspect);
                            var resizeLayer = new ResizeLayer(new Size(newWidth, 100), ResizeMode.Min);
                            imageProcessor.Resize(resizeLayer);
                            dimenssionTooSmall = true;
                        }

                        if (byteCuantity > 1 || dimenssionTooSmall)
                        {
                            //format.Quality = 6;

                            imageProcessor.Resize(new ResizeLayer(new Size(3840, 2160), ResizeMode.Min));

                            imageProcessor.Format(format);
                            imageProcessor.Save(outStream);
                            return outStream.ToArray();
                        }
                        else
                        {
                            return inStream.ToArray();
                        }
                    }
                }


            }
        }
    }

Thanks and regards.

Unfortunately there's no way you can really do this without reprocessing unless you're saving as bitmap.

When you save an image there are many compression processes that take place to store the image in each individual format (except bitmap which doesn't compress the image). Without actually going through the process itself you can't predict the file size.

You could potentially create your own lookup tables to act as a guideline by resizing a large sample of images in different formats and collecting the output sizes to give you a rough estimate for future processing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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