图像的最大尺寸不能超过8000字节?

时间:2013-10-16 12:52:01

标签: image png

我需要允许我的用户上传图片,我需要为其显示缩略图。我需要确保缩略图不超过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;
}

1 个答案:

答案 0 :(得分:0)

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

您可以正确地确定位图大小是宽*高。

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

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

相关问题