in my page. when i upload image i did image management. after that i want to delete the original image but i am getting error that the file is already using, like below
(The process cannot access the file ‘D:\sasiweb\myimage\Images\jalsa.jpeg’ because it is being used by another process.)
and this is my code
protected void sizeManage(string filename)
{
string fn = Server.MapPath("~/Images/" + filename );
System.Drawing.Bitmap newimg = new System.Drawing.Bitmap(fn);
int h = newimg.Height;
int w = newimg.Width;
if (w > 100)
{
objJpeg = new ASPJPEGLib.ASPJpeg();
objJpeg.Open(Server.MapPath("~/Images/" + FileUpload1.FileName.ToString()));
int L = 100;
objJpeg.Width = L;
objJpeg.Height = objJpeg.OriginalHeight * L / objJpeg.OriginalWidth;
objJpeg.Save(Server.MapPath("~/Images/" + "small" + FileUpload1.FileName));
string path = Server.MapPath("~/Images/" + FileUpload1.FileName.ToString());
FileInfo file = new FileInfo(path);
file.Delete();
}
else
{
}
}
protected void Button1_Click(object sender, EventArgs e)
{
FileUpload1.SaveAs(Server.MapPath("~/Images/" + FileUpload1.FileName.ToString()));
sizeManage(FileUpload1.FileName.ToString());
}
i am getting error at file.delete();
(The process cannot access the file ‘D:\sasiweb\myimage\Images\jalsa.jpeg’ because it is being used by another process.)
You have to close
to do this operation.
Since the file is opened using objJpeg object it has to be freed to delete the file.
or you can code file open process in the using statement.