Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save an image from a picturebox to jpg

I have been able to save a file as a .jpeg, but the image won't load, has anyone got a suggestion?

    Private Sub Btnconfirm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btnconfirm.Click

        MsgBox("A receipt will now be saved to your files", vbOKOnly, "Thank you for your purchase")

        SaveFileDialog1.ShowDialog()
        MsgBox("Thank you for choosing Tiny Theatre, have a nice day.", vbOKOnly, "Thank you")
        Me.Close()
    End Sub

    Private Sub SaveFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk
        Dim FileToSaveAs As String = SaveFileDialog1.FileName

        Dim objwriter As New System.IO.StreamWriter(FileToSaveAs)
        objwriter.Write(PictureBox1)
        objwriter.Close()
    End Sub
like image 647
rah Avatar asked Feb 16 '23 14:02

rah


1 Answers

Didn't try it, but might this do it?

Private Sub SaveFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk
    Dim FileToSaveAs As String = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Temp, SaveFileDialog1.FileName)
    PictureBox1.Image.Save(FileToSaveAs, System.Drawing.Imaging.ImageFormat.Jpeg)
End Sub

If you need to set encoder parameters (like jpeg compression) you will need an overload of the Save method. See http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.image.aspx and http://msdn.microsoft.com/en-us/library/system.drawing.image.aspx

The provided code saves the picturebox control in a serialized form to a file that has an extension jpeg. Renaming a text.txt file to text.jpg doesn't make it a valid jpg image. This is the same.

like image 189
nickvane Avatar answered Feb 23 '23 07:02

nickvane