Results 1 to 5 of 5

Thread: Outline Text in a Label

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2026
    Posts
    33

    Outline Text in a Label

    Hello All,

    I have been struggling with a little project that needs a label to contain text with an outline aka stroke.
    The Text is white and the background in teal. I would like to be able to add a black stroke to the text contained in a Label

    I came across lots of interesting code and have figured out how to create just outline text but I need to have the text white with a black outline.

    The code I have been trying to use to achieve this is as follows

    Code:
    Imports System.Drawing.Drawing2D
    
    Public Class BorderLabel
        Inherits Label
        Public outline_color As Color = Color.Black
        Public border_thickness As Integer = 5
        Public Sub New()
            MyBase.New
        End Sub
    
    
        Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
            e.Graphics.FillRectangle(New SolidBrush(BackColor), ClientRectangle)
            Dim gp As GraphicsPath = New GraphicsPath
            Dim outline As Pen = New Pen(Me.outline_color, Me.border_thickness)
            Dim sf As StringFormat = New StringFormat
            Dim foreBrush As Brush = New SolidBrush(ForeColor)
            gp.AddString(GV.Text, Font.FontFamily, CType(Font.Style, Integer), Font.Size, ClientRectangle, sf)
            e.Graphics.ScaleTransform(1.3!, 1.35!)
            e.Graphics.SmoothingMode = SmoothingMode.HighQuality
            e.Graphics.DrawPath(outline, gp)
            e.Graphics.FillPath(foreBrush, gp)
        End Sub
    End Class
    This isn't my code but it does seem to indicate that it should do as I want.
    I created a new project to test this and just using the defaults
    Form1
    and a label named BorderLabel1 with some text "Test TEXT"

    So this is where I fall down. I cannot get it to work at all and am hoping that someone might be able to point out where I am going wrong.

    I simply want to use a label with the added functionality of being able to display the text with a border where the text color is defined, the border color is defined and the border width is defined, along with all the normal attributes associated with the control

    I thought that using a label named BorderLabel1 would distinguish the it from the normal Label control.

    I am looking forward to your advice and guidance

    Regards,
    Antony.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Outline Text in a Label

    The code you posted is for a custom label. It draws an outline around the text instead of just around the label...

    Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Label1.BackColor = Color.Teal
        Label1.ForeColor = Color.White
    End Sub
    
    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim outline_color As Color = Color.Black
        Dim border_thickness As Integer = 5
        Dim outlinePen As Pen = New Pen(outline_color, border_thickness)
        Dim r As New Rectangle(Label1.Left - 2, Label1.Top - 2, Label1.Width + 4, Label1.Height + 4) ' edited here
        e.Graphics.DrawRectangle(outlinePen, r)  ' edited here
    End Sub
    Edit: Slight improvement to the border appearance
    Last edited by .paul.; May 17th, 2026 at 08:12 AM.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Outline Text in a Label

    This is a standard Label with a Black border (as my example shows)

    Name:  17-05-2026 13.57.54.png
Views: 46
Size:  543 Bytes

    This is the custom Label you posted

    Name:  17-05-2026 13.58.17.png
Views: 49
Size:  1.5 KB

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Outline Text in a Label

    To use the custom control you have, add the class (as edited below) to your project, then after running (or rebuilding), you’ll find the BorderLabel control at the top of your toolbox, and you can add it to your form and set properties either in the properties window, or in your code…

    Code:
    Imports System.Drawing.Drawing2D
    
    Public Class BorderLabel
        Inherits Label
        Public outline_color As Color = Color.Black
        Public border_thickness As Integer = 5
        Public Sub New()
            MyBase.New
        End Sub
    
    
        Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
            e.Graphics.FillRectangle(New SolidBrush(BackColor), ClientRectangle)
            Dim gp As GraphicsPath = New GraphicsPath
            Dim outline As Pen = New Pen(Me.outline_color, Me.border_thickness)
            Dim sf As StringFormat = New StringFormat
            Dim foreBrush As Brush = New SolidBrush(ForeColor)
            gp.AddString(Me.Text, Font.FontFamily, CType(Font.Style, Integer), Font.Size, ClientRectangle, sf)
            e.Graphics.ScaleTransform(1.3!, 1.35!)
            e.Graphics.SmoothingMode = SmoothingMode.HighQuality
            e.Graphics.DrawPath(outline, gp)
            e.Graphics.FillPath(foreBrush, gp)
        End Sub
    End Class
    Note that when you change things in the custom control code, it’ll be that way for every instance of the control you use, so, for example, if you wanted outline_color to be different in 2 different instances of your control, you’d change it to a Property which means you can use the same code with optional colors in more than 1instance of the control.
    Last edited by .paul.; May 17th, 2026 at 01:14 PM.

  5. #5

    Thread Starter
    Member
    Join Date
    Feb 2026
    Posts
    33

    Re: Outline Text in a Label

    Thanks Paul,

    I have it working now.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width