Save an Image...

MFS

Member
Joined
Feb 6, 2007
Messages
10
Programming Experience
Beginner
Hello,

I have a question about saving an image from a picturebox. I want to have a button that opens a dialog for users to pick an image from their computer and load it into a picturebox. Then I would like the image to be saved to a different directory either by using another button or in the same step as the first button.

I have sifted through numerous threads and have tried to work the solutions and I keep getting this generic GDI+ error and I have no idea what it means or what I am doing wrong. Please know that I have no formal training or education in programming. I am trying to learn as I go so any links or suggestions would be greatly appreciated.

I'm sorry if this is not the right forum for this question. If not can someone please direct me where to go?

Thank you,
MFS
 
Try this:
VB.NET:
Imports System.IO

Public Class frmMain
    Dim NewName As String
    Dim NewPath As String = "c:\test"
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        With OpenFileDialog1
            If .ShowDialog = Windows.Forms.DialogResult.OK Then
                Me.PictureBox1.Image = Image.FromFile(.FileName)
                Dim fi As FileInfo = New FileInfo(.FileName)
                NewName = NewPath & fi.Name
                'for the fi.copyto to work the folder you are sending it to has to already be created so a check that it exists
                If My.Computer.FileSystem.DirectoryExists (NewPath) = False Then
                    My.Computer.FileSystem.CreateDirectory( NewPath )
                End If
                fi.CopyTo(NewName)
            End If
        End With
    End Sub
End Class
 
Thank you for the reply.

That is a great start to what I am looking for. I have another question though. If I wanted to add a textbox or label to this form and have it display the original location of the picture that was just added to the picbox then how would I display that?

I really appreciate the help, thank you.

MFS
 
just add what's in red:
VB.NET:
Imports System.IO

Public Class frmMain
    Dim NewName As String
    Dim NewPath As String = "c:\test"

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        With OpenFileDialog1
            If .ShowDialog = Windows.Forms.DialogResult.OK Then
                Me.PictureBox1.Image = Image.FromFile(.FileName)
                Dim fi As FileInfo = New FileInfo(.FileName)
                [COLOR="Red"]TextBox1.Text = fi.DirectoryName[/COLOR]
                NewName = NewPath & fi.Name
                'for the fi.copyto to work the folder you are sending it to has to already be created so a check that it exists
                If My.Computer.FileSystem.DirectoryExists(NewPath) = False Then
                    My.Computer.FileSystem.CreateDirectory(NewPath)
                End If
                fi.CopyTo(NewName)
            End If
        End With
    End Sub
End Class
 
Back
Top