【问题标题】:What is the maximum dimensions of an image that can't be greater than 8000 bytes?不能大于 8000 字节的图像的最大尺寸是多少?
【发布时间】:2013-10-16 21:18:31
【问题描述】:

我需要允许我的用户上传图片,我需要为此显示缩略图。我需要确保缩略图不大于 8000 字节。

我正在使用nQuant(产生高质量 256 色 8 位 PNG 图像的颜色量化器)来量化图像并将其缩小为 8 位图像,而不是 32 位,从而大大减小了文件大小。

我想知道的是,始终小于 8000 字节的图像的最大尺寸是多少?

目前我使用 96 x 96 作为我的最大尺寸并且我没有超过 8000 字节的限制,但我不知道这是否是因为我正在测试转换器的源图像(194 张随机图片在我的 HD 上),或由于其他原因。

我现在正在考虑它,我想知道是否考虑到这一点

96 * 96 = 9216 (bytes)

假设我的推理在数学上是错误的,这是否正确?

我是否应该考虑将最大尺寸减小到

89 * 89 = 7921 (bytes)

作为参考,这是转换器:

var fileSystemInfos = new DirectoryInfo(sourcePath).GetFiles();
var i = 0;
var total = fileSystemInfos.Count();
foreach (var file in fileSystemInfos)
{
    using (var inputStream = new FileStream(file.FullName, FileMode.Open))
    using (var memoryStream = new MemoryStream())
    {
        using (var sourceBitmap = new Bitmap(inputStream))
        {
            var img = ResizeImage(sourceBitmap, 96, 96);

            QuantizeImage(img, memoryStream);

            var outputName = file.Name.Replace("JPG", "png");
            using (var outputStream = new FileStream(Path.Combine(outputPath, outputName), FileMode.Create))
            {
                memoryStream.Seek(0, SeekOrigin.Begin);
                memoryStream.CopyTo(outputStream);
            }
        }

    }

    Console.WriteLine(++i + " of " + total);
}

private static void QuantizeImage(Bitmap bmp, MemoryStream outputFile)
{
    var quantizer = new WuQuantizer();
    using (var quantized = quantizer.QuantizeImage(bmp))
    {
        try
        {
            quantized.Save(outputFile, ImageFormat.Png);
        }
        catch (System.Exception ex)
        {
            // deal with error
        }
    }
}

public static Bitmap ResizeImage(Bitmap image, int maxWidth, int maxHeight)
{
    int originalWidth = image.Width;
    int originalHeight = image.Height;

    // To preserve the aspect ratio
    float ratioX = (float)maxWidth / (float)originalWidth;
    float ratioY = (float)maxHeight / (float)originalHeight;
    float ratio = Math.Min(ratioX, ratioY);

    // New width and height based on aspect ratio
    int newWidth = (int)(originalWidth * ratio);
    int newHeight = (int)(originalHeight * ratio);

    // Convert other formats (including CMYK) to RGB.
    //Bitmap newImage = new Bitmap(newWidth, newHeight, PixelFormat.Format24bppRgb);
    Bitmap newImage = new Bitmap(newWidth, newHeight, PixelFormat.Format32bppArgb);

    // Draws the image in the specified size with quality mode set to HighQuality
    using (Graphics graphics = Graphics.FromImage(newImage))
    {
        graphics.CompositingQuality = CompositingQuality.HighQuality;
        graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
        graphics.SmoothingMode = SmoothingMode.HighQuality;
        graphics.DrawImage(image, 0, 0, newWidth, newHeight);
    }

    return newImage;
}

【问题讨论】:

    标签: image png


    【解决方案1】:

    对于压缩最坏的情况是位图根本不会被压缩,你会得到 gzip 和 PNG 数据结构的开销(最小开销是 67 字节)。

    您正确地推断位图大小是宽度 * 高度。

    256 色 RGBA 调色板将占用 1KB + 几个字节的块开销。编码器可以放置其他元数据,如 gamma/sRGB/ICC 块,因此 PNG 中的垃圾数量理论上是无限的(您可以创建占用兆字节的 1x1px 图像)。

    假设您的编码器没有将不必要的数据放入文件中,您应该为 PNG8 开销保留大约 1120 个字节,因此最大位图大小为 82x82px。

    【讨论】:

    • 是的,我的意思是压缩后。我对这些东西的细节并不是很熟悉,所以我很难确切地问出我想要了解什么。例如,对于我的 194 张图像,压缩后最大的是 7539 字节。为什么与其他文件相比,它是最大的文件大小,是什么阻止它大于 8000 字节?只是运气,还是压缩后的图像永远不会超过 8000 字节?
    • 我测试了大约 4600 张 82x82 的图片,所有图片的最大文件大小为 7763 字节。足够好!!
    猜你喜欢
    • 2013-04-13
    • 2011-03-23
    • 2015-01-22
    • 2011-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-05
    • 1970-01-01
    相关资源
    最近更新 更多