auto save image

caution021

Member
Joined
Dec 16, 2011
Messages
5
Programming Experience
Beginner
i have here a code that allows me to draw on a picturebox, but my problem is i want it to save on a specific directory for example: "C:\image". how can i do this? thanks in advance

VB.NET:
[/FONT][/COLOR]Imports SystemImports System.Drawing
Imports System.Drawing.Imaging
Imports System.Windows.Forms
Public Class Signature


    Inherits System.Windows.Forms.Form


    Dim mousePath As New System.Drawing.Drawing2D.GraphicsPath()


    Dim myAlpha As Integer = 100
    Dim myUserColor As New Color()
    Dim myPenWidth As Single = 2


    Private clearImage As Boolean


    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseDown


        If e.Button = MouseButtons.Left Then


            mousePath.StartFigure()


        End If


    End Sub
    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove


        If e.Button = MouseButtons.Left Then


            Try
                mousePath.AddLine(e.X, e.Y, e.X, e.Y)


            Catch
                MsgBox("No way, Hose!")
            End Try


        End If


        PictureBox1.Invalidate()


    End Sub


    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint




        If clearImage Then
            clearImage = False
            e.Graphics.Clear(Color.White)
        End If




        Try


            myUserColor = (System.Drawing.Color.Black)


            myAlpha = 255


            Dim CurrentPen = New Pen(Color.FromArgb(myAlpha, myUserColor), myPenWidth) 'Set up the pen


            e.Graphics.DrawPath(CurrentPen, mousePath)  'draw the path! :)
        Catch
            MsgBox("Not happening!")
        End Try
    End Sub


    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        clearImage = True
        PictureBox1.Invalidate()
        mousePath.Reset()
    End Sub


    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If SaveFileDialog1.ShowDialog = DialogResult.OK Then
            Dim B As New Bitmap(PictureBox1.Width, PictureBox1.Height)
            PictureBox1.DrawToBitmap(B, PictureBox1.Bounds)
            B.Save(SaveFileDialog1.FileName)
            clearImage = True
            PictureBox1.Invalidate()
            mousePath.Reset()
            Me.Close()
        End If
    End Sub

End Class[COLOR=#000000][FONT=verdana]
 
Perhaps you could replace SaveFileDialog1 with an InputBox asking a name for a file, and then build your own complete path on a new string, adding the folder/directory you have previously chosen to User's entry. You could also nest this Inputbox in a Do-Loop in order to assure that User's entry is either a valid filename, and not previously taken for an existent file, or blank, which would cancel the save operation.
 
To be specific, something like this:

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim StringImageName As String, StringImagesFolder As String = "C:\Image\"
        Do
            StringImageName = InputBox("Save current image named as:", "Please type a name to save the picture").Trim
            If StringImageName = "" Then
                MsgBox("A valid name was not provided. Therefore, the picture was not saved.")
                Exit Sub
            End If
            If StringImageName Like "*[<\|/>:?]*" Then
                MsgBox("A valid name must not contain the following characters: < \ | / > : ?")
            Else
                If Not StringImageName.ToLower.EndsWith(".bmp") Then StringImageName &= ".bmp"
                If (Not FileIO.FileSystem.FileExists(StringImagesFolder & StringImageName)) OrElse
                MsgBox("There is already a file with the name you have chosen (" & StringImageName & "). Do you want to overwrite it?",
                MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                    Exit Do
                End If
            End If
        Loop
        Dim B As New Bitmap(PictureBox1.Width, PictureBox1.Height)
        PictureBox1.DrawToBitmap(B, PictureBox1.Bounds)
        B.Save(StringImagesFolder & StringImageName)
        clearImage = True
        PictureBox1.Invalidate()
        mousePath.Reset()
        Me.Close()
    End Sub

 
thanks man it works, but i do it in a different way, i use a textbox on the other form that serves as the ID number and the primary key for my database for the filename instead of using a inputbox, but anyway thanks for the idea.
 
Yes, i'm happy to know that it worked. I suggested the InputBox as something that fitted inside the sub you posted, without harming the rest of the code. But if it was my app, I would rather use a TextBox in the form, too. Bye.
 

Latest posts

Back
Top