472,117 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,117 software developers and data experts.

convert streamreader string to system.drawing.point?



I have an application where I want to redraw a polygon from points I
retrieve from a file.
The file is read with streamreader line by line each line contains the
points for each polygon.
Below is a typical line from my file.
500,748 500,678 552,678 552,696 584,696 584,714 612,714 612,748

Each set of x,y coordinates is separated by a space.

How can I read this into my points collection. If I currently try I get
can't convert issues.

Here's code I have been hacking and slashing
Dim blueBrush As New SolidBrush(Color.Blue)
Dim curvePoints As Point()
Dim part As Integer
Dim part2 As Integer
Dim pts As Point

Dim formGraphics2 As System.Drawing.Graphics = Me.CreateGraphics()

Try
' Create an instance of StreamReader to read from a file.
Using sr As StreamReader = New StreamReader("Polygon.txt")
Dim line As String
' Read and display the lines from the file until the end
' of the file is reached.
Do
line = sr.ReadLine()
If line <"" Then
part =
Convert.ToInt32(Microsoft.VisualBasic.Left(line, 3))

part2 =
Convert.ToInt32(Microsoft.VisualBasic.Mid(line, 5, 3))
pts = (part,part2)
curvePoints = pts

End If

'formGraphics2.FillPolygon(blueBrush, curvePoints)
Loop Until line Is Nothing
sr.Close()
End Using
Catch E As Exception
' Let the user know what went wrong.
Console.WriteLine("The file could not be read:")
Console.WriteLine(E.Message)
End Try

please respond via email if possible

--
Dave Bootsma
Bradenton Fl.
(941) 224-8298
Sep 8 '08 #1
5 3420
why not use split for your line ?
"Dave Bootsma" <da**********@verizon.netwrote in message
news:uK**************@TK2MSFTNGP04.phx.gbl...
>

I have an application where I want to redraw a polygon from points I
retrieve from a file.
The file is read with streamreader line by line each line contains the
points for each polygon.
Below is a typical line from my file.
500,748 500,678 552,678 552,696 584,696 584,714 612,714 612,748

Each set of x,y coordinates is separated by a space.

How can I read this into my points collection. If I currently try I get
can't convert issues.

Here's code I have been hacking and slashing
Dim blueBrush As New SolidBrush(Color.Blue)
Dim curvePoints As Point()
Dim part As Integer
Dim part2 As Integer
Dim pts As Point

Dim formGraphics2 As System.Drawing.Graphics = Me.CreateGraphics()

Try
' Create an instance of StreamReader to read from a file.
Using sr As StreamReader = New StreamReader("Polygon.txt")
Dim line As String
' Read and display the lines from the file until the end
' of the file is reached.
Do
line = sr.ReadLine()
If line <"" Then
part =
Convert.ToInt32(Microsoft.VisualBasic.Left(line, 3))

part2 =
Convert.ToInt32(Microsoft.VisualBasic.Mid(line, 5, 3))
pts = (part,part2)
curvePoints = pts

End If

'formGraphics2.FillPolygon(blueBrush, curvePoints)
Loop Until line Is Nothing
sr.Close()
End Using
Catch E As Exception
' Let the user know what went wrong.
Console.WriteLine("The file could not be read:")
Console.WriteLine(E.Message)
End Try

please respond via email if possible

--
Dave Bootsma
Bradenton Fl.
(941) 224-8298
Sep 8 '08 #2
You can use the Split function a couple of times

Dim Rows(), Rows2() As String
Dim oRead As System.IO.StreamReader
Dim EntireFile As String
oRead = File.OpenText("c:\yourfile.txt")
EntireFile = oRead.ReadToEnd()
Rows = EntireFile.Split(CChar(" "))

