7 Replies - 3139 Views - Last Post: 09 July 2015 - 08:00 AM Rate Topic: -----

#1 little_prince41  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 163
  • Joined: 10-August 07

How to draw continuous line from array on picturebox

Posted 08 July 2015 - 10:58 PM

Hello
I want to draw continuous line from array on Picturebox,I don't know how to do that
somebody , Could you help me.
my array
x={ 0, -12, 3,15,-20 }
Y={ 0, 12, -3,15,-20 }

Thank you.

[code]
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim g2 As System.Drawing.Graphics
        Dim PicScale_X As Integer = Me.PictureBox1.Width / 2
        Dim PicScale_Y As Integer = Me.PictureBox1.Height / 2 - 200
        Dim pen1 As New System.Drawing.Pen(Color.Green, 1)
        Dim Opendialog1 As New OpenFileDialog
        If Opendialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim fiStream As New FileStream(Opendialog1.FileName, FileMode.Open, FileAccess.Read)
            Dim binReader As New BinaryReader(fiStream)
            Dim txt() As Byte = {}
            txt = binReader.ReadBytes(fiStream.Length)
            Dim count As Integer = 0
            Dim count2 As Integer = 1
            Dim Bits As String
            Dim X1 as Interger=0
            Dim Y1 as Interger=0
            Dim XOld as Interger=0
            Dim YOld as Interger=0
            For index As Integer = 72 To txt.Length - 1
                Dim g As Integer = txt(index)
                If g = 255 Then
                    g = 0
                End If
                Select Case count
                    Case 8
                        X1 = g
                    Case 9
                        Y1 = g
                    Case 10
                        g2 = PictureBox1.CreateGraphics
                        g2.Drawline(pen1, PicScale_X +XOld, PicScale_Y+ YOld, PicScale_X +X1,  PicScale_X +Y1)
                        XOld+=X1
                        YOld+=Y1
                End Select
              
            Next

        End If
[/code]

This post has been edited by little_prince41: 09 July 2015 - 04:58 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How to draw continuous line from array on picturebox

#2 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1490
  • View blog
  • Posts: 3,772
  • Joined: 01-February 13

Re: How to draw continuous line from array on picturebox

Posted 09 July 2015 - 03:51 AM

Are you trying to draw directly onto the PictureBox Graphics Object or are you trying to draw on an Image that is displayed in a PictureBox?

How are you coming up with the values in the Array? Normally you would want numbers that are from 0 to the width or height of the Image or PictureBox depending on which one you are trying to draw on.

If you use negative numbers such as -20 or -12 the lines would be off the left or top side and that part of the line would not be visible.

If possible you should change the way you are getting the X and Y numbers and store them in an Array or List of Point. It would make it much easier to draw a continuous line from point to point by using the Graphics.DrawLines Method (Pen, Point()).

This post has been edited by IronRazer: 09 July 2015 - 04:42 AM

Was This Post Helpful? 0
  • +
  • -

#3 little_prince41  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 163
  • Joined: 10-August 07

Re: How to draw continuous line from array on picturebox

Posted 09 July 2015 - 04:57 AM

I'm trying to directly onto the PictureBox Graphics Object.
Was This Post Helpful? 0
  • +
  • -

#4 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1490
  • View blog
  • Posts: 3,772
  • Joined: 01-February 13

Re: How to draw continuous line from array on picturebox

Posted 09 July 2015 - 05:07 AM

Below is an example of how you can add your X and Y values to an Array of Points and use them to draw lines between each Point. You would use the PictureBox.Paint event to draw the lines using the Graphics.DrawLines method.

However, as i said, you will see that the lines extend off the left and top sides of the PictureBox because of the negative numbers in the array. I don`t know if you want that or not.
Public Class Form1

    'These are your X and Y values
    Private X() As Integer = {0, -12, 3, 15, -20}
    Private Y() As Integer = {0, 12, -3, 15, -20}

    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint

        'create an array of (Point) and fill it with Points of your X and Y values
        Dim pnts(X.Length - 1) As Point

        For i As Integer = 0 To X.Length - 1
            pnts(i) = New Point(X(i), Y(i))
        Next

        'Draw lines between each point in the Array of Points
        e.Graphics.DrawLines(Pens.Red, pnts)
    End Sub
End Class


This post has been edited by IronRazer: 09 July 2015 - 05:07 AM

Was This Post Helpful? 0
  • +
  • -

#5 little_prince41  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 163
  • Joined: 10-August 07

Re: How to draw continuous line from array on picturebox

Posted 09 July 2015 - 06:00 AM

Quote

However, as i said, you will see that the lines extend off the left and top sides of the PictureBox because of the negative numbers in the array. I don`t know if you want that or not.

ํRight, I need to offset scale and zoom ,Could you help me again
Thank you for help.
Was This Post Helpful? 0
  • +
  • -

#6 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1490
  • View blog
  • Posts: 3,772
  • Joined: 01-February 13

Re: How to draw continuous line from array on picturebox

Posted 09 July 2015 - 06:49 AM

I am not sure what you are drawing or how you want it drawn so, it is hard for me to tell if this is what you want. You can use the Graphics.TranslateTransform Method to offset the drawing in the PictureBox. You can also use the Graphics.ScaleTransform Method to scale the drawing to make it larger or smaller.

Here is an example that uses TranslateTransform to move the drawing to the center of the picturebox and the ScaleTransform method to scale the drawing to 4 times its original size.
Public Class Form1

    'These are your X and Y values
    Private X() As Integer = {0, -12, 3, 15, -20}
    Private Y() As Integer = {0, 12, -3, 15, -20}

    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint

        'create an array of (Point) and fill it with Points of your X and Y values
        Dim pnts(X.Length - 1) As Point

        For i As Integer = 0 To X.Length - 1
            pnts(i) = New Point(X(i), Y(i))
        Next

        'Offset the drawing by half the picturebox`s width and height (draws it starting at center of picturebox)
        'you can offset it to wherever you want.
        Dim Xoffset As Single = CSng(PictureBox1.ClientSize.Width / 2)
        Dim Yoffset As Single = CSng(PictureBox1.ClientSize.Height / 2)
        e.Graphics.TranslateTransform(Xoffset, Yoffset)

        'scales the drawing to (width * 4) and (height * 4) (draws the lines at 4 times their actual size)
        'you can scale it to whatever you want.
        e.Graphics.ScaleTransform(4, 4)

        'Draw lines between each point in the Array of Points
        e.Graphics.DrawLines(Pens.Red, pnts)

        'resets the Transform back to normal after drawing the lines
        e.Graphics.ResetTransform()
    End Sub
End Class


Was This Post Helpful? 1
  • +
  • -

#7 little_prince41  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 163
  • Joined: 10-August 07

Re: How to draw continuous line from array on picturebox

Posted 09 July 2015 - 07:08 AM

Thank you so much for help me. It's work.
Was This Post Helpful? 0
  • +
  • -

#8 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1490
  • View blog
  • Posts: 3,772
  • Joined: 01-February 13

Re: How to draw continuous line from array on picturebox

Posted 09 July 2015 - 08:00 AM

You`re Welcome. 8)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1