Overwrite Existing Image

前端 未结 2 964
孤街浪徒
孤街浪徒 2020-12-05 14:22

I have this code

    private void saveImage()
    {
        Bitmap bmp1 = new Bitmap(pictureBox.Image);
        bmp1.Save(\"c:\\\\t.jpg\", System.Drawing.I         


        
相关标签:
2条回答
  • 2020-12-05 14:32

    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();
        }
    
    0 讨论(0)
  • 2020-12-05 14:44

    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.

    0 讨论(0)
提交回复
热议问题