使用 GDI 调整大小时限制文件大小+
我正在调整上传图像的大小,如下所示:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
调整大小代码不是您需要查看的内容。在执行 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.
您可以尝试使用质量 属性,但有一种方法可以知道生成的图像大小是多少。您还可以尝试将文件另存为 png,这对于非照片图像(图形)效果更好
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)