Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Thijs 97 posts 117 karma points
    Nov 18, 2011 @ 17:10
    Thijs
    0

    The process cannot access the file x because it is being used by another process.

    I'm trying to write an automatic image cropper when a user uploads an image to the media section. I'm using this function in the Media.AfterSave and Media.AfterNew events. When the code fires I'm getting the error mentioned in the title.

    Here is the code I'm using.

    private void CropImage(Media media, int width)
    {
      //Check where dealing with an image
      //if(media.ContentType.Alias.Equals("image"))
      {
        string fileUrl = HttpContext.Current.Server.MapPath(
                                media.getProperty("umbracoFile").Value.ToString());
    
        //Make the cropped image
        System.Drawing.Bitmap img = Image.ResizeByWidth(
                                          new System.Drawing.Bitmap(fileUrl), 500);
    
        //Delete original image
        File.Delete(fileUrl);
    
        //Save cropped image
        img.Save(fileUrl);
        }
      }
    }
    
  • Thijs 97 posts 117 karma points
    Nov 18, 2011 @ 17:12
    Thijs
    0

    Forgot to add what line triggers the error :) (Can't edit my post....)
    The errors comes with the File.Delete(fileUrl) line.

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Nov 21, 2011 @ 08:39
    Stefan Kip
    0

    I think that's because of the line above File.Delete(): new System.Drawing.Bitmap(fileUrl)

    What if you try this:

    private void CropImage(Media media, int width){

    //Check where dealing with an image

    //if(media.ContentType.Alias.Equals("image")) {

    string fileUrl = HttpContext.Current.Server.MapPath(media.getProperty("umbracoFile").Value.ToString());

    //Make the cropped image

    System.Drawing.Bitmap image = new System.Drawing.Bitmap(fileUrl);

    System.Drawing.Bitmap croppedImage = Image.ResizeByWidth(new System.Drawing.Bitmap(fileUrl), 500);-

    image.Dispose();

    //Delete original image

    File.Delete(fileUrl);

    //Save cropped image

    croppedImage.Save(fileUrl);

    croppedImage.Dispose();

    //}

    }

  • Thijs 97 posts 117 karma points
    Nov 21, 2011 @ 10:04
    Thijs
    0

    @kipusoep, thank you, that worked! 

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Nov 21, 2011 @ 10:05
    Stefan Kip
    0

    Awesome! :-)

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Nov 21, 2011 @ 10:08
    Stefan Kip
    0

    I just saw there were 2 minor errors in my script:

    private void CropImage(Media media, int width){
    //Check where dealing with an image
    //if(media.ContentType.Alias.Equals("image")) {
    string fileUrl = HttpContext.Current.Server.MapPath(media.getProperty("umbracoFile").Value.ToString());
    //Make the cropped image
    System.Drawing.Bitmap image = new System.Drawing.Bitmap(fileUrl);
    System.Drawing.Bitmap croppedImage = Image.ResizeByWidth(image, 500);
    image.Dispose();
    //Delete original image
    File.Delete(fileUrl);
    //Save cropped image
    croppedImage.Save(fileUrl);
    croppedImage.Dispose();
    //}
    }
Please Sign in or register to post replies

Write your reply to:

Draft