File used by another process error without using the stream

Last post 10-08-2008, 11:55 PM by CatenaLogic. 2 replies.
Sort Posts: Previous Next
  •  10-08-2008, 7:16 AM 44689

    File used by another process error without using the stream

    I am currently creating a user control that holds an UploadedImage property of type System.Drawing.Image.
     
    I read the other posts about this error, but they all say that I have to close the stream. However, I am not using the stream :(
     
    I use the code below when a file is uploaded:

            /// <summary>
            /// Invoked when the image uploader uploaded a new file
            /// </summary>
            /// <param name="sender">Sender</param>
            /// <param name="args">UploaderEventArgs</param>
            protected void imageUploader_FileUploaded(object sender, UploaderEventArgs args)
            {
                try
                {
                    // Store image
                    /*using (Stream stream = args.OpenStream())
                    {
                        _uploadedImage = new System.Drawing.Bitmap(stream);
                        _uploadedImage.Save(args.GetTempFilePath());
                    }*/

                    // Delay the deletion of the temp file with an hour
                    args.Persist();

                    // Save the file as image
                    _uploadedImage = System.Drawing.Image.FromFile(args.GetTempFilePath());

                    // Invoke event
                    OnImageUploaded(args.FileGuid);
                }
                catch (Exception)
                {
                    args.Delete();
                }
            }
     
    Simply said, I use args.Persist() to keep the file on disk for another hour (as described in another post). Then, I load the image into memory so I can easily retrieve it when I have to using the UploadedImage property.
     
    Finally, the OnImageUploaded invokes a custom event and calls an image handler with the file guid. In the handler, I use this code to load the image into the handler:

            /// <summary>
            /// Retrieves an image from disk
            /// </summary>
            /// <param name="objectID">ID of the object to retrieve</param>
            /// <returns>Image based on the object ID</returns>
            protected override System.Drawing.Image GetImage(string objectID)
            {
                // Check the data
                if (string.IsNullOrEmpty(objectID))
                {
                    // Failed
                    return null;
                }

                // Get the guid
                Guid guid = new Guid(objectID);

                // Get all the files
                try
                {
                    string[] files = Directory.GetFiles(HttpContext.Current.Server.MapPath(_uploadPath),
                        string.Format("persisted.{0}.*", guid), SearchOption.AllDirectories);
                    if (files.Length == 0)
                    {
                        // Failed
                        return null;
                    }

                    // Return the image
                    return System.Drawing.Image.FromFile(files[0]);
                }
                catch (Exception)
                {
                    // Failed
                    return null;
                }
            }

    The exception does not occur consequently, only now and then. What am I doing wrong?
     
    Thanks in advance!
  •  10-08-2008, 11:19 PM 44714 in reply to 44689

    Re: File used by another process error without using the stream

    Hi,
     
    When the code execute
     
    // Save the file as image
    _uploadedImage = System.Drawing.Image.FromFile(args.GetTempFilePath());
     
    the file would be locked until the _uploadedImage is disposed.
     
    So
     
    return System.Drawing.Image.FromFile(files[0]);
    would not works.
     
    maybe you can try this way :
     
      static public System.Drawing.Image LoadFromFileAndFree(string filepath)
      {
       byte[] data;
       using (System.IO.FileStream fs = new System.IO.FileStream(filepath,
        System.IO.FileMode.Open, System.IO.FileAccess.Read))
       {
        data = new byte[fs.Length];
        fs.Read(data, 0, data.Length);
       }
       return System.Drawing.Image.FromStream(new System.IO.MemoryStream(data));
      }
     
    Regards , Terry
  •  10-08-2008, 11:55 PM 44715 in reply to 44714

    Re: File used by another process error without using the stream

    Great, this seems to work!
     
    Thank you!
View as RSS news feed in XML