Nate879

I want to draw a line, but I want to draw it a little at a time. Just like the DrawLine finction, but one dot at a time. How do I do this



Re: Visual Basic Express Edition Drawing a line one pixel at a time

Jung Yi

Hello,

You can use Bitmap object SetPixel function.

Sample code from MSDN:

Code Snippet
Public Sub SetPixel_Example(ByVal e As PaintEventArgs)
' Create a Bitmap object from a file.    
Dim myBitmap As New Bitmap("Grapes.jpg")    
' Draw myBitmap to the screen.    
e.Graphics.DrawImage(myBitmap, 0, 0, myBitmap.Width, myBitmap.Height)    
' Set each pixel in myBitmap to black.    
Dim Xcount As Integer    
For Xcount = 0 To myBitmap.Width - 1        
Dim Ycount As Integer        
For Ycount = 0 To myBitmap.Height - 1           
myBitmap.SetPixel(Xcount, Ycount, Color.Black)        
Next Ycount    
Next Xcount    
' Draw myBitmap to the screen again.    
e.Graphics.DrawImage(myBitmap, myBitmap.Width, 0, myBitmap.Width, myBitmap.Height)
End Sub






Re: Visual Basic Express Edition Drawing a line one pixel at a time

NateNate

Not helpful. Is there a way to do this without bitmaps





Re: Visual Basic Express Edition Drawing a line one pixel at a time

Matt_Fomich

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim t As New Threading.Thread(AddressOf DrawLineSlowly)

t.Start()

' This method will slowly draw a line, but the line will not be permanent.

' Use Bitblt, or similar method to save the line, or declare a line, and override

' its paint event, to make the line draw itself slowly.

' This was coded in Visual Basic.net v. 2003, but it should work fine in V. 2005

End Sub

Sub DrawLineSlowly()

Dim g As Graphics = Me.CreateGraphics

For i As Integer = 0 To 300

Threading.Thread.Sleep(100)

g.DrawLine(Pens.Black, 20 + i, 20, i, 20)

Next

End Sub





Re: Visual Basic Express Edition Drawing a line one pixel at a time

Spidermans_DarkSide - VSIP

Hi,

Do you want to draw a line on the FORM or on a PictureBox

Regards,

S_DS






Re: Visual Basic Express Edition Drawing a line one pixel at a time

NateNate

I want to draw on a panel on The form. I NEVER use pictureboxes. They are too slow and most of my applications would require 441 or more pictureboxes to work. I draw on a panel.





Re: Visual Basic Express Edition Drawing a line one pixel at a time

iloveny

NateNate wrote:
Not helpful. Is there a way to do this without bitmaps


Even though it is not helpful, at least you have to appreciate the guy who posted it. There was not even a word called "THANKS" in your reply.

And, by just saying "Not helpful" doesn't give any clue to anybody. You have to help us to help you by telling us what doesn't work, what did you try, or how you think it will be done, and so on and so forth.

I am not being offensive to you or your post, but please learn to be a little appreciate to other members that try to help you out.





Re: Visual Basic Express Edition Drawing a line one pixel at a time

SJWhiteley

Does the post by Matt_Fomich help Essentially you draw a line from one point to the next. You will have to calculate where the points are. This should be straight forward with AntiAliasing off; I'm not sure what the results would be if AA was on. It may also depend on the style of the pen that you use, but a one pixel wide basic pen may work ok.

Unfortunately, there is no 'setpixel' method of the graphics object. You can either use the DrawLine, or perhaps draw a 1 pixel dameter circle (DrawEllipse method). Anti Aliasing I'm sure will affect that.

You could still use the bitmap method: 1 panel = 1 bitmap. Draw to the bitmap, then render the bitmap to the panel: this is what Jung Yi proposes, why might it not work .






Re: Visual Basic Express Edition Drawing a line one pixel at a time

Spidermans_DarkSide - VSIP

Hi,

