Click here to Skip to main content
6,595,854 members and growing! (18,621 online)
Email Password   helpLost your password?
Multimedia » GDI+ » General     Intermediate

Text on Path with VB.NET

By Alexander Seel

A VB.NET class for drawing text on a path.
VB, Windows, .NET, Visual Studio, GDI+, Dev
Posted:20 Apr 2006
Updated:2 May 2006
Views:40,225
Bookmarked:56 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
Prize winner in Competition "VB.NET Mar 2006"
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
32 votes for this article.
Popularity: 6.84 Rating: 4.55 out of 5
2 votes, 6.3%
1
1 vote, 3.1%
2
3 votes, 9.4%
3
5 votes, 15.6%
4
21 votes, 65.6%
5

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�

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

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

About the Author

Alexander Seel


Member

Occupation: Software Developer
Location: Germany Germany

Other popular GDI+ articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 10 of 10 (Total in Forum: 10) (Refresh)FirstPrevNext
GeneralC# version and with optimization. Pinmemberguidebee18:15 9 Jul '09  
GeneralGreat job Pinmembersf4all5:45 15 Jul '09  
GeneralInterested in getting 1 developer license PinmemberLarryKudrow6:49 17 Mar '08  
GeneralProblem with Running Project Pinmemberbnjneer15:30 5 Jan '08  
GeneralProblems Pinmembermysticwars10:36 13 Apr '07  
GeneralSqrt and Decimal PinmemberWesner Moise20:11 2 May '06  
GeneralRe: Sqrt and Decimal Pinmemberjarvisa23:55 29 May '06  
GeneralVGdotnet PinmemberPSwartzell16:51 24 Apr '06  
GeneralExcellent Pinmemberchimeric6917:53 23 Apr '06  
GeneralInteresting graphical effect... PinmemberVlad Stanciu9:32 21 Apr '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 2 May 2006
Editor: Smitha Vijayan
Copyright 2006 by Alexander Seel
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project