图像的最大尺寸不能超过8000个字节? [英] What is the maximum dimensions of an image that can't be greater than 8000 bytes?

查看:270
本文介绍了图像的最大尺寸不能超过8000个字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要允许我的用户上传图片,我需要为其显示缩略图。我需要确保缩略图不超过8000个字节。

I need to allow my users to upload images, for which I need to show a thumbnail. I need to ensure that the thumbnail is not greater than 8000 bytes.

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

I'm using nQuant (color quantizer producing high quality 256 color 8 bit PNG images) to quantize the image and reduce it to an 8 bit image, rather than 32 bit, thus drastically reducing the file size.

我想知道的是,图像的最大尺寸总是低于8000字节?

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

Currently I'm using 96 x 96 as my maximum dimensions and I have not exceeded the 8000 byte limit, but I don't know if this is because of the source images I'm testing the converter with (194 random pictures on my HD), or because of some other reason.

正如我现在想的那样,我想知道是否给出了

As I'm thinking about it now, I'm wondering if given that

96 * 96 = 9216 (bytes)

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

it's correct in assuming that my reasoning is mathematically wrong?

我应该考虑将最大尺寸减少到

Should I consider reducing the maximum dimensions to

89 * 89 = 7921 (bytes)

作为参考,这是转换器:

For reference, this is the converter:

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


推荐答案

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

For compression worst case is that bitmap won't be compressed at all, and you'll get overhead of gzip and PNG data structures (smallest overhead is 67 bytes).

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

You reason correctly that bitmap size is width * height.

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

256-color RGBA palette will take 1KB + few bytes of chunk overhead. Encoders can put other metadata like gamma/sRGB/ICC chunks, so amount of garbage in PNG is theoretically unbounded (you can create 1x1px image that takes megabytes).

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

Assuming your encoder doesn't put unnecessary data into the file you should reserve about 1120 bytes for PNG8 overhead, so maximum bitmap size is 82x82px.

这篇关于图像的最大尺寸不能超过8000个字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