I've managed to do this for horizontal and vertical lines only ( if you want the code )

There seems to be a problem drawing certain diagonal lines with the methods i'm using.

So far you can specify the start and end points and it will draw in that direction i.e;

  • Top to Bottom
  • Bottom to Top
  • Left To Right
  • Right To Left

My code takes the 1st point as the Start point so 10,10 to 100,10 goes Left To Right automatically.

Let me know if you want the code still.

Regards,

S_DS






Re: Visual Basic Express Edition Drawing a line one pixel at a time

NateNate

Yes, I want the code.





Re: Visual Basic Express Edition Drawing a line one pixel at a time

Chris Dunaway

Perhaps the articles at www.bobpowell.net will be helpful for you.

Chris




Re: Visual Basic Express Edition Drawing a line one pixel at a time

Spidermans_DarkSide - VSIP

Hi,

This code uses 1 button as Button1 ,

1 Panel as Panel1 ( can be any panel name though with my code ),

1 Timer as Timer1.

Code "so far" as promised.>>

This only works on a Panel as requested and only does slow vertical or horizontal lines so far.

If i get diagonals working i'll post the code.>>

Regards,

S_DS

____________________________________________________________

#Region "FORM CODE."

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'Make the interval smaller

'for a faster line draw.

Timer1.Interval = 25

End Sub

Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick

'How to use,

'10,10 is the start point.

'10,250 is the end point.

'As anyPen is a PUBLIC variable

'it is set to Color.Blue and Width = 10

'Same goes for the anyPanel variable

'it is set to Panel1

Call SlowLine(10, 10, 10, 250, anyPen, anyWidth, anyPanel)

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'Set PEN colour.

anyPen.Color = Color.Blue

'Set which PANEL to draw on.

anyPanel = Panel1

'Set the PEN line WIDTH.

anyWidth = 10

'Start the timer.

'This adds a dash to the line every 25 timer ticks.

Timer1.Start()

'Stop the timer when the line is done.

If Done = True Then Timer1.Stop()

End Sub

#Region "DrawingRoutines."

'Module Drawing_Subs

#Region "Public Sub SlowLine."

Public Sub SlowLine(ByVal myX1 As Integer, ByVal _

myY1 As Integer, ByVal myX2 As Integer, ByVal _

myY2 As Integer, ByVal myPen As Pen, ByVal _

myWidth As Integer, ByVal myPanel As Panel)

'Line is Vertical.

If myX1 = myX2 Then

Call Vertical(myX1, myY1, myX2, myY2, myPen, myWidth, myPanel)

End If

'Line is Horizontal.

If myY1 = myY2 Then

Call Horizontal(myX1, myY1, myX2, myY2, myPen, myWidth, myPanel)

End If

End Sub

#End Region

#Region "Vertical "

Public Sub Vertical(ByVal myX1 As Integer, ByVal _

myY1 As Integer, ByVal myX2 As Integer, ByVal _

myY2 As Integer, ByVal myPen As Pen, ByVal _

myWidth As Integer, ByVal myPanel As Panel)

'Line is Top to Bottom.

'myX1 = myX2 so myX2 is not needed.

If myY1 < myY2 Then

Call T2B(myX1, myY1, myY2, myPen, myWidth, myPanel)

End If

'Line is Bottom to Top.

'myX1 = myX2 so myX2 is not needed.

If myY1 > myY2 Then

Call B2T(myX1, myY1, myY2, myPen, myWidth, myPanel)

End If

End Sub

#Region "Top to bottom."

'Line is TOP to BOTTOM.

Private Sub T2B(ByVal myX1 As Integer, ByVal _

myY1 As Integer, ByVal myY2 As Integer, _

ByVal myPen As Pen, ByVal myWidth As Integer, _

ByVal myPanel As Panel)

Static x1 = myX1

Static x2 = myX1

Static y1 = myY1

Static y2 = myY1 + 1

If y1 >= myY2 Then

Done = True

