Click here to Skip to main content
15,867,749 members
Articles / Multimedia / GDI+
Article

Text on Path with VB.NET

Rate me:
Please Sign up or sign in to vote.
4.84/5 (44 votes)
2 May 20061 min read 142.9K   5.1K   88   15
A VB.NET class for drawing text on a path.

Sample Image - Text_on_Path_with_VBNET.jpg

Sample Image - Text_on_Path_with_VBNET.jpg

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.

a²+b²=c²

VB
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).

VB
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)
_gp.AddCurve(_points)

_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.

VB
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).

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Architect
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionMy Vote of 4. You have done the job, but it lacks on precision Pin
User 1106120120-Feb-16 11:12
User 1106120120-Feb-16 11:12 
QuestionQuick look does not reveal how to modify 'starting postion' of first char.... Pin
boblogan3-Aug-12 3:33
boblogan3-Aug-12 3:33 
Questiondraw text within any path? Pin
ASPUIPAB29-Oct-11 11:39
ASPUIPAB29-Oct-11 11:39 
GeneralC# version and with optimization. PinPopular
guidebee9-Jul-09 17:15
guidebee9-Jul-09 17:15 
GeneralGreat job Pin
sf4all15-Jul-09 4:45
sf4all15-Jul-09 4:45 
GeneralRe: C# version and with optimization. Pin
Member 740216621-Apr-11 5:21
Member 740216621-Apr-11 5:21 
GeneralRe: C# version and with optimization. Pin
Marc Clifton14-Sep-14 5:25
mvaMarc Clifton14-Sep-14 5:25 
GeneralInterested in getting 1 developer license Pin
LarryKudrow17-Mar-08 5:49
LarryKudrow17-Mar-08 5:49 
Hi Alexander,
I am Interested in getting 1 developer license. Could you please contact me:
larrykudrow@optonline.net
Thank you.
GeneralProblem with Running Project Pin
bnjneer5-Jan-08 14:30
bnjneer5-Jan-08 14:30 
GeneralProblems Pin
mysticwars13-Apr-07 9:36
mysticwars13-Apr-07 9:36 
GeneralSqrt and Decimal Pin
Wesner Moise2-May-06 19:11
Wesner Moise2-May-06 19:11 
GeneralRe: Sqrt and Decimal Pin
jarvisa29-May-06 22:55
jarvisa29-May-06 22:55 
GeneralVGdotnet Pin
PSwartzell124-Apr-06 5:51
PSwartzell124-Apr-06 5:51 
GeneralExcellent Pin
AllanNielsen23-Apr-06 16:53
AllanNielsen23-Apr-06 16:53 
GeneralInteresting graphical effect... Pin
Stanciu Vlad21-Apr-06 8:32
Stanciu Vlad21-Apr-06 8:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.