drawing in a picturebox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lab3terch
    New Member
    • Oct 2006
    • 22

    #1

    drawing in a picturebox

    Imports System.Drawing. Graphics

    Public Class Form1
    Inherits System.Windows. Forms.Form
    Dim x1, y1, x2, y2 As Integer
    Dim red, black, blue, green, orange As Color
    Dim x, y, w, h As Integer
    Dim rectangle, freehand As Graphics
    Dim line As Graphics
    Dim wide, n As Integer
    Dim myPen As Pen

    #Region " Windows Form Designer generated code "


    Private Sub Form1_Resize(By Val sender As Object, ByVal e As System.EventArg s) Handles MyBase.Resize
    'prevents the user from making the form smaller than its original size
    If Me.Width < 368 Then Me.Width = 368
    If Me.Height < 312 Then Me.Height = 312
    End Sub

    Private Sub BtnClear_Click( ByVal sender As System.Object, ByVal e As System.EventArg s) Handles BtnClear.Click
    Picbox.Image = Nothing 'clears picture box


    End Sub

    Private Sub Picbox_MouseDow n(ByVal sender As Object, ByVal e As System.Windows. Forms.MouseEven tArgs) Handles Picbox.MouseDow n
    'gets the co-ordinates of when the mouse button is pressed
    x1 = Location.X
    y1 = Location.Y


    End Sub


    Private Sub Picbox_MouseUp( ByVal sender As Object, ByVal e As System.Windows. Forms.MouseEven tArgs) Handles Picbox.MouseUp
    'gets the co-ordinates of when the mouse button is released
    x2 = Location.X
    y2 = Location.Y


    End Sub


    Private Sub Btndraw_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Btndraw.Click

    Dim mygraphics As Graphics
    Dim mycoordinates As Integer
    mygraphics = Picbox.CreateGr aphics
    Dim mywidth As Integer
    Dim mycolor As Color
    Dim myrectangle As Rectangle
    Dim mytool As Graphics
    Dim myline As Graphics


    ' gets the user selected color
    If CmbColor.Text.E quals(black) Then
    mycolor = black
    ElseIf CmbColor.Text.E quals(red) Then
    mycolor = red
    ElseIf CmbColor.Text.E quals(blue) Then
    mycolor = blue
    ElseIf CmbColor.Text.E quals(green) Then
    mycolor = green
    ElseIf CmbColor.Text.E quals(orange) Then
    mycolor = orange
    End If

    ' gets the user selected width
    If CmbWidth.Text = CStr(1) Then
    mywidth = 1
    ElseIf CmbWidth.Text = CStr(2) Then
    mywidth = 2
    ElseIf CmbWidth.Text = CStr(3) Then
    mywidth = 3
    ElseIf CmbWidth.Text = CStr(4) Then
    mywidth = 4
    ElseIf CmbWidth.Text = CStr(5) Then
    mywidth = 5
    End If

    'gets the user selected drawing tool
    If CmbTool.Text Is line Then
    mytool = line
    ElseIf CmbTool.Text Is rectangle Then
    mytool = rectangle
    ElseIf CmbTool.Text Is freehand Then
    mytool = freehand
    End If


    myPen = New Pen(mycolor, mywidth)
    ' mytool = (myline)

    myPen = New Pen(Color:=Colo r.CadetBlue, Width:=4)
    mygraphics.Draw Line(Pen:=myPen , x1:=35, y1:=35, x2:=300, y2:=400)


    myrectangle = New Rectangle(x:=30 , y:=30, Width:=80, Height:=160)
    mygraphics.Draw Rectangle(Pen:= myPen, rect:=myrectang le)

    End Sub
    End Class
  • lab3terch
    New Member
    • Oct 2006
    • 22

    #2
    For some reason my text did not post with the code - so here it is! I am finishing up a class in VB and would like some assistance in a program. I need to do the following: the user needs to select a drawing tool, line color and line width (have these displayed in comboboxes). when they click draw they can draw using the selected items and with the drawing start with mouse down and end with mouse up. I am able to get a drawing in the picture box, but only if I hardocde the information. I cannot get the user selected items to work, and also cannot get the mouse-up and mouse-down to be the points of the drawing. One of the tools has to be frehand drawing - would I use drawsgtring for this?
    Thanks for any assistance you may be able to provide.

    Comment

    Working...