Welcome to Dream.In.Code
Become a VB Expert!

Join 137,261 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,540 people online right now. Registration is fast and FREE... Join Now!




drawing in picture box using selected tools, colors, etc

 
Reply to this topicStart new topic

drawing in picture box using selected tools, colors, etc

lab3tech
2 Dec, 2006 - 03:33 AM
Post #1

New D.I.C Head
*

Joined: 28 Oct, 2006
Posts: 46


My Contributions
I am working on my last VB assignment for this class, and have some questions for the forum. The user needs to select the drawing tool, color and width and then draw in the picute box using the mouse-up and mouse-down coordinates as beginning and ending positions. They need to be able to clear the drawing area (works OK) and see the selected items. I have the options in comboboxers so they see the selected one. I have all the code in the btndraw event but have added co-ordinates in the mouse-up and mouse-down events. I can get a line or rectangle to draw in the picture box if I specify the tool etc in the code but cannot get the user defined options to work. I also have no idea how to code the freehand tool. My code follows; any assistance would be greatly appreciated.
CODE

mports 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

    End Sub

#End Region

    Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) 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.EventArgs) Handles BtnClear.Click
        Picbox.Image = Nothing  'clears picture box


    End Sub

    Private Sub Picbox_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Picbox.MouseDown
        '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.MouseEventArgs) 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(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btndraw.Click

        Dim mygraphics As Graphics
        mygraphics = Picbox.CreateGraphics
        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.Equals(black) Then
            mycolor = black
        ElseIf CmbColor.Text.Equals(red) Then
            mycolor = red
        ElseIf CmbColor.Text.Equals(blue) Then
            mycolor = blue
        ElseIf CmbColor.Text.Equals(green) Then
            mycolor = green
        ElseIf CmbColor.Text.Equals(orange) Then
            mycolor = orange
        End If

        ' gets the user selected width
        If CInt(CmbWidth.Text) = 1 Then
            mywidth = 1
        ElseIf CInt(CmbWidth.Text) = 2 Then
            mywidth = 2
        ElseIf CInt(CmbWidth.Text) = 3 Then
            mywidth = 3
        ElseIf CInt(CmbWidth.Text) = 4 Then
            mywidth = 4
        ElseIf CInt(CmbWidth.Text) = 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(Color:=mycolor, Width:=mywidth)


        'mygraphics.DrawLine(Pen:=myPen, x1:=5, y1:=5, x2:=30, y2:=40)


        ' myrectangle = New Rectangle(x:=5, y:=5, Width:=50, Height:=60)
        ' mygraphics.DrawRectangle(Pen:=myPen, rect:=myrectangle)
        
    End Sub
End Class

User is offlineProfile CardPM
+Quote Post

y0da
RE: Drawing In Picture Box Using Selected Tools, Colors, Etc
4 Dec, 2006 - 09:52 AM
Post #2

D.I.C Head
Group Icon

Joined: 8 Nov, 2006
Posts: 62


Dream Kudos: 75
My Contributions
Use [code] tags or you won't get any help. Fix your post with some [code] tags and maybe you will find what your looking for....
User is offlineProfile CardPM
+Quote Post

the_hangman
RE: Drawing In Picture Box Using Selected Tools, Colors, Etc
4 Dec, 2006 - 11:19 AM
Post #3

D.I.C Addict
Group Icon

Joined: 18 Jan, 2006
Posts: 593



Thanked: 1 times
Dream Kudos: 200
My Contributions
I'm not exactly sure what you are asking.

some quick things to reduce the amount of code though for you: instead of those long If...ElseIf statements try this
CODE
mycolor = CmbColor.Text
mywidth = CInt(CmbWidth.Text)


saves a lot of code.

then you can use an If...ElseIf statement to draw
CODE
If mytool = line
   'Draw line'
ElseIf mytool = rectangle
   'Draw Rectangle
End If

User is offlineProfile CardPM
+Quote Post

lab3tech
RE: Drawing In Picture Box Using Selected Tools, Colors, Etc
4 Dec, 2006 - 12:02 PM
Post #4

New D.I.C Head
*

Joined: 28 Oct, 2006
Posts: 46


