Results 1 to 40 of 42

Thread: Very Simple Drawing Program

Threaded View

  1. #1

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Very Simple Drawing Program

    C# version here, courtesy of ForumAccount.

    The following code can be used as the basis for a simple drawing program. It assumes that you have a PictureBox on your form named PictureBox1. The Save and Clear methods can be called from wherever you like. The most likely candidates would be the Click event handlers of either Buttons or menu items. I wrote this in VB 2005. For earlier versions you'd have to use an ArrayList instead of a List(Of Line) or else derive your own LineCollection class from CollectionBase.
    VB.NET Code:
    1. Public Class Form1
    2.  
    3.     'The lines that have been drawn but not saved.
    4.     Private lines As New List(Of Line)
    5.  
    6.     'The start point of the line currently being drawn.
    7.     Private start As Point
    8.  
    9.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    10.         'Place a blank image in the PictureBox control.
    11.         Me.PictureBox1.Image = New Bitmap(Me.PictureBox1.Width, Me.PictureBox1.Height)
    12.     End Sub
    13.  
    14.     Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
    15.         'Remember the point at which the line started.
    16.         Me.start = e.Location
    17.     End Sub
    18.  
    19.     Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
    20.         'Remember the point at which the line ended.
    21.         Dim [end] As Point = e.Location
    22.  
    23.         'Add the new line to the list.
    24.         Me.lines.Add(New Line(Me.start, [end]))
    25.  
    26.         Dim area As New Rectangle(Math.Min(Me.start.X, [end].X), _
    27.                                   Math.Min(Me.start.Y, [end].Y), _
    28.                                   Math.Abs(Me.start.X - [end].X), _
    29.                                   Math.Abs(Me.start.Y - [end].Y))
    30.  
    31.         'Inflate the rectangle by 1 pixel in each direction to ensure every changed pixel will be redrawn.
    32.         area.Inflate(1, 1)
    33.  
    34.         'Force the control to repaint so the new line is drawn.
    35.         Me.PictureBox1.Invalidate(area)
    36.         Me.PictureBox1.Update()
    37.     End Sub
    38.  
    39.     Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
    40.         'Draw each line on the control.
    41.         Me.DrawLines(e.Graphics)
    42.     End Sub
    43.  
    44.     Private Sub Save()
    45.         'Create a Graphics object from the Image in the PictureBox.
    46.         Using g As Graphics = Graphics.FromImage(Me.PictureBox1.Image)
    47.             'Draw each line on the image to make them permanent.
    48.             Me.DrawLines(g)
    49.         End Using
    50.  
    51.         'Clear the temporary lines that were just saved.
    52.         Me.Clear()
    53.     End Sub
    54.  
    55.     Private Sub Clear()
    56.         'Clear all unsaved lines.
    57.         Me.lines.Clear()
    58.  
    59.         'Force the control to repaint so the lines are removed.
    60.         Me.PictureBox1.Refresh()
    61.     End Sub
    62.  
    63.     Private Sub DrawLines(ByVal g As Graphics)
    64.         For Each line As Line In Me.lines
    65.             g.DrawLine(Pens.Black, line.Start, line.End)
    66.         Next line
    67.     End Sub
    68.  
    69. End Class
    70.  
    71. Public Class Line
    72.  
    73.     'The line's start point.
    74.     Private _start As Point
    75.  
    76.     'The line's end point.
    77.     Private _end As Point
    78.  
    79.     'The line's start point.
    80.     Public Property Start() As Point
    81.         Get
    82.             Return Me._start
    83.         End Get
    84.         Set(ByVal value As Point)
    85.             Me._start = value
    86.         End Set
    87.     End Property
    88.  
    89.     'The line's end point.
    90.     Public Property [End]() As Point
    91.         Get
    92.             Return Me._end
    93.         End Get
    94.         Set(ByVal value As Point)
    95.             Me._end = value
    96.         End Set
    97.     End Property
    98.  
    99.     Public Sub New()
    100.         Me.New(Point.Empty, Point.Empty)
    101.     End Sub
    102.  
    103.     Public Sub New(ByVal start As Point, ByVal [end] As Point)
    104.         Me._start = start
    105.         Me._end = [end]
    106.     End Sub
    107.  
    108. End Class
    I strongly recommend that you now go down and read post #17 for some explanation.
    Last edited by jmcilhinney; Oct 12th, 2017 at 08:08 PM. Reason: Added call to Update after Invalidate to force redraw.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width