Exit Sub

End If

With myPen

.Width = myWidth

End With

myPanel.CreateGraphics.DrawLine(myPen, x1, y1, x2, y2)

'Following line used to test the code.>>

'rt.AppendText(x1.ToString & " " & y1.ToString & " " & x2.ToString & " " & y2.ToString & vbCrLf)

y1 += 1

y2 = y1 + 1

End Sub

#End Region

#Region "Bottom to top."

'Line is BOTTOM to TOP.

Private Sub B2T(ByVal myX1 As Integer, ByVal _

myY1 As Integer, ByVal myY2 As Integer, _

ByVal myPen As Pen, ByVal myWidth As Integer, _

ByVal myPanel As Panel)

Static x1 = myX1

Static x2 = myX1

Static y1 = myY1

Static y2 = myY1 - 1

If y2 = myY2 - 1 Then

Done = True

Exit Sub

End If

With myPen

.Width = myWidth

End With

myPanel.CreateGraphics.DrawLine(myPen, x1, y1, x2, y2)

'Following line used to test the code.>>

'rt.AppendText(x1.ToString & " " & y1.ToString & " " & x2.ToString & " " & y2.ToString & vbCrLf)

y1 -= 1

y2 = y1 - 1

End Sub

#End Region

#End Region

#Region "Horizontal "

Private Sub Horizontal(ByVal myX1 As Integer, ByVal _

myY1 As Integer, ByVal myX2 As Integer, ByVal _

myY2 As Integer, ByVal myPen As Pen, ByVal _

myWidth As Integer, ByVal myPanel As Panel)

'Line is LEFT to RIGHT.

'Y1 and Y2 are the same

'on a horizontal line.

If myX1 < myX2 Then

Call L2R(myX1, myY1, myX2, myPen, myWidth, myPanel)

End If

'Line is RIGHT to LEFT.

'Y1 and Y2 are the same

'on a horizontal line.

If myX1 > myX2 Then

Call R2L(myX1, myY1, myX2, myPen, myWidth, myPanel)

End If

End Sub

#Region "Left to right."

'Line is LEFT to RIGHT.

Private Sub L2R(ByVal myX1 As Integer, ByVal _

myY1 As Integer, ByVal myX2 As Integer, _

ByVal myPen As Pen, ByVal myWidth As Integer, _

ByVal myPanel As Panel)

Static x1 = myX1

Static x2 = myX1 + 1

Static y1 = myY1

Static y2 = myY1

If x1 >= myX2 Then

Done = True

Exit Sub

End If

With myPen

.Width = myWidth

End With

myPanel.CreateGraphics.DrawLine(myPen, x1, y1, x2, y2)

'Following line used to test the code.>>

'rt.AppendText(x1.ToString & " " & y1.ToString & " " & x2.ToString & " " & y2.ToString & vbCrLf)

x1 += 1

x2 = x1 + 1

End Sub

#End Region

#Region "Right to left."

'Line is RIGHT to LEFT.

Private Sub R2L(ByVal myX1 As Integer, ByVal _

myY1 As Integer, ByVal myX2 As Integer, _

ByVal myPen As Pen, ByVal myWidth As Integer, _

ByVal myPanel As Panel)

Static x1 = myX1

Static x2 = myX1 - 1

Static y1 = myY1

Static y2 = myY1

If x1 <= myX2 Then

Done = True

Exit Sub

End If

With myPen

.Width = myWidth

End With

myPanel.CreateGraphics.DrawLine(myPen, x1, y1, x2, y2)

'Following line used to test the code.>>

'rt.AppendText(x1.ToString & " " & y1.ToString & " " & x2.ToString & " " & y2.ToString & vbCrLf)

x1 -= 1

x2 = x1 - 1

End Sub

#End Region

#End Region

'End Module

#End Region

End Class

#End Region

#Region "myVariables."

Module myVariables

'Start coordinates.

Public X1, Y1 As Integer

