c# 4.0 - The process cannot access the file 'C:\Photo.jpg' because it is being used by another process -


i m trying resize uploaded image , save in particular location replacing existing file. gives me error saying "the process cannot access file 'c:\photo.jpg' because being used process."

here code.

private void llblchangeplayerpicture_linkclicked(object sender, linklabellinkclickedeventargs e)         {             ofdplayerpicture.reset();                 ofdplayerpicture.filter = "jpeg(*.jpeg,*.jpg)|*.jpeg;*.jpg|bitmap (*.bmp)| *.bmp |pngs(*.png)|*.png";                 ofdplayerpicture.showdialog();                 image newimage;                 if (ofdplayerpicture.filename != "")                 {                     pboplayerpicture.image = null;                     pboplayerpicture.imagelocation = null;                     newimage = resize(image.fromfile(ofdplayerpicture.filename), new size(250, 100));                     savejpeg(program.playerimagepath + regno + "\\photo1.jpg", newimage, 100);                } 

here save image

 public static void savejpeg(string path, image img, int quality)             {                 encoderparameter qualityparam = new encoderparameter(system.drawing.imaging.encoder.quality, quality);                 imagecodecinfo jpegcodec = getencoderinfo("image/jpeg");                 encoderparameters encoderparams = new encoderparameters(1);                 encoderparams.param[0] = qualityparam;                 system.io.memorystream mss = new system.io.memorystream();                 system.io.filestream fs = new system.io.filestream(path, system.io.filemode.create, system.io.fileaccess.readwrite);                 img.save(mss, jpegcodec, encoderparams);                 byte[] matriz = mss.toarray();                 fs.write(matriz, 0, matriz.length);                  mss.close();                 fs.close();                 mss.dispose();                 fs.dispose();             } 

incase want delete photo.jpg using file.delete function, gives me same issue. appreciated. thanx

found solution. dispose objects, , picture boxes in image used. whether used in same control or different control. recreate picture box control in main control every time image changed. works fine me.

hope useful.

happy coding


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -