So here's the problem-

My app has an image in it in a picture box. I need the end user to be able to change it. So the code I added below should do that. However, I get the "A generic error occurred in GDI+. while saving"

I understand it as this- possibly an error saving it to C:\?? Because when I save it to a folder such as MyPictures or Pictures it saves A-ok.

Does anyone have a solution? It really needs to go in a folder on c:\

Private Sub btnChange_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChange.Click
        Dim dlg As New OpenFileDialog
        With dlg
            .Title = "open"
            '.Filter = "GIF Files (.gif)|*.gif"
            .Filter = "All Files|*.*|Bitmap Files (*)|*.bmp;*.gif;*.jpg"
            .FileName = "logo"
            .ShowDialog()
        End With
        TextBox1.Text = dlg.FileName
        PictureBox1.ImageLocation = TextBox1.Text
    End Sub

    Private Sub btnSaveClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveClose.Click
        If Me.PictureBox1.Image IsNot Nothing Then
            Me.PictureBox1.Image.Save(IO.Path.Combine("C:\DailyLog\", "logo.gif"))
        End If
        Me.Close()
    End Sub

Please, be so kind to confirm:
1) Disk C: has been NTFS formatted
2) The folder C:\DailyLog exists
3) The user running the application has Modify permission to this folder and almost Read permissions on C:\
4) The file C:\DailyLog\logo.gif does not exist before saving

Thanks in advance

1) Yes
2) Yes
3) I believe so, whatever the defaults are in Win7/XP
4) It does exist, as it's being used in the app.

From researching this, it seems that there's a permission problem. The file being overwritten is currently in use by the app. So overwriting it while the app is using the image I guess won't work. How do I get the app to release the image so it can be overwritten?

In this case, you need:
1) to save the file in a temporary file,
2) set the image to nothing so will release the source file,
3) wait for some miliseconds to give time to the OS to release the handler,
4) then delete the old file and
5) rename the temp to the right name.

Hope this helps

It does- thanks- marked as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.