Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET Server file File-Upload , +
I want delete a file but an error occured please help me.
==============================
The process cannot access the file 'D:\My Project\AzadUniv\New\12\GUI\Upload\NewsImage\5564868769930125853-Image' because it is being used by another process.
==============================
 

string ImagePath = Server.MapPath(".")+"\\Upload\\NewsImage\\" + Utility.GenerateGUID() + "-Image";
            if (fuImage.PostedFile != null)
            {
                fuImage.PostedFile.SaveAs(ImagePath);
                fuImage.Dispose();
                string d = Utility.GetThumbnailImage(ImagePath, 100, 100,true);
 
            }
 

 
  public static string GetThumbnailImage(string path,int x,int y,bool DeleteAfterSave)
        {
 
                // Create the in-memory bitmap where you will draw the image.
                Bitmap image = new Bitmap(x, y);
                Graphics g = Graphics.FromImage(image);
 
                // Load the file data.
                System.Drawing.Image thumbnail =
                System.Drawing.Image.FromFile(path);
 
                // Draw the thumbnail.
                g.DrawImage(thumbnail, 0, 0, x, y);
 
                // Render the image.
                string NewPath = path +"-small.jpg";
               image.Save(NewPath, ImageFormat.Jpeg);
                
                g.Dispose();
                image.Dispose();
                if (DeleteAfterSave == true)
                {
                    System.IO.File.Delete(path);
                }
                return path;
            }
 
Posted 23-Jul-12 11:16am
Edited 23-Jul-12 11:44am
v2
Comments
ryanb31 at 23-Jul-12 17:00pm
   
What's the question? The error is telling you what is going on. If your code is correct you'll likely have to kick it off on a new thread and let the current thread finish, so that dispose is actually called.
Behnam Mohammadi at 24-Jul-12 11:44am
   
THIS IS A ANSWER!!!!!!!!!!!!!!!!!
You are attempting to delete the file that was read in within the thumbnail object. Perhaps if you disposed of the thumbnail object, the file resource would be released.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You are attempting to delete the file that was read in within the thumbnail object. Perhaps if you disposed of the thumbnail object, the file resource would be released.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 410
1 Jochen Arndt 200
2 Richard MacCutchan 135
3 DamithSL 105
4 PIEBALDconsult 90
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,032
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,220


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 23 Jul 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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