0
string imagePath = HttpContext.Current.Server.MapPath("~/Resources/image.jpeg");
if (File.Exists(imagePath))
{
    File.Delete(imagePath);
    Debug.WriteLine("image path exists");
}

using (var imageStream = new MemoryStream(binData))
{
    Bitmap image = new Bitmap(imageStream);

    image.Save(imagePath, System.Drawing.Imaging.ImageFormat.Jpeg);
    image.Dispose();
}

I am getting a System.IO.IOException ..The process cannot be accessed as it is already in use

Joehl
  • 3,671
  • 3
  • 25
  • 53
jayasurya_j
  • 1,519
  • 1
  • 15
  • 22
  • Which part is causing the IOException? Also, make sure that you don't have the file open in a different program (image viewer, for example). – Krikor Ailanjian Jun 14 '16 at 05:17
  • When i try to delete it. Also i dont have my image viewer or anything open – jayasurya_j Jun 14 '16 at 05:18
  • 3
    If the exception is occurring at 'File.Delete', then the only explanation is that the file is in use by your code, or there is an open handle on the file. You should check that you are closing the file properly when you use it in other areas of your code. – Krikor Ailanjian Jun 14 '16 at 05:34
  • 1
    Do you have write/delete privileges? – TaW Jun 14 '16 at 05:45
  • I doubt you show us all the involved code. If the image is still used anywhere, like in a `PictureBox.Image` or as the `BackgroundImage` of a control it will cling to the original file stream. This is even true if you have cloned the bitmap. The clone will also lock the original file. The way to go would the to create a copy using a memorystream. See [here](http://stackoverflow.com/questions/37736815/overwrite-image-picturebox-in-c-sharp/37741101?s=1|0.0000#37741101) for an example – TaW Jun 14 '16 at 08:23

0 Answers0