Question how to open & edit & save image in picturebox

Aldiano Fdem

New member
Joined
Jun 10, 2011
Messages
3
Location
Bandung, Indonesia, Indonesia
Programming Experience
Beginner
my application is used to open a image file from computer with openFileDialog, after the image is opened to picturebox then i plot some dot/square on it then i want to save the edited one to computer

VB.NET:
'for opening a image file
 Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
        'TextBox1.Text = OpenFileDialog1.FileName
        If (OpenFileDialog1.FileName.Trim() <> "") Then
            PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
        End If
    End Sub

'for editing
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
 Dim z As Point
            z = PictureBox1.PointToClient(Cursor.Position)
            Dim aGraphics As Graphics
            Dim b As Brush = Brushes.Black
            aGraphics = PictureBox1.CreateGraphics()
            aGraphics.FillEllipse(b, z.X, z.Y, 5, 5)
            MsgBox(pushPinStat)
end sub
that is my code,the image is not edited, please give me a correction or reference code, how to save the edited image with saveFileDialog?
please help me, urgent..

thank you
 
you could Use the save method from Image class?

the image class has a save method that you can use. you can get the image from the picturebox and pass that to your graphics and call the save method afterwards like so

            Dim aGraphics As Graphics
            Dim bmpPic As Image = PictureBox1.Image

            Dim b As Brush = Brushes.Black
            aGraphics = Graphics.FromImage(bmpPic)
            aGraphics.FillEllipse(b, z.X, z.Y, 5, 5)

            'since you want to use the save dialog, just call that first and get the location of where you want to save to
            'then do something like this
            bmpPic.Save("C:\filename.jpg")
 
Create a SaveFileDialog and get the file location using .FileName property

users can specified where to save the image file

like i noted in the comment,
'since you want to use the save dialog, just call that first and get the location of where you want to save to

The saveFileDialog is just like the OpenFileDialog you used in your code. You can create a SaveFileDialog in your WYSIWYG, it should be under the dialogs in the toolbox in VS. Then just write a method to handle the FileOk Event, something like

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

    'elsewhere in your method somewhere with the save method just pass that string
    'so like
    bmpPic.Save(fileLocationName)


you can also just create a savefiledialog in your code if you choose to.

Dim diagSaveFile As New SaveFileDialog
diagSaveFile.ShowDialog()

'check for correct filename, you can use dialogresult, add a handler for the FileOk, or just check that the string is not empty
'then get the filename selected
Dim fileLocationName As String = diagSaveFile.FileName
'use it for the location
bmpPic.Save(fileLocationName)
 
Last edited:
so, example using previous codes mentioned..

heres a quick example. Say you added the savefiledialog through your WYSIWYG and then created an event for the fileOk. If you wanted to edit the image at that point and then save it, you can do something like

    Private Sub SaveFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk
        Dim fileLocationName As String = SaveFileDialog1.FileName
        Dim aGraphics As Graphics
        Dim bmpPic As Image = PictureBox1.Image

        Dim b As Brush = Brushes.Black
        aGraphics = Graphics.FromImage(bmpPic)
        aGraphics.FillEllipse(b, z.X, z.Y, 5, 5)

        bmpPic.Save(fileLocationName)
    End Sub


otherwise if you want to save it at a different point, just get the filename and store it to a variable that has the scope you need, like forexample in the class level or something. Then pass that string into the save method when you need it.

heres an example by creating the dialog in code, well either way its created in code, (VS generator just puts it in the partial class of the form)
        Dim diagSaveFile As New SaveFileDialog

        If diagSaveFile.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim fileLocationName As String = SaveFileDialog1.FileName
            Dim aGraphics As Graphics
            Dim bmpPic As Image = PictureBox1.Image

            Dim b As Brush = Brushes.Black
            aGraphics = Graphics.FromImage(bmpPic)
            aGraphics.FillEllipse(b, z.X, z.Y, 5, 5)

            bmpPic.Save(fileLocationName)
        End If

        'alternative code if you wanted to use the fileOk event
        'AddHandler diagSaveFile.FileOk, Sub()
        '                                    fileLocationName = diagSaveFile.FileName
        '                                End Sub 'lambda expression

        'or point to a method
        'AddHandler diagSaveFile.FileOk, AddressOf SaveMethod


the savemethod would include the same code or something of the sort if you used the addhandler
    'Sub SaveMethod(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs)
        ''code in here
    'End Sub
 
Back
Top