Sunday, December 13, 2009

VB.Net Draw String Center of Point location

Private Sub DrawStringCenterOfPoint(ByRef gr As Graphics, _
ByVal txt As String, ByVal txtfont As Font, _
ByVal txtcolor As Brush, ByVal location As PointF)

' Mark the center for debugging
Dim x as Single = location.X
Dim y as Single = location.Y
gr.DrawLine(New Pen(Color.Red, 2), x - 10, y, x + 10, y)

' Make a StringFormat object that centers
Dim sf As New StringFormat
sf.LineAlignment = StringAlignment.Center
sf.Alignment = StringAlignment.Center

' Draw the text.
Dim StringSize As New SizeF
StringSize = gr.MeasureString(txt, txtfont)

gr.DrawString(txt, txtfont, txtcolor, x, y, sf)
sf.Dispose()

End Sub

VB.Net Find Center of a Polygon

Private Function CenterOfPolygon(ByRef _pointf() As PointF) As PointF
CenterOfPolygon = New Point(0, 0)

Dim total_points As Integer = _pointf.Length
Dim x_total As Single = 0, y_total As Single = 0
For Each _point As PointF In _pointf
x_total = x_total + _point.X
y_total = y_total + _point.Y
Next

CenterOfPolygon.X = x_total / total_points
CenterOfPolygon.Y = y_total / total_points

End Function