为 Web 应用程序创建合理文件大小的缩略图

发布于 2024-07-14 01:29:57 字数 2020 浏览 6 评论 0原文

我的源图像是 2288px x 1712px 和 816kb。 当我创建 1024 x 768 的缩略图时,文件大小是高分辨率源的两倍以上(1.13mb - 1.8mb,具体取决于所使用的方法)。

创建的源图像和缩略图是 .jpg

即使创建 299 x 224 缩略图也是 180kb,而如果我在 GIMP 中手动创建缩略图,它是 14kb。

我需要图像看起来不错,所以不能因为尺寸而牺牲质量。

我尝试过 Image.GetThumbnailImage 它创建了一个 1.13mb 文件,但我读到它可能会产生意想不到的结果。

大多数文章都说要编写像本文这样的代码: http://www.codeproject.com/KB/GDI-plus/imageresize。 aspx

这给了我 1.83mb 的缩略图。

与使用桌面应用程序执行相同的操作相比,我是否应该期望更大的文件大小?

我尝试了 16 位而不是 24 位,并注释掉了“高质量”值,但我仍然无法将其降至 1mb 以下。 有任何想法吗?

Bitmap bmPhoto = new Bitmap(destWidth, destHeight,PixelFormat.Format24bppRgb);
        bmPhoto.SetResolution(image.HorizontalResolution,image.VerticalResolution);

        Graphics grPhoto = Graphics.FromImage(bmPhoto);

        grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;        
        grPhoto.SmoothingMode = SmoothingMode.HighQuality;
        grPhoto.PixelOffsetMode = PixelOffsetMode.HighQuality;
        grPhoto.CompositingQuality = CompositingQuality.HighQuality;

编辑答案

我缺乏压缩。 我的 1024 缩略图现在是 112kb。

Encoder qualityEncoder = Encoder.Quality;
//80L is the compression percentage
EncoderParameter ratio = new EncoderParameter(qualityEncoder, 80L);
// Add the quality parameter to the list
EncoderParameters codecParams = new EncoderParameters(1);
codecParams.Param[0] = ratio;

ImageCodecInfo jpegCodecInfo = GetEncoderInfo("image/jpeg");


bmPhoto.Save(Path.Combine(filepath, RenameImage(filename, appendName, replaceWholeString)), jpegCodecInfo, codecParams);


ImageCodecInfo GetEncoderInfo(string mimeType)    
{    
int j;    
ImageCodecInfo[] encoders;    
encoders = ImageCodecInfo.GetImageEncoders();

for(j = 0; j < encoders.Length; ++j)    
{    
if(encoders[j].MimeType.ToUpper() == mimeType.ToUpper())    
return encoders[j];    
}    
return null;    
}

My source images are 2288px x 1712px and 816kb. When I create a thumbnail of 1024 x 768 the file sizes are more than double the high res source (1.13mb - 1.8mb depending on method used).

The source images and thumbnails created are .jpg

Even creating a 299 x 224 thumbnail is 180kb where as if I manually create a thumbnail in GIMP it is 14kb.

I need the images to look good so cannot compromise on quality for size.

I have tried Image.GetThumbnailImage which created a 1.13mb file but I read that it can create unexpected results.

Most articles say to write the code like this article:
http://www.codeproject.com/KB/GDI-plus/imageresize.aspx

That gives me a thumbnail of 1.83mb.

Should I be expecting larger file sizes compared to doing the same thing with a desktop application?

I have tried 16 bit instead of 24 and commenting out the "high quality" values but I still cannot get it under 1mb. Any ideas?

Bitmap bmPhoto = new Bitmap(destWidth, destHeight,PixelFormat.Format24bppRgb);
        bmPhoto.SetResolution(image.HorizontalResolution,image.VerticalResolution);

        Graphics grPhoto = Graphics.FromImage(bmPhoto);

        grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;        
        grPhoto.SmoothingMode = SmoothingMode.HighQuality;
        grPhoto.PixelOffsetMode = PixelOffsetMode.HighQuality;
        grPhoto.CompositingQuality = CompositingQuality.HighQuality;

EDIT FOR ANSWER

I was lacking compression. My 1024 thumbnail is now 112kb.

Encoder qualityEncoder = Encoder.Quality;
//80L is the compression percentage
EncoderParameter ratio = new EncoderParameter(qualityEncoder, 80L);
// Add the quality parameter to the list
EncoderParameters codecParams = new EncoderParameters(1);
codecParams.Param[0] = ratio;

ImageCodecInfo jpegCodecInfo = GetEncoderInfo("image/jpeg");


bmPhoto.Save(Path.Combine(filepath, RenameImage(filename, appendName, replaceWholeString)), jpegCodecInfo, codecParams);


ImageCodecInfo GetEncoderInfo(string mimeType)    
{    
int j;    
ImageCodecInfo[] encoders;    
encoders = ImageCodecInfo.GetImageEncoders();

for(j = 0; j < encoders.Length; ++j)    
{    
if(encoders[j].MimeType.ToUpper() == mimeType.ToUpper())    
return encoders[j];    
}    
return null;    
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

毁梦 2024-07-21 01:29:57

这听起来像是您没有使用压缩或使用非常弱的压缩(例如 RLE)。 尝试 PNG、JPG(或 JPG2000,可以是无损的)或 GIF。

编辑:如果您使用 JPG,请尝试降低图像质量。 1024x768 缩略图应该比 1 MB 小很多,但看起来仍然不错。

This sounds like you're using no compression or a very weak one (like RLE). Try PNG, JPG (or JPG2000 which can be lossless) or GIF.

EDIT: If you use JPG, try to reduce image quality. 1024x768 thumbnails should be able to be a lot smaller than 1 MB and still looking good.

2024-07-21 01:29:57

位图格式是您的主要问题。 与其他格式相比,这总是巨大的。 位图是游程编码且未压缩的。

如果您的图像是照片,那么 JPEG 是您的最佳选择。 您可以通过较小的文件大小获得出色的视觉质量。 如果它们是线条艺术并且没有太多颜色,GIF 也可能是一个不错的选择。 有几个很好的图像处理库,例如 FreeImage

编辑:注意到您要将 JPEG 转换为 JPEG 的评论...缺乏良好的压缩可能是框架内置图像功能的限制。

The bitmap format is your main problem. This will always be huge by comparison with other formats. Bitmaps are run-length-encoded and uncompressed.

If your images are photographs, JPEG is your best bet. You can get great visual quality with tiny file sizes. If they are line art and don't have a lot of color, GIF may also be a good option. There are several good image manipulation libraries out there, such as FreeImage.

EDIT: Noticed the comment that you're going JPEG to JPEG... the lack of good compression may be a limitation of the Framework's built-in image capabilities.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文