如何将图像从图片框保存到jpg

时间:2013-05-10 18:40:07

标签: vb.net system.drawing savefiledialog

我能够将文件保存为.jpeg,但图片无法加载,是否有人有建议?

    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

2 个答案:

答案 0 :(得分:4)

没试过,但可能会这样做吗?

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

如果需要设置编码器参数(如jpeg压缩),则需要重载Save方法。请参阅http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.image.aspxhttp://msdn.microsoft.com/en-us/library/system.drawing.image.aspx

提供的代码将图片框控件以序列化形式保存到具有扩展名jpeg的文件中。将text.txt文件重命名为text.jpg并不会使其成为有效的jpg图像。这是一样的。

答案 1 :(得分:-1)

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