【问题标题】:Thumbnails and a photo album缩略图和相册
【发布时间】:2011-12-28 13:31:57
【问题描述】:

我正在为我的照片组合制作相册。但我在以下问题上质疑自己:

我应该将缩略图版本保存在服务器上的文件系统中,还是应该在请求缩略图图像时动态生成缩略图?

我想存储缩略图的优点是服务器上的处理较少,因为它生成了一次文件(当文件上传时)。但缺点是,如果有一天我决定缩略图的大小不对,我就有问题了。另外,我现在存储 2 个文件(拇指加原始文件)。

那么,缩略图有最佳尺寸吗?我想答案是 - 您希望缩略图存储多大。我的问题是,我的缩略图被调整为最大 150 高或最大 150 宽。但是,文件大小仍然达到 40k 左右。

我正在使用这个:

public void ResizeImage(string originalFile, string newFile, int newWidth, int maxHeight, bool onlyResizeIfWider)
    {
        System.Drawing.Image fullsizeImage = System.Drawing.Image.FromFile(MapPath(GlobalVariables.UploadPath + "/" + originalFile));

        // Prevent using images internal thumbnail
        fullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
        fullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);

        if (onlyResizeIfWider)
        {
            if (fullsizeImage.Width <= newWidth)
            {
                newWidth = fullsizeImage.Width;
            }
        }

        int newHeight = fullsizeImage.Height * newWidth / fullsizeImage.Width;
        if (newHeight > maxHeight)
        {
            // Resize with height instead
            newWidth = fullsizeImage.Width * maxHeight / fullsizeImage.Height;
            newHeight = maxHeight;
        }

        System.Drawing.Image newImage = fullsizeImage.GetThumbnailImage(newWidth, newHeight, null, IntPtr.Zero);

        // Clear handle to original file so that we can overwrite it if necessary
        fullsizeImage.Dispose();
        // Save resized picture
        newImage.Save(MapPath(GlobalVariables.UploadPath + "/" + newFile));
    }

有没有办法减小文件大小,因为 150 高/150 宽非常小。我想提高到 250 左右,但如果我显示 12 个拇指...加载需要一段时间。

【问题讨论】:

    标签: c# asp.net image-processing


    【解决方案1】:

    是的,绝对必须保存为缩略图文件,而不是一直处理。

    关于图像,尝试使其绝对适合 8x8 或 16x16 的数组块,因为这是 jpeg 用来分割和压缩它的大小。例如,不要将其设为 150x150,因为 150/8= 18.75,使用 152x152,因为 152/8=19

    那么减小文件大小和改变质量的属性是

    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
    g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    

    这是How to: Use Interpolation Mode to Control Image Quality During Scaling的示例

    【讨论】:

    • 感谢块信息。非常有帮助。就质量而言,我正在使用 System.Drawing.Image - 这个对象类型没有提到的属性。我应该改用位图,然后总是保存为 Jpeg,一旦使用了上面提到的那些属性?
    • @cdotlister 是的使用更好的更详细的缩略图程序,您可以控制更多的想法。
    • 谢谢 - 我不确定如何在我的位图上使用这些属性... BitmapObject.InterpolationMode 不起作用。
    猜你喜欢
    • 2012-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多