7 Replies - 612 Views - Last Post: 30 January 2013 - 12:57 AM Rate Topic: -----

#1 eradia.de.ace  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-January 13

Draw a freeline

Posted 29 January 2013 - 06:44 PM

hello.. im using vb.net 2003..
ive searched everywhere, the code that provide draw a free line inside picturebox.. which is myself can click anywhere inside the picturebox as first line than extend the line as it reach scond point... ive only found a code that provide value of axis already on coding...the picturebox contain a picture..
Is This A Good Question/Topic? 0
  • +

Replies To: Draw a freeline

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3243
  • View blog
  • Posts: 10,882
  • Joined: 12-December 12

Re: Draw a freeline

Posted 29 January 2013 - 07:27 PM

I don't know how close VB.NET 2003 is to a more recent version but the code sample below demonstrates drawing a line when dragging:

Private _Previous As System.Nullable(Of Point) = Nothing
Private Sub pictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
  _Previous = e.Location
  pictureBox1_MouseMove(sender, e)
End Sub

Private Sub pictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
  If _Previous IsNot Nothing Then
    If PictureBox1.Image Is Nothing Then
      Dim bmp As New Bitmap(PictureBox1.Width, PictureBox1.Height)
      Using g As Graphics = Graphics.FromImage(bmp)
        g.Clear(Color.White)
      End Using
      PictureBox1.Image = bmp
    End If
    Using g As Graphics = Graphics.FromImage(PictureBox1.Image)
      g.DrawLine(Pens.Black, _Previous.Value, e.Location)
    End Using
    PictureBox1.Invalidate()
    _Previous = e.Location
  End If
End Sub

Private Sub pictureBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUp
  _Previous = Nothing
End Sub

You'll need to modify it if you want it to happen on clicking two different points.
Was This Post Helpful? 0
  • +
  • -

#3 eradia.de.ace  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-January 13

Re: Draw a freeline

Posted 29 January 2013 - 11:05 PM

View Postandrewsw, on 29 January 2013 - 07:27 PM, said:

I don't know how close VB.NET 2003 is to a more recent version but the code sample below demonstrates drawing a line when dragging:

Private _Previous As System.Nullable(Of Point) = Nothing
Private Sub pictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
  _Previous = e.Location
  pictureBox1_MouseMove(sender, e)
End Sub

Private Sub pictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
  If _Previous IsNot Nothing Then
    If PictureBox1.Image Is Nothing Then
      Dim bmp As New Bitmap(PictureBox1.Width, PictureBox1.Height)
      Using g As Graphics = Graphics.FromImage(bmp)
        g.Clear(Color.White)
      End Using
      PictureBox1.Image = bmp
    End If
    Using g As Graphics = Graphics.FromImage(PictureBox1.Image)
      g.DrawLine(Pens.Black, _Previous.Value, e.Location)
    End Using
    PictureBox1.Invalidate()
    _Previous = e.Location
  End If
End Sub

Private Sub pictureBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUp
  _Previous = Nothing
End Sub

You'll need to modify it if you want it to happen on clicking two different points.


helo.. regarding you code.. i already try it..but it seem an error occur when i start debug.. something wrong in section if_Previous isnot nothing then.. error describe "is not requires operands that have reference types, but this operand has the value type 'system.nullable(of system.drawing.point)"
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3243
  • View blog
  • Posts: 10,882
  • Joined: 12-December 12

Re: Draw a freeline

Posted 29 January 2013 - 11:50 PM

It is not my code, I just found it for illustration. However, I have just copied and run it successfully in VB.NET 2012. So it looks like there is a difference in some syntax between VB 2003 and 2012.

Try splitting it to two separate words, Is Not, or:

If Not _Previous Is Nothing Then

Apparently, the IsNot operator was introduced in 2005, so I'm surprised that it accepts it at all. But I think believe the previous code-line is equivalent.
Was This Post Helpful? 0
  • +
  • -

#5 eradia.de.ace  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-January 13

Re: Draw a freeline

Posted 30 January 2013 - 12:36 AM

i tried to use it on vb.net 2005.. the error still the same, at the same line.. i start from begining..at vb.net 2005.. on empty form, i create picturebox and insert picture.. i didnt modified properties of it.. then go to coding.. copy the code you give to me.. test it and error pop up.. same error.. maybe im miss something or you forgot something to put at the code..
Was This Post Helpful? 0
  • +
  • -

#6 erik venn  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 14
  • Joined: 17-January 13

Re: Draw a freeline

Posted 30 January 2013 - 12:46 AM

above code runs perfect in vb2012... but there need so much modification i order to run that in vb2005 coz of changed syntax.
Was This Post Helpful? 0
  • +
  • -

#7 eradia.de.ace  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-January 13

Re: Draw a freeline

Posted 30 January 2013 - 12:55 AM

oh my.. i need more reference if the case like that.. to combine old and new one version.. anyway thanks guys..
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3243
  • View blog
  • Posts: 10,882
  • Joined: 12-December 12

Re: Draw a freeline

Posted 30 January 2013 - 12:57 AM

Quote

or you forgot something to put at the code..

He, he - thanks :whistling:.

I just copied the code. It works for me in 2012. I found this code (quite quickly) and offered it to you to illustrate how you might do this, suggesting that you would probably need to modify it for 2003.

I didn't insert an image into the picturebox, and don't know if this would make a difference(?).

Perhaps it is this line:
Private _Previous As System.Nullable(Of Point) = Nothing

which is not acceptable to 2003 (or 2005), but I would have thought it would indicate an error with an underline of some kind.

Anyway, good luck.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1