Uploading photo ( because it is being used by another process. )?
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.En开发者_JS百科coderParameters(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 would say that your code for saving the file isn't disposing of all the handles you are holding, so when you try to save it there again, it's already in use. Can you post your saving code?
I think that this function fails, if the file all ready exist !
fupPhoto.PostedFile.SaveAs(path + filename);
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.
public void SaveAs(string filename)
{
if (!Path.IsPathRooted(filename) && RuntimeConfig.GetConfig().HttpRuntime.RequireRootedSaveAsPath)
{
throw new HttpException(SR.GetString("SaveAs_requires_rooted_path", new object[] { filename }));
}
FileStream s = new FileStream(filename, FileMode.Create);
try
{
this._stream.WriteTo(s);
s.Flush();
}
finally
{
s.Close();
}
}
I suggest also to use the using keyword on the objects that need dispose.
using (Bitmap src = Bitmap.FromStream(fupPhoto.PostedFile.InputStream) as Bitmap)
{
//Bitmap result = ResizeBitmap(src);
//SizeMgt(filename);
using (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();
}
src.Dispose();
}
精彩评论