Overwrite Existing Image

You must remove your image if that is already exists.

private void saveImage()
    {
        Bitmap bmp1 = new Bitmap(pictureBox.Image);

       if(System.IO.File.Exists("c:\\t.jpg"))
              System.IO.File.Delete("c:\\t.jpg");

        bmp1.Save("c:\\t.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
        // Dispose of the image files.
        bmp1.Dispose();
    }

I presume you earlier loaded the c:\t.jpg image using the Image.Load method. If so, the Image object is holding an open file handle on the image file, which means that the file can't be overwritten.

Instead of using Image.Load to get the original image, load it from a FileStream that you create and dispose of.

So, instead of

Image image = Image.Load(@"c:\\t.jpg");

do this:

using(FileStream fs = new FileStream(@"c:\\t.jpg", FileMode.Open))
{
    pictureBox.Image = Image.FromStream(fs);
    fs.Close();
}

The file handle has been released so overwriting the file with Bitmap.Save can succeed. The code you gave in your question should therefore work. There is no need to delete the original file or dispose of the image before saving.

Tags:

C#

Image

Bitmap