0

i have use a controller action with use of file uploader and i want to compress the image and compress process perform after save the image, so my problem is : i want save only compress image and delete the orignal one. but this code shows error : file because it is being used by another process

My Code is :

  public ActionResult submitgeneralinfo(HttpPostedFileBase file, int? EmployeeId, GeneralInfoViewModel model)
    {
           var ext = Path.GetExtension(file.FileName);
           uniquefilename = Convert.ToString(ID) + ext;
           var path = Path.Combine(Server.MapPath("~/Attachements/GeneralInfodp/"), uniquefilename);
           var compaths = Path.Combine(Server.MapPath("~/Attachements/GeneralInfodp/"), "com" + uniquefilename);
           file.SaveAs(path);
           file.InputStream.Dispose();
           CompressImage(Image.FromFile(path), 30, compaths);
           file.InputStream.Close();
                    file.InputStream.Dispose();
                    GC.Collect();
                    FileInfo file3 = new FileInfo(path);
                    if (file3.Exists)
                    {
                        file3.Delete(); // error :- file because it is being used by another process
                    }

    } 


    private void CompressImage(Image sourceImage, int imageQuality, string savePath)
    {
        try
        {
            //Create an ImageCodecInfo-object for the codec information
            ImageCodecInfo jpegCodec = null;

            //Set quality factor for compression
            EncoderParameter imageQualitysParameter = new EncoderParameter(
                        System.Drawing.Imaging.Encoder.Quality, imageQuality);

            //List all avaible codecs (system wide)
            ImageCodecInfo[] alleCodecs = ImageCodecInfo.GetImageEncoders();

            EncoderParameters codecParameter = new EncoderParameters(1);
            codecParameter.Param[0] = imageQualitysParameter;

            //Find and choose JPEG codec
            for (int i = 0; i < alleCodecs.Length; i++)
            {
                if (alleCodecs[i].MimeType == "image/jpeg")
                {
                    jpegCodec = alleCodecs[i];
                    break;
                }
            }

            //Save compressed image
            sourceImage.Save(savePath, jpegCodec, codecParameter);
        }
        catch (Exception e)
        {

        }
    }
sandeep singh
  • 143
  • 1
  • 13
  • Try reading the input stream into a byte[] initially, rather than storing the uploaded file to disk. I know this isn't a solution, but it sounds like the goal is to avoid using excess storage space. Also, you could make CompressImage static, passing in the byte[] since it doesn't really need controller context once you've read the bytes. – DUBYATOO Sep 01 '15 at 13:26
  • try sourceImage.Dispose(); after sourceImage.Save(savePath, jpegCodec, codecParameter); – Dandy Sep 01 '15 at 18:49
  • Image keeps file in use, read it all before and use a MemoryStream. Moreover: get rid of all that Dispose()! Also GC.Collect() should/must be avoided. Code to pick encoder is prolix without reason. – Adriano Repetti Sep 02 '15 at 06:00

1 Answers1

0

Save image direct in low size with using WebImage

[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
    WebImage img = new WebImage(file.InputStream);
    if (img.Width > 1000)
        img.Resize(1000, 1000);
    img.Save("path");
    return View();
}
sandeep singh
  • 143
  • 1
  • 13