在C#中上传时将图像缩小到指定大小 [英] Reduce the image to specified size while uploading in C#

查看:99
本文介绍了在C#中上传时将图像缩小到指定大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个web应用程序。我想实现以下功能,希望你们帮助我:



在上传图像文件时,我想做< b>如果照片是>自动将照片的大小减小到精确2 MB。 2 MB 与图像质量,高度和宽度相媲美。



如何在c#中实现这些人,希望您使用示例代码详细说明





在此先感谢...



我的尝试:



i尝试了很多但没有减少到精确的2Mb,减少到不可预测的大小< 2Mb

I have a web application.I want to implement the following Functionality ,Hope You guys Help me:

While Uploading an image File,I want to do Automatically decrease the size of the photo to Exact 2 MB if the photo is > 2 MB Compromises with image quality,height and width.

How implement this guys in c#,Hope You Elaborate with Sample Code


Thanks in Advance...

What I have tried:

i tried lot but not reduced to exact 2Mb, reduce to non-predictable size < 2Mb

推荐答案

出于两个原因你不能。

1)你无法在上传文件时或之前更改文件 - C#代码只在服务器上运行,而不是在客户端上运行 - 并且服务器在上传完成之前无法访问数据。

2)如果你总能在不影响高度的情况下缩小图像,宽度或质量,你不认为原始图像 已经完成了 吗?您可以通过更改存储格式来缩小图像大小,前提是您使用无损压缩格式(例如,PNG,JPG不是),但不能保证它会变小(甚至可能会变小)对于某些图像来说更大)并且绝对不能保证得到的图像大小是任何特定值。
You can't, for two reasons.
1) You can't change a file while or before it's uploaded - C# code only runs on the Server, not the client - and the server has no access to the data until the upload is complete.
2) If you could always make an image smaller without affecting the height, width, or quality, don't you think that the original image would already have done that? You may be able to reduce image size by changing the format in which you store it, provided you use a lossless compression format (PNG is, JPG isn't for example) but there is no guarantee it will get smaller (it may even get bigger for some images) and absolutely no guarantee that the resulting image size will be any specific value.


引用:

我想这样做如果照片是>则自动将照片的大小减小到精确2 MB 2 MB而不改变宽度,高度和质量。

I want to do Automatically decrease the size of the photo to Exact 2 MB if the photo is > 2 MB without changing the width,height and quality.



这是不可能的。图像的宽度,高度,质量和大小是相互关联的,你不能在不改变另一个的情况下改变它。


It is just impossible. width, height, quality and size of an image are linked, you can't change one without changing another.

引用:

在上传图像文件时,



上传图像时不执行任何操作。你需要等到上传结束。


You do nothing while uploading an image. You need to wait until the end of upload.

引用:

自动将照片的大小减小到精确2 MB

Automatically decrease the size of the photo to Exact 2 MB



确切的大小几乎不可能实现。


Exact size is almost impossible to achieve.


您可能需要将图像保存为JPG并检查文件大小,如果它太大,请用更高的压缩率再次保存,检查尺寸等等。可在此处找到更改压缩比的代码



如何:设置JPEG压缩级别Microsoft Docs [ ^ ]



伪代码将类似



You'll probably have to save the image as a JPG and check the file size, if it's too big save it again with a higher compression, check the size and so on. Code for changing compression ratio can be found here

How to: Set JPEG Compression Level | Microsoft Docs[^]

pseudocode would be something like

quality = 100
save image at quality
do while image size > 2MB and quality > 10
    quality = quality - 10
    save image at quality
end do





抢占你的下一个问题,不,我不会为你写的,有很多关于如何上传图像和保存它们的例子,我已经向你展示了如何以给定的质量保存的代码,所以谷歌那些样本,了解然后将它们放在一起。



To preempt your next question, no I won't write it for you, there are loads of examples on how to upload images and save them, and I've shown you the code for how to save at a given quality, so google those samples, understand them, then put them together.


这篇关于在C#中上传时将图像缩小到指定大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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