fast2021
fast2021

Reputation: 117

Save the image from the picturebox

Use this code to save the image from the PictureBox. By saving the file and adding a name to it The problem I'm facing, come on, when adjusting the image the following error occurs : - a generic error occurred in gdi+

My Code

show image

Using fs As New System.IO.FileStream("path", IO.FileMode.Open, IO.FileAccess.Read) PIC_PARTA.Image = System.Drawing.Image.FromStream(fs)  End Using 
save image
   Dim pic As Image
            pic = PIC_PARTA.Image
            Dim Savefild As New SaveFileDialog
            Savefild.FileName = TEXT_NAMEIMG.Text & ".PNG"
            Savefild.ShowDialog()
            pic.Save(Savefild.FileName, System.Drawing.Imaging.ImageFormat.Png)
            LAB_path.Text = Savefild.FileName
            Savefild.Dispose()
            pic.Dispose()

Upvotes: 0

Views: 1106

Answers (1)

jmcilhinney
jmcilhinney

Reputation: 54417

This is not a full answer at this stage but it includes a significant amount of code, so I'll post as an answer and then either update or delete as required.

I just tried this code and all four saves worked, even though I thought that some may not:

Imports System.IO

Public Class Form1

    Private ReadOnly folderPath As String = Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyPictures, "Test")

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        PictureBox1.Image = Image.FromFile(Path.Combine(folderPath, "Picture1.png"))

        Using fs = File.OpenRead(Path.Combine(folderPath, "Picture2.png"))
            PictureBox2.Image = Image.FromStream(fs)
        End Using

        PictureBox3.ImageLocation = Path.Combine(folderPath, "Picture3.png")

        PictureBox4.Load(Path.Combine(folderPath, "Picture4.png"))
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            PictureBox1.Image.Save(Path.Combine(folderPath, "Output1.png"))
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Try
            PictureBox2.Image.Save(Path.Combine(folderPath, "Output2.png"))
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Try
            PictureBox3.Image.Save(Path.Combine(folderPath, "Output3.png"))
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Try
            PictureBox4.Image.Save(Path.Combine(folderPath, "Output4.png"))
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try
    End Sub

End Class

Can you please test that with your own images in your own folder and see if you get the same result?

Note that the Image.FromFile option locks the file until you dispose the Image. I have seen instances in the past where trying to save an Image created using Image.FromStream generates the error message you mentioned because it can't access the stream again later. Image.FromFile avoids that but has its own downsides.

My code that uses a FileStream directly is simpler and, therefore, better than yours but setting ImageLocation or calling Load is simpler again and thus better again. I suggest that you use one of those two options and see whether you still have an issue.

Upvotes: 1

Related Questions