12,243,058 members (46,725 online)
alternative version

97.2K views
80 bookmarked
Posted

# Text on Path with VB.NET

, 2 May 2006
 Rate this:
A VB.NET class for drawing text on a path.

## Introduction

I searched the internet for a long time for some algorithm to display a "Text on a Path", but I only fond the Batik SVG Toolkit. It's a Java project for displaying SVG. SVG has the ability to display "Text on a Path", but I only needed the Text on Path functionality.

So, I started recollecting my old math education, and coded the first lines to calculate the angle of defined position on a path.

Do you remember a²+b²=c²? That is the base to solve this problem. You need two points on a straight line.

```Private Function GetAngle(ByVal _point1 As PointF, _
ByVal _point2 As PointF) As Decimal
Dim c As Decimal
c = Math.Sqrt((_point2.X - _point1.X) ^ 2 + _
(_point2.Y - _point1.Y) ^ 2)
'Oh yeah good old math a²+b²=c²

If c = 0 Then
Return 0
End If 'We must change the side where the triangle is
If _point1.X > _point2.X Then
Return Math.Asin((_point1.Y - _point2.Y) / c) * _
180 / Math.PI - 180
Else
Return Math.Asin((_point2.Y - _point1.Y) / c) * _
180 / Math.PI
End If

End Function```

## Using the code

You can use the `TextOnPath` class to get a bitmap with the text on the given path.

The path must be set as `PathData`. You can also the the `TextPathPosition` (`Under`, `Center`, `Above`) and the align the text (`Left`, `Center`, `Right`).

```Dim _points(5) As PointF
Dim _top As TextOnPath.TextOnPath = New TextOnPath.TextOnPath
Dim _gp As GraphicsPath = New GraphicsPath

_points(0) = New PointF(81, 183)
_points(1) = New PointF(321, 305)
_points(2) = New PointF(333, 622)
_points(3) = New PointF(854, 632)
_points(4) = New PointF(864, 153)
_points(5) = New PointF(562, 224)

_top.PathDataTOP = _gp.PathData
_top.FontTOP = New Font("Microsoft Sans Serif", _
36, FontStyle.Regular)
_top.FillColorTOP = Color.Black
_top.ColorTOP = Color.White
_top.TextTOP = "Text on path goes around a path"
_top.ShowPath = True
_top.PathAlignTOP = TextOnPath.PathAlign.Center
_top.LetterSpacePercentage = 100
_top.TextPathPosition = TextOnPath.TextPosition.CenterPath

Dim oBitmap as Bitmap
oBitmap = _top.TextOnPathBitmap```

You can also get the new path data as an SVG string, so you can use it in HTML with Adobe SVG Viewer, or in Corel Draw or Adobe Illustrator.

```Dim _svg As String
_svg = _top.GetSVG(1024, 768)```

## Points of interest

If someone has ideas to optimize this code, please contact me, and sorry for the bad English and the short description. This is my first article, and I'm still learning.

## History

• Version 1.0 released.
• Version 1.01 released (`PathPoint` calculation optimized).

A list of licenses authors might use can be found here

## Share

 Software Developer Germany
No Biography provided

## You may also be interested in...

 First Prev Next
 Quick look does not reveal how to modify 'starting postion' of first char.... boblogan3-Aug-12 4:33 boblogan 3-Aug-12 4:33
 draw text within any path? ASPUIPAB29-Oct-11 12:39 ASPUIPAB 29-Oct-11 12:39
 C# version and with optimization. guidebee9-Jul-09 18:15 guidebee 9-Jul-09 18:15
 Great job sf4all15-Jul-09 5:45 sf4all 15-Jul-09 5:45
 Re: C# version and with optimization. Member 740216621-Apr-11 6:21 Member 7402166 21-Apr-11 6:21
 Re: C# version and with optimization. Marc Clifton14-Sep-14 6:25 Marc Clifton 14-Sep-14 6:25
 Interested in getting 1 developer license LarryKudrow17-Mar-08 6:49 LarryKudrow 17-Mar-08 6:49
 Problem with Running Project bnjneer5-Jan-08 15:30 bnjneer 5-Jan-08 15:30
 Problems mysticwars13-Apr-07 10:36 mysticwars 13-Apr-07 10:36
 Sqrt and Decimal Wesner Moise2-May-06 20:11 Wesner Moise 2-May-06 20:11
 Re: Sqrt and Decimal jarvisa29-May-06 23:55 jarvisa 29-May-06 23:55
 VGdotnet PSwartzell124-Apr-06 6:51 PSwartzell1 24-Apr-06 6:51
 VG dot net of www.vgdotnet.com has a very similar ability. It is also a great product for other vector graphics applications. And no, I do not work for them or get paid by them, I just like their product. Great article though! I never would have been able to produce this without either your code or vgdotnet.
 Excellent chimeric6923-Apr-06 17:53 chimeric69 23-Apr-06 17:53