none
Having trouble with saving my drawing in jpeg format to my computer (SaveFileDialog) RRS feed

  • Question

  • Hello.

    I am having trouble saving the drawings I create on my PictureBox1 to a file in Jpeg format to my computer.

    When I run my program it pretends to save my picture but when I look where I saved it, its not there. I have a 'save picture' button on my form and the 'PictureBox1' is on my form too.

    This is the code that runs the saving part.

     Dim bmp As Bitmap

        Private Sub SavePicture_Click(sender As Object, e As EventArgs) Handles SavePicture.Click
    
            Dim SaveDLG As New SaveFileDialog
    
            SaveDLG.Filter = "Bmp File (*.bmp)|*.bmp|JPEG Files (*.jpeg)|*.jpeg|All Files|*.*"
    
            SaveDLG.Title = "Save File As"
            SaveDLG.InitialDirectory = "C:\"
    
            DialogResult = SaveDLG.ShowDialog
    
            If DialogResult = Windows.Forms.DialogResult.OK Then
    
                bmp = New Bitmap(PictureBox1.Width, PictureBox1.Height)
    
    
                PictureBox1.DrawToBitmap(bmp, PictureBox1.ClientRectangle)
    
                bmp.Save("Drawing.Jpeg", System.Drawing.Imaging.ImageFormat.Jpeg)
    
            ElseIf DialogResult = Windows.Forms.DialogResult.Cancel Then
            End If
    
    
    
        End Sub

    Thank you for the help.

    Sunday, July 22, 2018 6:04 PM

Answers

  • Hi

    You would likely find the file you saved in the Project folder as you didn't specify where to save it.

    Her is your code (slightly modified) which would save the Image with the File Name, Path chosen and file type as per user choices.

    ' Form1 with Button (SavePicture)
    ' and PictureBox1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        PictureBox1.SizeMode = PictureBoxSizeMode.Zoom
        PictureBox1.Image = Image.FromFile("C:\Users\lesha\Desktop\Plans\Dice 2\54.png")
      End Sub
      Private Sub SavePicture_Click(sender As Object, e As EventArgs) Handles SavePicture.Click
        Dim SaveDLG As New SaveFileDialog
        SaveDLG.Filter = "Bmp File (*.bmp)|*.bmp|JPEG Files (*.jpeg)|*.jpeg|All Files|*.*"
        SaveDLG.Title = "Save File As"
        SaveDLG.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
        DialogResult = SaveDLG.ShowDialog
        If DialogResult = DialogResult.OK Then
          PictureBox1.Image.Save(SaveDLG.FileName, Imaging.ImageFormat.Jpeg)
        End If
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    Sunday, July 22, 2018 6:50 PM