'End coordinates.

Public X2, Y2 As Integer

'Declare a panel.

Public anyPanel As Panel

'Declare a pen.

Public anyPen As New Pen(Color.Blue)

'Create a width for the Pen.

Public anyWidth As Integer

'Create an INDEX for any LOOPs

Public Index As Integer

'Create a variable to check if

'the line is finished drawing.

Public Done As Boolean

End Module

#End Region






Re: Visual Basic Express Edition Drawing a line one pixel at a time

ISAWHIM

Code Snippet

Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click

MyLine(Panel1, 0, 50, 200, 50)

End Sub

Private Sub MyLine(ByRef DrawOn As Panel, ByVal X1 As Single, ByVal Y1 As Single, ByVal X2 As Single, ByVal Y2 As Single)

Dim pCount As Single

For pCount = X1 To X2 Step 1

DrawOn.CreateGraphics.DrawLine(Pens.Blue, X1, Y1, pCount, Y2)

System.Threading.Thread.Sleep(10)

Application.DoEvents()

Next

End Sub

This will allow you to draw on any object that can be drawn on...

For example, this draws on a panel, when you click on your form...

The ghetto DoEvents and SLEEP is only to show the pixel loading... Or it draws so fast, you don't see it!

You can play with the code to draw diagonal, up/down, reverse, etc...






Re: Visual Basic Express Edition Drawing a line one pixel at a time

Spidermans_DarkSide - VSIP

ISAWHIM wrote:

Code Snippet

Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click

MyLine(Panel1, 0, 50, 200, 50)

End Sub

Private Sub MyLine(ByRef DrawOn As Panel, ByVal X1 As Single, ByVal Y1 As Single, ByVal X2 As Single, ByVal Y2 As Single)

Dim pCount As Single

For pCount = X1 To X2 Step 1

DrawOn.CreateGraphics.DrawLine(Pens.Blue, X1, Y1, pCount, Y2)

System.Threading.Thread.Sleep(10)

Application.DoEvents()

Next

End Sub

This will allow you to draw on any object that can be drawn on...

For example, this draws on a panel, when you click on your form...

The ghetto DoEvents and SLEEP is only to show the pixel loading... Or it draws so fast, you don't see it!

You can play with the code to draw diagonal, up/down, reverse, etc...

Hi,

The above allows drawing on any PANEL not on ANY OBJECT.

Try passing ByRef a PictureBox to your "MyLine" Sub then.

Regards,

S_DS






Re: Visual Basic Express Edition Drawing a line one pixel at a time

ISAWHIM

Code Snippet

Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click

MyLine(Panel1, 0, 50, 200, 50)

End Sub

Private Sub MyLine(ByRef DrawOn As Panel, ByVal X1 As Single, ByVal Y1 As Single, ByVal X2 As Single, ByVal Y2 As Single)

Dim pCount As Single

For pCount = X1 To X2 Step 1

DrawOn.CreateGraphics.DrawLine(Pens.Blue, X1, Y1, pCount, Y2)

System.Threading.Thread.Sleep(10)

Application.DoEvents()

Next

End

ByRef DrawOn As Panel

ByRef DrawOn As Form

ByRef DrawOn As PictureBox

ByRef DrawOn As ... any OBJECT that can be drawn on

He has stated that he is using a PANEL object as his drawing medium.

Attempting to use this with any other object, will instantly show an issue, and indicate what needs to be changed, before you run the program. I did not say that this code will allow you to draw on any object as is... I said "THIS WILL ALLOW YOU TO DRAW ON ANY OBJECT THAT CAN BE DRAWN ON.", and it does... if you tell it the object that you are going to be drawing on.

I could have made it complex, and used a STRING, and created a long list of SELECT CASE, with each possible drawing object, but I am not here to do other peoples homework. That has nothing to do with this posting. This posting is about DRAWING A LINE, ONE PIXEL AT A TIME, which happens to be on PANEL_X.