For i As Integer = 0 to Rows.Length - 1
Rows2 = Rows(i).Split(CChar(",")
For j As Integer = 0 to Rows2.Length - 1
'--do your polygon thing
Next
Next
Rich

*** Sent via Developersdex http://www.developersdex.com ***
Sep 8 '08 #3
I've tried pts= .split(line," ")
but then I get type cannot be converted to 1-dimensional array
system.drawing.point

"Gillard" <gillard_georges@@@@@@@@@hotmail.comwrote in message
news:#q*************@TK2MSFTNGP06.phx.gbl...
why not use split for your line ?
"Dave Bootsma" <da**********@verizon.netwrote in message
news:uK**************@TK2MSFTNGP04.phx.gbl...
>>

I have an application where I want to redraw a polygon from points I
retrieve from a file.
The file is read with streamreader line by line each line contains the
points for each polygon.
Below is a typical line from my file.
500,748 500,678 552,678 552,696 584,696 584,714 612,714 612,748

Each set of x,y coordinates is separated by a space.

How can I read this into my points collection. If I currently try I get
can't convert issues.

Here's code I have been hacking and slashing
Dim blueBrush As New SolidBrush(Color.Blue)
Dim curvePoints As Point()
Dim part As Integer
Dim part2 As Integer
Dim pts As Point

Dim formGraphics2 As System.Drawing.Graphics = Me.CreateGraphics()

Try
' Create an instance of StreamReader to read from a file.
Using sr As StreamReader = New StreamReader("Polygon.txt")
Dim line As String
' Read and display the lines from the file until the end
' of the file is reached.
Do
line = sr.ReadLine()
If line <"" Then
part =
Convert.ToInt32(Microsoft.VisualBasic.Left(line , 3))

part2 =
Convert.ToInt32(Microsoft.VisualBasic.Mid(line, 5, 3))
pts = (part,part2)
curvePoints = pts

End If

'formGraphics2.FillPolygon(blueBrush, curvePoints)
Loop Until line Is Nothing
sr.Close()
End Using
Catch E As Exception
' Let the user know what went wrong.
Console.WriteLine("The file could not be read:")
Console.WriteLine(E.Message)
End Try

please respond via email if possible

--
Dave Bootsma
Bradenton Fl.
(941) 224-8298

Sep 8 '08 #4
...
Dim k, m As Integer
For i As Integer = 0 to Rows.Length - 1
Rows2 = Rows(i).Split(CChar(",")
k = CInt(Rows2(0))
m = CInt(Rows2(1))
Pt = New Point(k, m)
Next
...

Rich

*** Sent via Developersdex http://www.developersdex.com ***
Sep 8 '08 #5
pts = line.Split(" "c)

or

pts = line.Split(New String() { " " }, StringSplitOptions.None)

You don't pass the string to Split(), you call Split() on the string
you with to split.

If you look at the IntelliSense you will see that String.Split takes
the separators as any of:
Char
Char array
String Array followed by StringSplitOptions

On Mon, 8 Sep 2008 17:52:14 -0400, "Dave Bootsma"
<da**********@verizon.netwrote:
>I've tried pts= .split(line," ")
but then I get type cannot be converted to 1-dimensional array
system.drawing.point

"Gillard" <gillard_georges@@@@@@@@@hotmail.comwrote in message
news:#q*************@TK2MSFTNGP06.phx.gbl...
>why not use split for your line ?
"Dave Bootsma" <da**********@verizon.netwrote in message
news:uK**************@TK2MSFTNGP04.phx.gbl...
>>>

I have an application where I want to redraw a polygon from points I
retrieve from a file.
The file is read with streamreader line by line each line contains the
points for each polygon.
Below is a typical line from my file.
500,748 500,678 552,678 552,696 584,696 584,714 612,714 612,748

Each set of x,y coordinates is separated by a space.

How can I read this into my points collection. If I currently try I get
can't convert issues.

Here's code I have been hacking and slashing
Dim blueBrush As New SolidBrush(Color.Blue)
Dim curvePoints As Point()
Dim part As Integer
Dim part2 As Integer
Dim pts As Point

Dim formGraphics2 As System.Drawing.Graphics = Me.CreateGraphics()

Try
' Create an instance of StreamReader to read from a file.
Using sr As StreamReader = New StreamReader("Polygon.txt")
Dim line As String
' Read and display the lines from the file until the end
' of the file is reached.
Do
line = sr.ReadLine()
If line <"" Then
part =
Convert.ToInt32(Microsoft.VisualBasic.Left(lin e, 3))

part2 =
Convert.ToInt32(Microsoft.VisualBasic.Mid(line , 5, 3))
pts = (part,part2)
curvePoints = pts

End If

'formGraphics2.FillPolygon(blueBrush, curvePoints)
Loop Until line Is Nothing
sr.Close()
End Using
Catch E As Exception
' Let the user know what went wrong.
Console.WriteLine("The file could not be read:")
Console.WriteLine(E.Message)
End Try

please respond via email if possible

--
Dave Bootsma
Bradenton Fl.
(941) 224-8298

Sep 8 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Toby Mathews | last post: by
4 posts views Thread by John | last post: by
21 posts views Thread by JoKur | last post: by
4 posts views Thread by Harry Hudini | last post: by
7 posts views Thread by Scott Schluer | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.