All replies

  • Check this:

       bmp.Save(SaveDLG.FileName)



    Sunday, July 22, 2018 6:34 PM
  • Hi

    You would likely find the file you saved in the Project folder as you didn't specify where to save it.

    Her is your code (slightly modified) which would save the Image with the File Name, Path chosen and file type as per user choices.

    ' Form1 with Button (SavePicture)
    ' and PictureBox1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        PictureBox1.SizeMode = PictureBoxSizeMode.Zoom
        PictureBox1.Image = Image.FromFile("C:\Users\lesha\Desktop\Plans\Dice 2\54.png")
      End Sub
      Private Sub SavePicture_Click(sender As Object, e As EventArgs) Handles SavePicture.Click
        Dim SaveDLG As New SaveFileDialog
        SaveDLG.Filter = "Bmp File (*.bmp)|*.bmp|JPEG Files (*.jpeg)|*.jpeg|All Files|*.*"
        SaveDLG.Title = "Save File As"
        SaveDLG.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
        DialogResult = SaveDLG.ShowDialog
        If DialogResult = DialogResult.OK Then
          PictureBox1.Image.Save(SaveDLG.FileName, Imaging.ImageFormat.Jpeg)
        End If
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    Sunday, July 22, 2018 6:50 PM
  • Thank you for the help. It saves the light gray of my form and its in my pictures folder on my computer but it doesn't have the drawing that I drew on it in the picture that I saved. I ran into this same problem when I tried saving my drawing a few days ago.

    This is the whole code of the program:

    You'll see what I mean if you make a new project and copy/paste this code with its buttons. It saves images of just light gray color and not the drawing that I made with my mouse by clicking on the picture box: I need a picture of the grid and the drawing I made inside the picture box1.

    Imports System.Drawing.Drawing2D
    
    
    
    
    Public Class Form1
    
        Dim g As System.Drawing.Graphics
        Dim r As System.Drawing.Graphics
        Dim a As System.Drawing.Graphics
    
    
        Dim col As Color
        Dim D As Integer = 2
        Dim p As Pen = New Pen(col, D)
    
    
        Dim q As Pen = New Pen(Color.Black, 0)
        Dim t As Pen = New Pen(Color.Blue, 5)
        Dim n As Pen = New Pen(Color.Black, 5)
        Dim count As Integer = 0
        Dim point1 As New Point()
        Dim point2 As New Point()
        Dim point3 As New Point()
    
        Dim myPointsArray As New List(Of PointF)
        Dim myPointsArrays(100)
    
        Dim ShowGrid As Boolean = False
        Dim Draw As Boolean = False
        Dim TurnOnDrawL As Boolean = False
        Dim StopConD As Boolean = False
        Dim Snapo As Boolean = False
    
    
        Dim isDrag As Boolean = False
        Dim theRectangle As New Rectangle(New Point(0, 0), New Size(0, 0))
        Dim startPoint As Point
    
        Dim bmp As Bitmap
    
    
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    
    
            Me.Text = "Drawing Application Version 1.0"
    
            bmp = New Bitmap(PictureBox1.Width, PictureBox1.Height)
    
            PictureBox1.Image = bmp
    
            g = PictureBox1.CreateGraphics
    
    
        End Sub
    
    
    
    
        Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
    
    
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
    
            PictureBox1.Refresh()
    
    
            Dim H, W As Integer
    
    
    
            r = PictureBox1.CreateGraphics
    
            D = Depth.Text
            W = Width.Text
            H = Height.Text
    
            p.Color = col
            p.Width = D
    
            g = PictureBox1.CreateGraphics
    
            gridon_Checked(sender, e)
    
            g.DrawRectangle(p, 100, 30, 60 * W, 60 * H)
    
            g.DrawRectangle(p, 100, 450, 60 * W, 60 * H)
    
            g.DrawRectangle(p, 588, 449, 60 * W, 60 * H)
    
            r.DrawLine(n, 500, 0, 500, 900)
    
            r.DrawLine(n, 0, 313, 878, 313)
    
    
    
    
    
    
    
    
        End Sub
    
        Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles Red.CheckedChanged
    
    
            col = Color.Red
            p.Color = col
    
        End Sub
    
        Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
    
        End Sub
    
        Private Sub White_CheckedChanged(sender As Object, e As EventArgs) Handles White.CheckedChanged
    
            col = Color.White
            p.Color = col
    
        End Sub
    
        Private Sub Blue_CheckedChanged(sender As Object, e As EventArgs) Handles Blue.CheckedChanged
    
            col = Color.Blue
            p.Color = col
    
        End Sub
    
        Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton2.CheckedChanged
            p.DashStyle = Drawing2D.DashStyle.Solid
        End Sub
    
        Private Sub RadioButton3_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton3.CheckedChanged
            p.DashStyle = Drawing2D.DashStyle.Dash
        End Sub
    
        Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
    
            Draw = CheckBox1.Checked
    
    
        End Sub
    
        Private Sub PictureBox1_MouseClick(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseClick
    
    
    
            If Draw = True Then
    
                count = count + 1
    
                If count = 1 Then
    
    
                    point1 = New Point(e.X, e.Y)
    
                End If
    
    
                If count = 2 Then
    
                    point2 = New Point(e.X, e.Y)
    
                    g.DrawLine(p, point1, point2)
    
                    point1.X = point2.X
                    point1.Y = point2.Y
    
                    count = 1
    
                End If
    
    
            End If
    
    
        End Sub
    
    
        Private Sub About_Click(sender As Object, e As EventArgs) Handles About.Click
    
            MessageBox.Show("Hi, my name is Joshua. Check out unpopularthemovie.com (It has a Bible message). Thanks for using this application. All rights, privaleges, and royalties are for Joshua Tashbar(maker of this application). Copyright created in 2018",
        "About", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation)
    
    
        End Sub
    
    
        Private Sub gridon_Checked(sender As Object, e As EventArgs) Handles gridon.CheckedChanged
    
            ShowGrid = gridon.Checked
    
            If ShowGrid = True Then
                q.Color = Color.Black
            Else
                q.Color = Color.LightGray
            End If
    
            For i As Integer = 1 To 44
                g.DrawLine(q, 50 * i, 0, 50 * i, 735)
            Next
    
            For j As Integer = 1 To 37
                g.DrawLine(q, 0, 50 * j, 878, 50 * j)
            Next
    
        End Sub
    
    
    
        Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
    
            Dim q As New Pen(Color.LightGray, D)
    
            'This z variable holds the remainder of the x coordinate of the mouse
            Dim z As Integer
    
            'This b variable holds the remainder of the y coordiante of the mouse
            Dim b As Integer
    
    
            If Snapo = True Then
    
                If count = 1 Then
    
                    point3 = New Point(e.X, e.Y)
    
                    'Here is where I do the math for the lines to go on the grid
    
                    'This are the variables to hold the remainders of the division
                    z = e.X / 50
                    b = e.Y / 50
    
                    'The reason I divide and multiply by 50 is because thats the height and width of my squares in my grid
                    'These are the x and y coordinate values for the point 3
                    point3.X = z * 50
                    point3.Y = b * 50
    
    
                    'This makes a line of whatever color the user chooses (red for example)
                    g.DrawLine(p, point1, point3)
    
                    gridon_Checked(sender, e)
    
                    'This line makes a line of lightgray so that the user cannot see it because thats what color the picture box is
                    g.DrawLine(q, point1, point3)
    
                End If
    
            End If
    
    
        End Sub
    
        Private Sub ResetButton_Click(sender As Object, e As EventArgs) Handles ResetButton.Click
    
            PictureBox1.Refresh()
    
    
        End Sub
    
        'Button to save picture
        Private Sub SavePicture_Click(sender As Object, e As EventArgs) Handles SavePicture.Click
    
            Dim SaveDLG As New SaveFileDialog
    
            SaveDLG.Filter = "Bmp File (*.bmp)|*.bmp|JPEG Files (*.jpeg)|*.jpeg|All Files|*.*"
    
            SaveDLG.Title = "Save File As"
            SaveDLG.InitialDirectory = "C:\"
    
            DialogResult = SaveDLG.ShowDialog
    
            If DialogResult = Windows.Forms.DialogResult.OK Then
    
                bmp = New Bitmap(PictureBox1.Width, PictureBox1.Height)
    
    
                PictureBox1.DrawToBitmap(bmp, PictureBox1.DisplayRectangle)
    
                bmp.Save(SaveDLG.FileName)
    
    
                bmp.Save("Drawing.Jpeg", System.Drawing.Imaging.ImageFormat.Jpeg)
    
                'Trying out Copyfromscreen
                'g.CopyFromScreen(PictureBox1.Width, PictureBox1.Height)
    
    
            ElseIf DialogResult = Windows.Forms.DialogResult.Cancel Then
            End If
    
    
    
        End Sub
    
        Private Sub LoadPicture_Click(sender As Object, e As EventArgs) Handles LoadPicture.Click
    
            If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
    
                PictureBox1.ImageLocation = OpenFileDialog1.FileName
    
            End If
    
        End Sub
    
    
        Private Sub BStop_Click(sender As Object, e As EventArgs)
    
            StopConD = True
    
    
        End Sub
    
        Private Sub Snapon_CheckedChanged(sender As Object, e As EventArgs) Handles Snapon.CheckedChanged
    
    
            Snapo = Snapon.Checked
    
        End Sub
    End Class
    
    
    

    Sunday, July 22, 2018 6:58 PM
  • Thank you for the help. It saves the light gray of my form and its in my pictures folder on my computer but it doesn't have the drawing that I drew on it in the picture that I saved. I ran into this same problem when I tried saving my drawing a few days ago.

    This is the whole code of the program:

    You'll see what I mean if you make a new project and copy/paste this code with its buttons. It saves images of just light gray color and not the drawing that I made with my mouse by clicking on the picture box: I need a picture of the grid and the drawing I made inside the picture box1.

    Imports System.Drawing.Drawing2D
    
    
    
    
    Public Class Form1
    
        Dim g As System.Drawing.Graphics
        Dim r As System.Drawing.Graphics
        Dim a As System.Drawing.Graphics
    
    
        Dim col As Color
        Dim D As Integer = 2
        Dim p As Pen = New Pen(col, D)
    
    
        Dim q As Pen = New Pen(Color.Black, 0)
        Dim t As Pen = New Pen(Color.Blue, 5)
        Dim n As Pen = New Pen(Color.Black, 5)
        Dim count As Integer = 0
        Dim point1 As New Point()
        Dim point2 As New Point()
        Dim point3 As New Point()
    
        Dim myPointsArray As New List(Of PointF)
        Dim myPointsArrays(100)
    
        Dim ShowGrid As Boolean = False
        Dim Draw As Boolean = False
        Dim TurnOnDrawL As Boolean = False
        Dim StopConD As Boolean = False
        Dim Snapo As Boolean = False
    
    
        Dim isDrag As Boolean = False
        Dim theRectangle As New Rectangle(New Point(0, 0), New Size(0, 0))
        Dim startPoint As Point
    
        Dim bmp As Bitmap
    
    
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    
    
            Me.Text = "Drawing Application Version 1.0"
    
            bmp = New Bitmap(PictureBox1.Width, PictureBox1.Height)
    
            PictureBox1.Image = bmp
    
            g = PictureBox1.CreateGraphics
    
    
        End Sub
    
    
    
    
        Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
    
    
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
    
            PictureBox1.Refresh()
    
    
            Dim H, W As Integer
    
    
    
            r = PictureBox1.CreateGraphics
    
            D = Depth.Text
            W = Width.Text
            H = Height.Text
    
            p.Color = col
            p.Width = D
    
            g = PictureBox1.CreateGraphics
    
            gridon_Checked(sender, e)
    
            g.DrawRectangle(p, 100, 30, 60 * W, 60 * H)
    
            g.DrawRectangle(p, 100, 450, 60 * W, 60 * H)
    
            g.DrawRectangle(p, 588, 449, 60 * W, 60 * H)
    
            r.DrawLine(n, 500, 0, 500, 900)
    
            r.DrawLine(n, 0, 313, 878, 313)
    
    
    
    
    
    
    
    
        End Sub
    
        Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles Red.CheckedChanged
    
    
            col = Color.Red
            p.Color = col
    
        End Sub
    
        Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
    
        End Sub
    
        Private Sub White_CheckedChanged(sender As Object, e As EventArgs) Handles White.CheckedChanged
    
            col = Color.White
            p.Color = col
    
        End Sub
    
        Private Sub Blue_CheckedChanged(sender As Object, e As EventArgs) Handles Blue.CheckedChanged
    
            col = Color.Blue
            p.Color = col
    
        End Sub
    
        Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton2.CheckedChanged
            p.DashStyle = Drawing2D.DashStyle.Solid
        End Sub
    
        Private Sub RadioButton3_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton3.CheckedChanged
            p.DashStyle = Drawing2D.DashStyle.Dash
        End Sub
    
        Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
    
            Draw = CheckBox1.Checked
    
    
        End Sub
    
        Private Sub PictureBox1_MouseClick(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseClick
    
    
    
            If Draw = True Then
    
                count = count + 1
    
                If count = 1 Then
    
    
                    point1 = New Point(e.X, e.Y)
    
                End If
    
    
                If count = 2 Then
    
                    point2 = New Point(e.X, e.Y)
    
                    g.DrawLine(p, point1, point2)
    
                    point1.X = point2.X
                    point1.Y = point2.Y
    
                    count = 1
    
                End If
    
    
            End If
    
    
        End Sub
    
    
        Private Sub About_Click(sender As Object, e As EventArgs) Handles About.Click
    
            MessageBox.Show("Hi, my name is Joshua. Check out unpopularthemovie.com (It has a Bible message). Thanks for using this application. All rights, privaleges, and royalties are for Joshua Tashbar(maker of this application). Copyright created in 2018",
        "About", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation)
    
    
        End Sub
    
    
        Private Sub gridon_Checked(sender As Object, e As EventArgs) Handles gridon.CheckedChanged
    
            ShowGrid = gridon.Checked
    
            If ShowGrid = True Then
                q.Color = Color.Black
            Else
                q.Color = Color.LightGray
            End If
    
            For i As Integer = 1 To 44
                g.DrawLine(q, 50 * i, 0, 50 * i, 735)
            Next
    
            For j As Integer = 1 To 37
                g.DrawLine(q, 0, 50 * j, 878, 50 * j)
            Next
    
        End Sub
    
    
    
        Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
    
            Dim q As New Pen(Color.LightGray, D)
    
            'This z variable holds the remainder of the x coordinate of the mouse
            Dim z As Integer
    
            'This b variable holds the remainder of the y coordiante of the mouse
            Dim b As Integer
    
    
            If Snapo = True Then
    
                If count = 1 Then
    
                    point3 = New Point(e.X, e.Y)
    
                    'Here is where I do the math for the lines to go on the grid
    
                    'This are the variables to hold the remainders of the division
                    z = e.X / 50
                    b = e.Y / 50
    
                    'The reason I divide and multiply by 50 is because thats the height and width of my squares in my grid
                    'These are the x and y coordinate values for the point 3
                    point3.X = z * 50
                    point3.Y = b * 50
    
    
                    'This makes a line of whatever color the user chooses (red for example)
                    g.DrawLine(p, point1, point3)
    
                    gridon_Checked(sender, e)
    
                    'This line makes a line of lightgray so that the user cannot see it because thats what color the picture box is
                    g.DrawLine(q, point1, point3)
    
                End If
    
            End If
    
    
        End Sub
    
        Private Sub ResetButton_Click(sender As Object, e As EventArgs) Handles ResetButton.Click
    
            PictureBox1.Refresh()
    
    
        End Sub
    
        'Button to save picture
        Private Sub SavePicture_Click(sender As Object, e As EventArgs) Handles SavePicture.Click
    
            Dim SaveDLG As New SaveFileDialog
    
            SaveDLG.Filter = "Bmp File (*.bmp)|*.bmp|JPEG Files (*.jpeg)|*.jpeg|All Files|*.*"
    
            SaveDLG.Title = "Save File As"
            SaveDLG.InitialDirectory = "C:\"
    
            DialogResult = SaveDLG.ShowDialog
    
            If DialogResult = Windows.Forms.DialogResult.OK Then
    
                bmp = New Bitmap(PictureBox1.Width, PictureBox1.Height)
    
    
                PictureBox1.DrawToBitmap(bmp, PictureBox1.DisplayRectangle)
    
                bmp.Save(SaveDLG.FileName)
    
    
                bmp.Save("Drawing.Jpeg", System.Drawing.Imaging.ImageFormat.Jpeg)
    
                'Trying out Copyfromscreen
                'g.CopyFromScreen(PictureBox1.Width, PictureBox1.Height)
    
    
            ElseIf DialogResult = Windows.Forms.DialogResult.Cancel Then
            End If
    
    
    
        End Sub
    
        Private Sub LoadPicture_Click(sender As Object, e As EventArgs) Handles LoadPicture.Click
    
            If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
    
                PictureBox1.ImageLocation = OpenFileDialog1.FileName
    
            End If
    
        End Sub
    
    
        Private Sub BStop_Click(sender As Object, e As EventArgs)
    
            StopConD = True
    
    
        End Sub
    
        Private Sub Snapon_CheckedChanged(sender As Object, e As EventArgs) Handles Snapon.CheckedChanged
    
    
            Snapo = Snapon.Checked
    
        End Sub
    End Class
    
    
    

    Sunday, July 22, 2018 6:58 PM
  • Thank you for the help. It saves the light gray of my form and its in my pictures folder on my computer but it doesn't have the drawing that I drew on it in the picture that I saved. I ran into this same problem when I tried saving my drawing a few days ago.

    This is the whole code of the program:

    You'll see what I mean if you make a new project and copy/paste this code with its buttons. It saves images of just light gray color and not the drawing that I made with my mouse by clicking on the picture box: I need a picture of the grid and the drawing I made inside the picture box1.

    Imports System.Drawing.Drawing2D
    
    
    
    
    Public Class Form1
    
        Dim g As System.Drawing.Graphics
        Dim r As System.Drawing.Graphics
        Dim a As System.Drawing.Graphics
    
    
        Dim col As Color
        Dim D As Integer = 2
        Dim p As Pen = New Pen(col, D)
    
    
        Dim q As Pen = New Pen(Color.Black, 0)
        Dim t As Pen = New Pen(Color.Blue, 5)
        Dim n As Pen = New Pen(Color.Black, 5)
        Dim count As Integer = 0
        Dim point1 As New Point()
        Dim point2 As New Point()
        Dim point3 As New Point()
    
        Dim myPointsArray As New List(Of PointF)
        Dim myPointsArrays(100)
    
        Dim ShowGrid As Boolean = False
        Dim Draw As Boolean = False
        Dim TurnOnDrawL As Boolean = False
        Dim StopConD As Boolean = False
        Dim Snapo As Boolean = False
    
    
        Dim isDrag As Boolean = False
        Dim theRectangle As New Rectangle(New Point(0, 0), New Size(0, 0))
        Dim startPoint As Point
    
        Dim bmp As Bitmap
    
    
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    
    
            Me.Text = "Drawing Application Version 1.0"
    
            bmp = New Bitmap(PictureBox1.Width, PictureBox1.Height)
    
            PictureBox1.Image = bmp
    
            g = PictureBox1.CreateGraphics
    
    
        End Sub
    
    
    
    
        Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
    
    
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
    
            PictureBox1.Refresh()
    
    
            Dim H, W As Integer
    
    
    
            r = PictureBox1.CreateGraphics
    
            D = Depth.Text
            W = Width.Text
            H = Height.Text
    
            p.Color = col
            p.Width = D
    
            g = PictureBox1.CreateGraphics
    
            gridon_Checked(sender, e)
    
            g.DrawRectangle(p, 100, 30, 60 * W, 60 * H)
    
            g.DrawRectangle(p, 100, 450, 60 * W, 60 * H)
    
            g.DrawRectangle(p, 588, 449, 60 * W, 60 * H)
    
            r.DrawLine(n, 500, 0, 500, 900)
    
            r.DrawLine(n, 0, 313, 878, 313)
    
    
    
    
    
    
    
    
        End Sub
    
        Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles Red.CheckedChanged
    
    
            col = Color.Red
            p.Color = col
    
        End Sub
    
        Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
    
        End Sub
    
        Private Sub White_CheckedChanged(sender As Object, e As EventArgs) Handles White.CheckedChanged
    
            col = Color.White
            p.Color = col
    
        End Sub
    
        Private Sub Blue_CheckedChanged(sender As Object, e As EventArgs) Handles Blue.CheckedChanged
    
            col = Color.Blue
            p.Color = col
    
        End Sub
    
        Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton2.CheckedChanged
            p.DashStyle = Drawing2D.DashStyle.Solid
        End Sub
    
        Private Sub RadioButton3_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton3.CheckedChanged
            p.DashStyle = Drawing2D.DashStyle.Dash
        End Sub
    
        Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
    
            Draw = CheckBox1.Checked
    
    
        End Sub
    
        Private Sub PictureBox1_MouseClick(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseClick
    
    
    
            If Draw = True Then
    
                count = count + 1
    
                If count = 1 Then
    
    
                    point1 = New Point(e.X, e.Y)
    
                End If
    
    
                If count = 2 Then
    
                    point2 = New Point(e.X, e.Y)
    
                    g.DrawLine(p, point1, point2)
    
                    point1.X = point2.X
                    point1.Y = point2.Y
    
                    count = 1
    
                End If
    
    
            End If
    
    
        End Sub
    
    
        Private Sub About_Click(sender As Object, e As EventArgs) Handles About.Click
    
            MessageBox.Show("Hi, my name is Joshua. Check out unpopularthemovie.com (It has a Bible message). Thanks for using this application. All rights, privaleges, and royalties are for Joshua Tashbar(maker of this application). Copyright created in 2018",
        "About", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation)
    
    
        End Sub
    
    
        Private Sub gridon_Checked(sender As Object, e As EventArgs) Handles gridon.CheckedChanged
    
            ShowGrid = gridon.Checked
    
            If ShowGrid = True Then
                q.Color = Color.Black
            Else
                q.Color = Color.LightGray
            End If
    
            For i As Integer = 1 To 44
                g.DrawLine(q, 50 * i, 0, 50 * i, 735)
            Next
    
            For j As Integer = 1 To 37
                g.DrawLine(q, 0, 50 * j, 878, 50 * j)
            Next
    
        End Sub
    
    
    
        Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
    
            Dim q As New Pen(Color.LightGray, D)
    
            'This z variable holds the remainder of the x coordinate of the mouse
            Dim z As Integer
    
            'This b variable holds the remainder of the y coordiante of the mouse
            Dim b As Integer
    
    
            If Snapo = True Then
    
                If count = 1 Then
    
                    point3 = New Point(e.X, e.Y)
    
                    'Here is where I do the math for the lines to go on the grid
    
                    'This are the variables to hold the remainders of the division
                    z = e.X / 50
                    b = e.Y / 50
    
                    'The reason I divide and multiply by 50 is because thats the height and width of my squares in my grid
                    'These are the x and y coordinate values for the point 3
                    point3.X = z * 50
                    point3.Y = b * 50
    
    
                    'This makes a line of whatever color the user chooses (red for example)
                    g.DrawLine(p, point1, point3)
    
                    gridon_Checked(sender, e)
    
                    'This line makes a line of lightgray so that the user cannot see it because thats what color the picture box is
                    g.DrawLine(q, point1, point3)
    
                End If
    
            End If
    
    
        End Sub
    
        Private Sub ResetButton_Click(sender As Object, e As EventArgs) Handles ResetButton.Click
    
            PictureBox1.Refresh()
    
    
        End Sub
    
        'Button to save picture
        Private Sub SavePicture_Click(sender As Object, e As EventArgs) Handles SavePicture.Click
    
            Dim SaveDLG As New SaveFileDialog
    
            SaveDLG.Filter = "Bmp File (*.bmp)|*.bmp|JPEG Files (*.jpeg)|*.jpeg|All Files|*.*"
    
            SaveDLG.Title = "Save File As"
            SaveDLG.InitialDirectory = "C:\"
    
            DialogResult = SaveDLG.ShowDialog
    
            If DialogResult = Windows.Forms.DialogResult.OK Then
    
                bmp = New Bitmap(PictureBox1.Width, PictureBox1.Height)
    
    
                PictureBox1.DrawToBitmap(bmp, PictureBox1.DisplayRectangle)
    
                bmp.Save(SaveDLG.FileName)
    
    
                bmp.Save("Drawing.Jpeg", System.Drawing.Imaging.ImageFormat.Jpeg)
    
                'Trying out Copyfromscreen
                'g.CopyFromScreen(PictureBox1.Width, PictureBox1.Height)
    
    
            ElseIf DialogResult = Windows.Forms.DialogResult.Cancel Then
            End If
    
    
    
        End Sub
    
        Private Sub LoadPicture_Click(sender As Object, e As EventArgs) Handles LoadPicture.Click
    
            If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
    
                PictureBox1.ImageLocation = OpenFileDialog1.FileName
    
            End If
    
        End Sub
    
    
        Private Sub BStop_Click(sender As Object, e As EventArgs)
    
            StopConD = True
    
    
        End Sub
    
        Private Sub Snapon_CheckedChanged(sender As Object, e As EventArgs) Handles Snapon.CheckedChanged
    
    
            Snapo = Snapon.Checked
    
        End Sub
    End Class
    
    
    

    Sunday, July 22, 2018 6:58 PM
  • Josh,

    You are stuck where we all get stuck. You should go slow and do a simple example first.

    You need to learn how to PERSIST the drawing.

    There is a difference between drawing temporary on the video screen and drawing persisted or more permanent on a bitmap saved in memory.

    Review the difference between a drawing surface and a bitmap. A drawing surface is a temporary memory area the system uses while a bitmap is a more permanent image being saved in a declared application memory area.

    The graphics drawing surface is like the first step layer in creating an image bitmap. Then you can show that drawing on the video screen or save it as a bitmap file.

    When you draw a line or any gdi+ drawing on a graphics surface (like using e.graphics in a paint event) that image is shown on the video screen temporary until cleared in the next paint event.

    When you draw on a graphics surface that is declared from a bitmap the drawing gets added to the bitmap and therefore saved in memory or persisted. However that new drawing is not shown on the screen. The two are not connected. The bitmap needs to be drawn on video screen by using e.graphics (or copy the bitmap to the picturebox.image property).

    So you have to get your drawing image on the video screen as required. And if needed, you have to get your drawing onto your bitmap memory image. Then the bitmap can be saved to disk or shown on the video screen.

    See? Two things. Several ways of combining and arranging them.

    In your example posted here you are mix drawing from the button click, the mouse events, etc. Then, each time you call picturebox.refresh, all the drawing you have done is being lost or cleared because the entire drawing surface is cleared and redrawn anytime the paint event is triggered.

    In a similar way, when you try to capture the picturebox image your drawing is being lost.

    You are only drawing on a temporary screen image when using the g graphicssurface in this example's buttonclick and mousemove events. You need to persist the image by drawing the entire image, grid and lines,  everytime the paint event fires and/or everytime you make a bitmap.

    You should do all the drawing at one time in the paint event with e.graphics as I showed you in your last question. Even make a routine to accept a graphics object and draw on it. Then you can call the drawing routine from the paint event passing e.graphics, and you can use  a button event to call the drawing routine with g made using creategraphics for the picturebox. Then save that bitmap that has been drawn on to a disk file.


    So you need to save the line positions and redraw them each paint event as I showed you in your last question.

    Or you need to draw the lines on the same one permanent bitmap everytime you draw your line.

    However I would just use the paint event and save the line coordinates etc. That is scalable vector drawing like a CAD program, Using a bitmap produces only dots like a paint program ie photograph, which is ok if that is what you want.

    Sunday, July 22, 2018 9:01 PM
  • Thank you Mr. Tom for the help
    Sunday, July 22, 2018 11:35 PM
  • Thank you Mr. Tom for the help

    Here is what I tried to describe.

    The example has a renderscene sub routine that draws the same graphics on either the paint event graphics or a bitmap. You can save the bitmap and edit that in a paint program. For true cad drawing you would save the vector line coordinates to a disk file for future use.

    For a paint type drawing you would only save and draw the bitmap dot colors to edit in the future (not shown).

    So those are two different types of drawing. Cad and Paint.

    The example makes the controls, just paste the code into an empty form.

    'lmb draws line, rmb resets draw line
    Public Class Form3
        'create the controls
        Private WithEvents PictureBox1 As New PictureBox With {.Parent = Me, .Location = New Point(10, 40), .Size = New Size(200, 120)}
        Private WithEvents PictureBox2 As New PictureBox With {.Parent = Me, .Location = New Point(10, 180), .Size = New Size(250, 170)}
        Private WithEvents Button1 As New Button With {.Parent = Me, .Location = New Point(10, 10), .Text = "Copy Bmp"}
        Private MouseDownPt, MouseMovePt As PointF
        Private MouseStage As Integer
    
        Private Class LineX
            Public Pt1 As PointF
            Public Pt2 As PointF
        End Class
    
        Private Lines As New List(Of LineX)
    
        Private Sub Form3_Load(sender As Object, e As EventArgs) Handles Me.Load
            ClientSize = New Size(300, 400)
        End Sub
    
        Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
            'draw on the e.graphics drawing surface given by the system
            RenderScene(e.Graphics)
        End Sub
    
        Private Sub RenderScene(g As Graphics)
            With g
                .Clear(Color.Black)
                .SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
    
                Using p As New Pen(Color.LightBlue, 2)
                    'draw all lines
                    For Each lx As LineX In Lines
                        .DrawLine(p, lx.Pt1, lx.Pt2)
                    Next
    
                    'draw the tracer if drawing
                    If MouseStage = 1 Then
                        p.DashStyle = Drawing2D.DashStyle.Dash
                        p.Color = Color.Yellow
                        .DrawLine(p, MouseDownPt, MouseMovePt)
                    End If
    
                End Using
            End With
        End Sub
    
        Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
            MouseMovePt = e.Location
    
            Select Case MouseStage
                Case 0
                    If e.Button = MouseButtons.Left Then
                        'start drawing
                        MouseDownPt = e.Location
                        MouseStage = 1
                    End If
    
                Case Else    'right mouse button cancel drawing
                    If e.Button <> MouseButtons.Left Then
                        MouseStage = 0
                        PictureBox1.Invalidate()
                    End If
            End Select
        End Sub
    
        Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
            If MouseStage = 1 Then
                MouseMovePt = e.Location
                PictureBox1.Invalidate()
            End If
        End Sub
    
        Private Sub PictureBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUp
            If MouseStage = 1 Then
                MouseMovePt = e.Location
                Dim lx As New LineX
                With lx
                    .Pt1 = MouseDownPt
                    .Pt2 = MouseMovePt
                End With
    
                Lines.Add(lx)
    
                MouseDownPt = MouseMovePt
    
                PictureBox1.Invalidate()
            End If
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            'draw on a bitmap for pictuebox 2
            'create a memory bitmap and graphics surface
            Dim bmp As New Bitmap(PictureBox2.ClientSize.Width, PictureBox2.ClientSize.Height),
                   g As Graphics = Graphics.FromImage(bmp)
    
            'draw the vector graphics on the bitmap via the drawing surface graphics object
            RenderScene(g)
    
            'reference the bitmap in the picturebox
            PictureBox2.Image = bmp
    
            'to do: dispose the image as required
    
        End Sub
    End Class
    


    Monday, July 23, 2018 12:31 PM
  • Thank you again for the help. I think what I should do is by saving the CAD drawing way. Thank you for the example.
    Monday, July 23, 2018 3:02 PM
  • Thank you again for the help. I think what I should do is by saving the CAD drawing way.

    Thank you for the example.

    Joshua,

    You are welcome.

    It all depends on the exact results one wants. But you have to learn the alternates and positives and negatives to be able to switch and decide exactly whats best for each case etc.

    I want to clarify wording, when I say CAD drawing I mean VECTOR CAD. Where the drawing screen objects consist of vectors (magnitude and direction). So lines and circles and text and even a bitmap you show and can move and resize on the screen as a vector rectangle with a bitmap in it. The vectors are simply the magnitude (size) and direction (coordinates and other object geometry) of the objects.

    For now you just need to save a text data file with the vector data (ie the line coordinates).

    There are hundreds of ways to do all of it. For me it always boils down to this same basic drawing framework that can be expanded any direction one desires.

    Finally you should mark Viorel's and Leshay's posts as the correct answer to your original question of saving the bitmap file using the mark as answer link at the bottom of their forum posts. That will close out this thread for future reference.

    :)

    Monday, July 23, 2018 3:40 PM