Hello guys, since I'm new here, I did some research to find similar problems but I didn't find what I needed. Well, my problem follows as this:

I created a Picture Viewing application in VB.NET and I want to add two functions: a save function and a function that lets the user "clear" the current picture.

Please help me :-/
Jimmy

This should do the trick:

Public Class Form1

	Private Sub OpenPicture()
		If (OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
			PictureBox1.SuspendLayout()
			ClearPicture()
			PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
			PictureBox1.ResumeLayout()
		End If
	End Sub

	Private Sub ClearPicture()
		Dim img As Image = PictureBox1.Image
		img = PictureBox1.Image
		PictureBox1.Image = Nothing
		If Not (img Is Nothing) Then
			img.Dispose()
		End If
	End Sub

	Private Sub SavePicture()
		If (PictureBox1.Image Is Nothing) Then
			MessageBox.Show("No picture to save!")
			Return
		End If

		If (SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
			If (System.IO.File.Exists(SaveFileDialog1.FileName)) Then
				System.IO.File.Delete(SaveFileDialog1.FileName)
			End If

			PictureBox1.Image.Save(SaveFileDialog1.FileName)
		End If

	End Sub

	Private Sub ButtonOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonOpen.Click
		OpenPicture()
	End Sub

	Private Sub ButtonSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSave.Click
		SavePicture()
	End Sub

	Private Sub ButtonClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonClear.Click
		ClearPicture()
	End Sub
End Class

I have attached the full solution (you can download it at the bottom of the post).

You're welcome :)

Please mark this thread as solved if you have found an answer to your original question and good luck!

Private Sub SavePicture()
        If (PictureBox1.Image Is Nothing) Then
            MessageBox.Show("No picture to save!")
            Return
        End If

        If (SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
            If (System.IO.File.Exists(SaveFileDialog1.FileName)) Then
                System.IO.File.Delete(SaveFileDialog1.FileName)
            End If

            PictureBox1.Image.Save(SaveFileDialog1.FileName + ".jpg")       
 End If

this is the correct update that u need (the red line)

Edited 7 Years Ago by John A: added code tags

Comments
no, you dont

Private Sub SavePicture()
If (PictureBox1.Image Is Nothing) Then
MessageBox.Show("No picture to save!")
Return
End If

If (SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
If (System.IO.File.Exists(SaveFileDialog1.FileName)) Then
System.IO.File.Delete(SaveFileDialog1.FileName)
End If

PictureBox1.Image.Save(SaveFileDialog1.FileName + ".jpg")
End If

this is the correct update that u need (the red line)

No, you don't. This is saving the images as bitmaps and the save file dialog is set to automatically append the file extension. So what good would a file.bmp.jpg do you if the file format is a bitmap?

Edited 7 Years Ago by sknake: n/a

Comments
Nice remark.
This question has already been answered. Start a new discussion instead.