【发布时间】: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