确保大于 5 mb 的图像缩小到该大小以下 c#

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

我正在使用 ImageProcessor 来降低图像的分辨率或质量,但我不知道如何确保图像的最终大小低于 5 兆字节。我尝试将图像尺寸设置为 3840-2160,但我想使用更好的选项。

这是我的代码:

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();
                        }
                    }
                }


            }
        }
    }

感谢和问候。

不幸的是,除非您另存为位图,否则您无法在不重新处理的情况下真正做到这一点。

当您保存图像时,会发生许多压缩过程以将图像存储为每种格式(不压缩图像的位图除外)。如果不实际执行该过程本身,您将无法预测文件大小。

您可以创建自己的查找表作为指导,方法是调整大量不同格式的图像样本并收集输出大小,以便为您提供未来处理的粗略估计。