Click here to Skip to main content
15,917,328 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I open a bitmap, I cannot save over the bitmap because it's being used. In fact, if I open several bitmaps I cannot save over any I have opened and they all stay in memory. I want to be able to overwrite a bitmap that I open. Where is the issue?

What I have tried:

Please ignore the redundant code in the open sub. I've tried several things to fix the issue.

Private Sub ButtonOpen_Click(sender As Object, e As EventArgs) Handles ButtonOpen.Click
        Dim myStream As System.IO.Stream = Nothing
        Dim OpenFileDialog1 As New OpenFileDialog()

        OpenFileDialog1.InitialDirectory = My.Application.Info.DirectoryPath
        OpenFileDialog1.Filter = "Image Files(*.bmp;*.png;*.jpg)|*.bmp;*.png;*.jpg|All files (*.*)|*.*"
        OpenFileDialog1.FilterIndex = 1
        OpenFileDialog1.RestoreDirectory = True

        If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
                myStream = OpenFileDialog1.OpenFile()
                If (myStream IsNot Nothing) Then
                    Dim OpenFileBitmap As Bitmap = New Bitmap(OpenFileDialog1.OpenFile(), 0)
                    Dim TempBitmap As Bitmap = OpenFileBitmap.Clone()
                    PictureBoxMain.Image = TempBitmap.Clone()
                End If
            Catch Ex As Exception
                MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
                If (myStream IsNot Nothing) Then
                End If
            End Try
        End If

    End Sub

Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click
        Dim saveFileDialog1 As New SaveFileDialog()
        Dim b As Bitmap = CType(PictureBoxMain.Image, Bitmap)

        saveFileDialog1.InitialDirectory = My.Application.Info.DirectoryPath
        saveFileDialog1.Filter = "BMP Windows or OS/2 Bitmap (*.bmp)|*.bmp|PNG Portable Network Graphics (*.png)|*.png|JPG JPEG (*.jpg, *.jpeg)|*.jpg;*.jpeg|TIFF Tagged Image File Format (*.tif, *.tiff)|*.tif;*.tiff"
        saveFileDialog1.FilterIndex = 1
        saveFileDialog1.RestoreDirectory = True

            If saveFileDialog1.ShowDialog() = DialogResult.OK Then
                If (saveFileDialog1.FileName IsNot Nothing) Then
                    Dim filename As String = System.IO.Path.GetFileName(saveFileDialog1.FileName)
                    If saveFileDialog1.FilterIndex = 1 Then
                        b.Save(filename, System.Drawing.Imaging.ImageFormat.Bmp)
                    End If
                    If saveFileDialog1.FilterIndex = 2 Then
                        b.Save(filename, System.Drawing.Imaging.ImageFormat.Png)
                    End If
                    If saveFileDialog1.FilterIndex = 3 Then
                        b.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg)
                    End If
                    If saveFileDialog1.FilterIndex = 4 Then
                        b.Save(filename, System.Drawing.Imaging.ImageFormat.Tiff)
                    End If
                End If
            End If
        Catch ex As Exception
            MessageBox.Show("Cannot write file to disk. Original error: " & ex.Message)
        End Try

    End Sub
Updated 3-Sep-22 18:13pm

1 solution

This has been a known issue for about two decades. You can read the file into an array of bytes, wrap that in a MemoryStream, then create the Bitmap from the stream. That will avoid locking the file for the life of the Bitmap.

From memory and untested:
public Bitmap GetBitmapFromFile(string filepath)
    byte[] buffer = File.ReadAllBytes(filepath);
    using (MemoryStream ms = new MemoryStream(buffer))
        Bitmap bitmap = Image.FromStream(ms);
        return img;
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900