图像大小在C#中压缩为50 kb [英] Image size compress into 50 kb in C#

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

问题描述

我想压缩图像尺寸

这里是我的代码欢迎任何建议



我尝试过:



  string  CustomerID =  ; 
if (会话[ RoleID]!= null
{
CustomerID = Request.Form [ CustomerID];

// string CustomerID = Request.Form [Customer.CustomerID];
if (CustomerID!= null
{
if (Request.Files.Count > 0
{
var file = Request.Files [ 0 ];
if (file!= null && file.ContentLength > 0
{
// var fileName = Path.GetFileName(file.FileName);
var fileExtension = Path.GetExtension(file.FileName);
var allowedExtensions = new [] { 。bmp 。png 。jpg jpeg 。gif};
if (allowedExtensions.Contains(fileExtension))
{
// 删除文件
var pathD = Server.MapPath( 〜/ Avatar / 1);
var images = Directory.GetFiles(pathD,CustomerID + *。);
for int i = 0 ; i < images.Length; i ++)
System.IO.File.Delete(Server.MapPath(( 〜/ Avatar / 1 /)+ Path.GetFileName(images [i])));

// 上传文件
var fileName = CustomerID + fileExtension;
var path = Path.Combine(Server.MapPath( 〜/ Avatar / 1 /),fileName);
file.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
file.SaveAs(path);

// 会话[头像] = fileName;
}

解决方案

你可能不会。

图像通常已被压缩:JPG,GIF ,至少是PNG,但BMP文件不是。任何进一步压缩它们的尝试通常都不会带来任何大的节省,并且实际上可能会导致更大的文件(因为压缩算法需要一些开销,因此可以再次解压缩)。

当然,没有办法压缩图像并保证结果大小低于50KB。



要减小图像大小,要么降低质量或分辨率图像 - 而且无法逆转:你会永远拥有一个更糟糕的形象。 (但即使这样也不能保证所有图像的特定尺寸)



相反,设置对头像文件大小和分辨率的限制,就像这个网站一样。如果您的用户试图忽略这一点,您会发出一条消息,提醒他们限制并拒绝化身。


如果您想压缩图像,则需要使用支持的格式像jpg一样的可变压缩,所以无论上传的图像类型如何,你都需要将它保存为jpg。如果你想确保图像低于一定的大小,你可能需要以一定的速率设置压缩级别,看看图像是否足够小,然后以不同的压缩率重新进行,直到它低于你的大小阈值。



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



PNG还支持可变压缩,因此如果您愿意,可以尝试使用该格式。



另外作为一般提示,如果您要发布代码,请不要在任何地方使用var。无论如何使用它是不好的做法,但至少在使用visual studio时,您可以将鼠标悬停在声明上以发现它们的类型,但我们不知道您的任何类型的对象是什么类型,这会使问题更难以诊断。

了解图像格式已经压缩,将文件大小减小到50Kb,将涉及某种降级质量或降低分辨率。


i want to compress the image size
here is my code any suggetion is welcome

What I have tried:

string CustomerID = "";
           if (Session["RoleID"] != null)
           {               
                   CustomerID = Request.Form["CustomerID"];                           
 
               //string CustomerID = Request.Form["Customer.CustomerID"];
               if (CustomerID != null)
               {
                   if (Request.Files.Count > 0)
                   {
                       var file = Request.Files[0];
                       if (file != null && file.ContentLength > 0)
                       {
                           //var fileName = Path.GetFileName(file.FileName);                       
                           var fileExtension = Path.GetExtension(file.FileName);
                           var allowedExtensions = new[] { ".bmp", ".png", ".jpg", "jpeg", ".gif" };
                           if (allowedExtensions.Contains(fileExtension))
                           {
                               //Delete files
                               var pathD = Server.MapPath("~/Avatar/1");
                               var images = Directory.GetFiles(pathD, CustomerID + ".*");
                               for (int i = 0; i < images.Length; i++)
                                   System.IO.File.Delete(Server.MapPath(("~/Avatar/1/") + Path.GetFileName(images[i])));
 
                               //Up files
                               var fileName = CustomerID + fileExtension;
                               var path = Path.Combine(Server.MapPath("~/Avatar/1/"), fileName);
                               file.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
                               file.SaveAs(path);
 
                               //Session["Avatar"] = fileName;
                           }

解决方案

You can't, probably.
Images are generally already compressed: JPG, GIF, and PNG at least are, though BMP files aren't. And any attempt to compress them further doesn't normally result in any big savings, and can in fact result in a bigger file (as the compression algorithm needs some overhead so it can be decompressed again).
Certainly, there is no way to compress an image and guarantee a resulting size that is lower than 50KB.

To reduce an images size, you either reduce the quality or resolution of the image - and that can't be reversed: you will permanently have a "worse" image. (But even that can't guarantee a specific size for all images)

Instead, set limits on avatar file size and resolution, just as this site does. If your users attempt to ignore that, you issue a message reminding them of the limits and refuse the avatar.


If you want to compress the images you'll need to use a format that supports variable compression like jpg does, so regardless of the image type uploaded you'll need to save it as a jpg. If you want to ensure the image is below a certain size you'll probably have to set the compression level at a certain rate and see if the image is small enough then re-do at a different compression rate until it is below your size threshold.

How to: Set JPEG Compression Level[^]

PNG also supports variable compression so you could try that format if you prefer.

Also as a general tip, if you're going to post your code don't use "var" everywhere. It's bad practice to use it anyway, but at least when using visual studio you can hover over the declarations to discover their types but we don't know what type any of your objects are and that can make problems harder to diagnose.


Knowing that images formats are already compressed, reducing file size into 50Kb, will involve some kind of downgrading the quality or reducing resolution.


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

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