Click here to Skip to main content
14,392,909 members
Rate this:
Please Sign up or sign in to vote.
See more:
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;
                           }
Posted
Updated 27-Apr-16 2:49am
v2
Comments
Philippe Mori 27-Apr-16 22:18pm
   
Well, you might simply create a JPG of the desired final resolution with normal compression ratio and ignore the fact that the image might sometime be bigger than 50KB.
Rate this:
Please Sign up or sign in to vote.

Solution 1

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.
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

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.
   
Comments
Philippe Mori 27-Apr-16 22:16pm
   
Using var is not a bad practice. In that case, it is fairly easy to guess the type of most variables with minimal C# and ASP.NET knowledge.
Rate this:
Please Sign up or sign in to vote.

Solution 3

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100