My Contributions
My apolgies for omitting the code tags. Am having to work with an old dial-up system until my high speed internet access is repaired and was rushing to get the message sent before I lost my connection. I know the code is long, but I get it to work and then cut out all the un-necessary items and streamline it then. Is easier for me to do after it is working - am still very new at this.

I need to drawe lines, rectangles and free-hand in a picture box, using the tool, color and width selected by the user. The user draws beginning with mouse down and finishes with mouse up. My big problem is getting the x and y coordinates of the mouse when it is in the picture box. I cna do this on a form using x1,y1,x2,y2 but doens't seem to work in the picturebox. I can get a line drawn, but it is not where the mouse is. I am also having problems getting the selected width in the code. Where it is now is not right, and results in changes to the size of the rectangle not the line.

Again, my apologies for omitting the code tags. It was not intentional.

CODE

rivate Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) 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.EventArgs) Handles BtnClear.Click
        Picbox.Image = Nothing 'clears picture box

    End Sub

    Private Sub Picbox_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Picbox.MouseDown
        'gets the co-ordinates of when the mouse button is pressed
        x1 = Location.X
        y1 = Location.Y
      
        If drawtool = "Line" Then

            drawgraphics.SmoothingMode = Drawing2D.SmoothingMode.None
            drawgraphics.DrawLine(drawpen, x2, y2, CInt(CmbWidth.SelectedItem), CInt(CmbWidth.SelectedItem))
            Picbox.Image = drawbitmap
        ElseIf drawtool = "Rectangle" Then
            'Width = CmbWidth.Text
            drawgraphics.SmoothingMode = Drawing2D.SmoothingMode.None
            drawgraphics.DrawRectangle(drawpen, e.X, e.Y, CInt(CmbWidth.SelectedItem), CInt(CmbWidth.SelectedItem))
            Picbox.Image = drawbitmap

        ElseIf drawtool = "Freehand" Then
            ' Width = CmbWidth.Text
            drawgraphics.SmoothingMode = Drawing2D.SmoothingMode.None
            drawgraphics.DrawLine(drawpen, e.X, e.Y, CInt(CmbWidth.SelectedItem), CInt(CmbWidth.SelectedItem))
            Picbox.Image = drawbitmap
        End If

    End Sub



    Private Sub Btndraw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btndraw.Click
        Select Case CmbTool.SelectedIndex
            Case 0
                drawtool = "Line"
            Case 1
                drawtool = "Rectangle"
            Case 2
                drawtool = "Freehand"
            Case Else
                drawtool = "Line"
        End Select

        Select Case CmbColor.SelectedIndex
            Case 0
                drawpen = Pens.Black
            Case 1
                drawpen = Pens.Red
            Case 2
                drawpen = Pens.Green
            Case 3
                drawpen = Pens.Blue
            Case 4
                drawpen = Pens.Orange
            Case Else
                drawpen = Pens.Black
        End Select


        Select Case CmbWidth.SelectedIndex
            Case 0
                mysize = 1
            Case 1
                mysize = 2
            Case 2
                mysize = 3
            Case 3
                mysize = 4
            Case 4
                mysize = 5
            Case Else
                mysize = 1
        End Select
    End Sub



    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        drawbitmap = New Bitmap(Picbox.Width, Picbox.Height)
        drawgraphics = Graphics.FromImage(drawbitmap)
        Picbox.Image = drawbitmap

        With CmbColor
            .Items.Add("Black")
            .Items.Add("Red")
            .Items.Add("Green")
            .Items.Add("Blue")
            .Items.Add("Orange")
        End With
        CmbColor.SelectedIndex = 0

        With CmbWidth
            .Items.Add(1)
            .Items.Add(2)
            .Items.Add(3)
            .Items.Add(4)
            .Items.Add(5)
        End With
        CmbWidth.SelectedIndex = 0


        With CmbTool
            .Items.Add("Line")
            .Items.Add("Rectangle")
            .Items.Add("Freehand")
        End With
        CmbTool.SelectedIndex = 0

    End Sub

    Private Sub Picbox_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Picbox.MouseUp
        x2 = Location.X
        y2 = Location.Y
        If drawtool = "Line" Then

            drawgraphics.SmoothingMode = Drawing2D.SmoothingMode.None
            drawgraphics.DrawLine(drawpen, x2, y2, CInt(CmbWidth.SelectedItem), CInt(CmbWidth.SelectedItem))
            Picbox.Image = drawbitmap
        ElseIf drawtool = "Rectangle" Then
            'Width = CmbWidth.Text
            drawgraphics.SmoothingMode = Drawing2D.SmoothingMode.None
            drawgraphics.DrawRectangle(drawpen, e.X, e.Y, CInt(CmbWidth.SelectedItem), CInt(CmbWidth.SelectedItem))
            Picbox.Image = drawbitmap

        ElseIf drawtool = "Freehand" Then
            ' Width = CmbWidth.Text
            drawgraphics.SmoothingMode = Drawing2D.SmoothingMode.None
            drawgraphics.DrawLine(drawpen, e.X, e.Y, CInt(CmbWidth.SelectedItem), CInt(CmbWidth.SelectedItem))
            Picbox.Image = drawbitmap
        End If

    End Sub

