使用asyncfileupload在上载之前缩小或压缩图像大小 [英] Reduce or compress image size before upload using asyncfileupload

查看:82
本文介绍了使用asyncfileupload在上载之前缩小或压缩图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在上传开始之前缩小图像大小或压缩图像。我想在asyncfileupload的OnClientUploadStarted事件中处理这个。



谢谢,

Saurav



我尝试过:



找不到任何解决办法。

I want to reduce the image size or compress the image, before the upload has started. I want to handle this in OnClientUploadStarted event of asyncfileupload.

Thanks,
Saurav

What I have tried:

Didnt find any solution soo far.

推荐答案

好的,所以你在谈论重新采样图像。例如,从4096x2048 PNG(或任何格式)开始,您希望将其转换为2048x1024,它保留纵横比,但将尺寸压缩4倍(每个维度2个)。



一般情况下,你不能在不影响质量的情况下重新采样,因为必要的插值会在附近的像素之间引入一些模糊。然而,对于大多数实际目的,这是可以的不要担心这个,因为算法已经解决了很多年,你不会比使用现有的现成的更好。



关于代码项目其他地方的这个主题有一个很好的 C#示例项目 ,另一个更深入地选择重新采样算法这里



最后,你提到了50%的压缩率。如果您的总图像尺寸为50%,则需要在X和Y尺寸上减少1 / SQRT(2)= 0.707。例如,原始4096 X 2048图像需要减少到2896.3 X 1448.15:在这种情况下,您需要舍入到整数个像素,例如2896 X 1448.插值通常会更加优化(并且使用计算如果你坚持每个维度的减少2的幂,则更有可能实现优化,但在这种情况下,实现的最小压缩将是2x2 => 4 =>尺寸缩小25%。



我希望这会有所帮助。
OK, so you are talking about re-sampling the image. For example, starting with a 4096x2048 PNG (or whatever format) you're wanting to convert this to, say, 2048x1024 which preserves the aspect ratio but compresses the size by a factor of 4 (2 in each dimension).

You can't in general re-sample without having some impact on quality, because there's a necessary interpolation which introduces some blur between nearby pixels. However for most practical purposes this is OK. Don't worry about this because the algos have been worked out for many years and you aren't going to do better than using existing "off the shelf" ones.

There's a nice C# example project on this topic elsewhere on Code Project, and another that goes into more depth on the choice of re-sampling algorithm here.

Finally, you mentioned a 50% compression. If you mean 50% in total image size this requires a 1/SQRT(2) = 0.707 reduction in the X and Y dimensions. For example, an original 4096 X 2048 image is going to need to reduce to 2896.3 X 1448.15: In that case you will need to round to an integer number of pixels such as 2896 X 1448. Interpolation will generally be more optimum (and with computational optimisations more likely achieved) if you stick to reductions in each dimension by powers of 2, but in that case the minimum compression achieved will be 2x2 => 4 => 25% reduction in size.

I hope this helps.


这篇关于使用asyncfileupload在上载之前缩小或压缩图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