6 Replies - 29913 Views - Last Post: 04 December 2006 - 03:09 PM Rate Topic: -----

#1 lab3tech  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 46
  • Joined: 28-October 06

drawing in picture box using selected tools, colors, etc

Posted 02 December 2006 - 04:33 AM

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.
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



Is This A Good Question/Topic? 0
  • +

Replies To: drawing in picture box using selected tools, colors, etc

#2 y0da  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 08-November 06

Re: drawing in picture box using selected tools, colors, etc

Posted 04 December 2006 - 10:52 AM

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....
Was This Post Helpful? 0
  • +
  • -

#3 the_hangman  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 13
  • View blog
  • Posts: 602
  • Joined: 18-January 06

Re: drawing in picture box using selected tools, colors, etc

Posted 04 December 2006 - 12:19 PM

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
mycolor = CmbColor.Text
mywidth = CInt(CmbWidth.Text)


saves a lot of code.

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

Was This Post Helpful? 0
  • +
  • -

#4 lab3tech  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 46
  • Joined: 28-October 06

Re: drawing in picture box using selected tools, colors, etc

Posted 04 December 2006 - 01:02 PM

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.

 
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


Was This Post Helpful? 0
  • +
  • -

#5 the_hangman  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 13
  • View blog
  • Posts: 602
  • Joined: 18-January 06

Re: drawing in picture box using selected tools, colors, etc

Posted 04 December 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
Was This Post Helpful? 0
  • +
  • -

#6 lab3tech  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 46
  • Joined: 28-October 06

Re: drawing in picture box using selected tools, colors, etc

Posted 04 December 2006 - 01:57 PM

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

Thanks again
Was This Post Helpful? 0
  • +
  • -

#7 lab3tech  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 46
  • Joined: 28-October 06

Re: drawing in picture box using selected tools, colors, etc

Posted 04 December 2006 - 03:09 PM

View Postthe_hangman, on 4 Dec, 2006 - 01:17 PM, said:

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



View Postthe_hangman, on 4 Dec, 2006 - 01:17 PM, said:

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1