使用 GDI 调整大小时限制文件大小+

发布于 2024-08-06 07:40:07 字数 623 浏览 3 评论 0原文

我正在调整上传图像的大小,如下所示:

var bmPhoto = new Bitmap(width, height, PixelFormat.Format16bppRgb555);

using (var grPhoto = Graphics.FromImage(bmPhoto))
{
  grPhoto.SmoothingMode = SmoothingMode.HighSpeed;
  grPhoto.CompositingQuality = CompositingQuality.HighSpeed;
  grPhoto.InterpolationMode = InterpolationMode.Low;
  grPhoto.DrawImage(sourceImage, new Rectangle(destX, destY, destWidth, destHeight),
                        new Rectangle(0, 0, sourceWidth, sourceHeight), GraphicsUnit.Pixel);
}

如何限制生成的文件大小,使其不大于 3KB?我正在上传 1024x768 JPG,大小为 768KB。当使用上面的代码将大小调整为 100x100 时,我无法得到小于 12KB 的大小。

I am resizing uploaded images as follows:

var bmPhoto = new Bitmap(width, height, PixelFormat.Format16bppRgb555);

using (var grPhoto = Graphics.FromImage(bmPhoto))
{
  grPhoto.SmoothingMode = SmoothingMode.HighSpeed;
  grPhoto.CompositingQuality = CompositingQuality.HighSpeed;
  grPhoto.InterpolationMode = InterpolationMode.Low;
  grPhoto.DrawImage(sourceImage, new Rectangle(destX, destY, destWidth, destHeight),
                        new Rectangle(0, 0, sourceWidth, sourceHeight), GraphicsUnit.Pixel);
}

How can I limit the resulting file size so that it is no bigger than, say, 3KB? I am uploading a 1024x768 JPG which is 768KB. When resizing to 100x100 with the above code, I can't get it any smaller than 12KB.

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

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

发布评论

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

评论(2

凉栀 2024-08-13 07:40:07

调整大小代码不是您需要查看的内容。在执行 Bitmap.Save() 时查看 jpeg 压缩级别。

对于 100x100 的图像来说 3kb 是完全可行的。如果您获得 12kb,您很可能会使用最高质量的 jpeg 压缩来保存 jpeg。

这里是一篇有关如何在保存时设置压缩级别的 MSDN 文章位图。

The resize code isn't what you need to be looking at. Take a look at the jpeg compression level when you are doing your Bitmap.Save().

3kb is completely doable with a 100x100 image. If you are getting 12kb, you are most likely saving the jpeg with the highest quality jpeg compression.

Here is an MSDN article about how to set the compression level when saving the bitmap.

知足的幸福 2024-08-13 07:40:07

You can try to play with the quality property, but there is a way to know whats the resulting image size. You can also try to save the file as png, which works better for non photo images (graphics)

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