vb.net 如何将图片框中的图像保存为jpg

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16488802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:53:12  来源:igfitidea点击:

How to save an image from a picturebox to jpg

vb.netsystem.drawingsavefiledialog

提问by Boats

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

我已经能够将文件保存为 .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

回答by nickvane

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.aspxand http://msdn.microsoft.com/en-us/library/system.drawing.image.aspx

如果您需要设置编码器参数(如 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

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.

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

回答by frs

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