When I upload the image it is showing this error please can u help me?
The process cannot access the file ‘C:\Websites\telugufilmchance\httpdocs\User\photo\thumb\PPTS00025sonali.jpeg’ because it is being used by another process.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.IO.IOException: The process cannot access the file ‘C:\Websites\telugufilmchance\httpdocs\User\photo\thumb\PPTS00025sonali.jpeg’ because it is being used by another process.
Source Error:
if (ext == ".JPEG" || ext == ".JPG" || ext == ".PNG" || ext == ".BMP" || ext == ".GIF")
Line 193: {
Line 194: fupPhoto.PostedFile.SaveAs(path + filename);
Line 195: Bitmap src = Bitmap.FromStream(fupPhoto.PostedFile.InputStream) as Bitmap;
Line 196: Bitmap result = ResizeBitmap(src);
this is my code (updating code it is a edit page and i am updating)
if (fupPhoto.FileName != string.Empty)
{
filename = ran(fupPhoto.FileName);
FileInfo fi = new FileInfo(filename);
string ext = fi.Extension.ToUpper();
string path = Server.MapPath("../User/photo/thumb/");
if (ext == ".JPEG" || ext == ".JPG" || ext == ".PNG" || ext == ".BMP" || ext == ".GIF")
{
fupPhoto.PostedFile.SaveAs(path + filename);
Bitmap src = Bitmap.FromStream(fupPhoto.PostedFile.InputStream) as Bitmap;
Bitmap result = ResizeBitmap(src);
SizeMgt(filename);
System.Drawing.Bitmap img = new System.Drawing.Bitmap(result, newwid, newhgt);
System.Drawing.Imaging.ImageCodecInfo jpegcodec = null;
System.Drawing.Imaging.EncoderParameters EncParams;
foreach (System.Drawing.Imaging.ImageCodecInfo codec in System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders())
{
if (codec.MimeType == "image/jpeg")
{
jpegcodec = codec;
break;
}
}
EncParams = new System.Drawing.Imaging.EncoderParameters(1);
EncParams.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 20L);
newfilename = "new" + filename;
img.Save(path + newfilename, jpegcodec, EncParams);
img.Dispose();
}
}
else
{
newfilename = myphot;
}
cmd = new SqlCommand("sp_uptreg", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@tregno", lblregno1.Text);
cmd.Parameters.AddWithValue("@catid", ddlCategory.SelectedItem.Value);
cmd.Parameters.AddWithValue("@fname", txtfname.Text);
cmd.Parameters.AddWithValue("@mname", txtmname.Text);
cmd.Parameters.AddWithValue("@lname", txtlname.Text);
cmd.Parameters.AddWithValue("@emailid", txtEmail.Text);
cmd.Parameters.AddWithValue("@phoneno", txtphone.Text);
cmd.Parameters.AddWithValue("@mobileno", txtMobile.Text);
cmd.Parameters.AddWithValue("@address", txtAdd.Text);
cmd.Parameters.AddWithValue("@photos", newfilename);
int a = cmd.ExecuteNonQuery();
I think that this function fails, if the file all ready exist !
so you need to delete it first (if exist).
You can use the FileInfo(path + filename), to see if FileExist and delete it.
From the SaveAs, you see that have the Create Option. So the procedure fails if the file all ready exist.
I suggest also to use the using keyword on the objects that need dispose.