End Class

User is offlineProfile CardPM
+Quote Post

the_hangman
RE: Drawing In Picture Box Using Selected Tools, Colors, Etc
4 Dec, 2006 - 12:17 PM
Post #5

D.I.C Addict
Group Icon

Joined: 18 Jan, 2006
Posts: 593



Thanked: 1 times
Dream Kudos: 200
My Contributions
Ah! I see what you need.

The positions you are trying use are relative positions. For instance the mouse position is relative to the screen. So is the position of your form.

But the objects inside your form are relative to the left and top of the form.

So if your form is 100 pixels from the top of the screen and your picture box is 100 pixels from the top of the form, then your picture box is actually 200 pixels from the top of the screen.

try this x1=MousePosition.X - Me.Left - PictureBox1.Left and so on
that will give you the actual mouse position as is relative to the picture box
User is offlineProfile CardPM
+Quote Post

lab3tech
RE: Drawing In Picture Box Using Selected Tools, Colors, Etc
4 Dec, 2006 - 12:57 PM
Post #6

New D.I.C Head
*

Joined: 28 Oct, 2006
Posts: 46


My Contributions
Thank you! Was going crazy working on this - will try your suggestions and let you know how I do.

Thanks again
User is offlineProfile CardPM
+Quote Post

lab3tech
RE: Drawing In Picture Box Using Selected Tools, Colors, Etc
4 Dec, 2006 - 02:09 PM
Post #7

New D.I.C Head
*

Joined: 28 Oct, 2006
Posts: 46


My Contributions
QUOTE(the_hangman @ 4 Dec, 2006 - 01:17 PM) *

Ah! I see what you need.

The positions you are trying use are relative positions. For instance the mouse position is relative to the screen. So is the position of your form.

But the objects inside your form are relative to the left and top of the form.

So if your form is 100 pixels from the top of the screen and your picture box is 100 pixels from the top of the form, then your picture box is actually 200 pixels from the top of the screen.

try this x1=MousePosition.X - Me.Left - PictureBox1.Left and so on
that will give you the actual mouse position as is relative to the picture box



QUOTE(the_hangman @ 4 Dec, 2006 - 01:17 PM) *

Ah! I see what you need.

The positions you are trying use are relative positions. For instance the mouse position is relative to the screen. So is the position of your form.

But the objects inside your form are relative to the left and top of the form.

So if your form is 100 pixels from the top of the screen and your picture box is 100 pixels from the top of the form, then your picture box is actually 200 pixels from the top of the screen.

try this x1=MousePosition.X - Me.Left - PictureBox1.Left and so on
that will give you the actual mouse position as is relative to the picture box



The suggested code works great!! Thank you so much!!

I have a few other things to do, such as get the user selected width, make the line visible as it is being drawn not only after the mouse is released, and work on the freehand tool but those will be much easier now that I can draw the lines. As someone new to programming I have to take things step by step - get one working and then on to another. After all is working, I will use your suggestions for streamlining my code.

Again, thanks for all your help.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/4/08 06:55PM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month