Text Output Functions (Colorizing for Console)

   Function funWriteTextColorful(sText As String) As Boolean

        Dim aColors = {ConsoleColor.Red, ConsoleColor.Magenta, ConsoleColor.Yellow, ConsoleColor.Green, ConsoleColor.Cyan, ConsoleColor.White}

        Static iColorNum As Integer

        'ReD,orange,yell,gre,blu,ind,vio

        Dim oldColor As ConsoleColor = Console.ForegroundColor

        For x As Integer = 0 To sText.Length - 1

            Dim ch As Char = Strings.Mid(sText, x + 1, 1)

            Console.ForegroundColor = aColors(iColorNum)

            Console.Write(ch)

            If Not Char.IsWhiteSpace(ch) Then iColorNum += 1

            If iColorNum Mod UBound(aColors) = 0 Then iColorNum = 0

        Next

        Console.ForegroundColor = oldColor

    End Function



    Function funWriteWordinColor(sText As String, cfFontColor As ConsoleColor)

        Dim oldColor As ConsoleColor = Console.ForegroundColor

        Console.ForegroundColor = cfFontColor

        Console.Write(sText)

        Console.ForegroundColor = oldColor

    End Function


Convert to RGB


Public Function ConvertToRbg(ByVal HexColor As String) As Color
    Dim Red As String
    Dim Green As String
    Dim Blue As String
    HexColor = Replace(HexColor, "#", "")
    Red = Val("&H" & Mid(HexColor, 1, 2))
    Green = Val("&H" & Mid(HexColor, 3, 2))
    Blue = Val("&H" & Mid(HexColor, 5, 2))
    Return Color.FromArgb(Red, Green, Blue)
End Function

Hex to Color

Public Shared Function HexToColor(ByVal hexColor As String) As Color
    If hexColor.IndexOf("#"c) <> -1 Then
        hexColor = hexColor.Replace("#", "")
    End If
    Dim red As Integer = 0
    Dim green As Integer = 0
    Dim blue As Integer = 0
    If hexColor.Length = 6 Then
        red = Integer.Parse(hexColor.Substring(0, 2), NumberStyles.AllowHexSpecifier)
        green = Integer.Parse(hexColor.Substring(2, 2), NumberStyles.AllowHexSpecifier)
        blue = Integer.Parse(hexColor.Substring(4, 2), NumberStyles.AllowHexSpecifier)
    ElseIf hexColor.Length = 3 Then
        red = Integer.Parse(hexColor(0).ToString() + hexColor(0).ToString(), NumberStyles.AllowHexSpecifier)
        green = Integer.Parse(hexColor(1).ToString() + hexColor(1).ToString(), NumberStyles.AllowHexSpecifier)
        blue = Integer.Parse(hexColor(2).ToString() + hexColor(2).ToString(), NumberStyles.AllowHexSpecifier)
    End If
    Return Color.FromArgb(red, green, blue)
End Function

Text Output  (spacing)

   Function funFormatColumns(sString As String, iCount As Integer, blnFormatAddSpace As Boolean)

        If iCount > Len(sString) Then

            Dim iDiff As Integer = iCount - Len(sString)

            funFormatColumns = sString & Space(iDiff)

        Else

            funFormatColumns = Left(sString, iCount)

        End If

        If blnFormatAddSpace Then funFormatColumns = funFormatColumns & " "

    End Function


Create Text as an Image on the fly and add to control

New Way

Function createTextImage(text As String, oColor As Color) As Bitmap
        'Dim text As String = txtText.Text.Trim()
        Dim bitmap As New Bitmap(1, 1)
        Dim font As New Font("arial", 13, FontStyle.Bold, GraphicsUnit.Pixel)
        Dim graphics As Graphics = Graphics.FromImage(bitmap)
        Dim width As Integer = CInt(graphics.MeasureString(text, font).Width)
        Dim height As Integer = CInt(graphics.MeasureString(text, font).Height)

        bitmap = New Bitmap(bitmap, New Size(width, height))
        graphics = Graphics.FromImage(bitmap)
        'graphics.Clear(Color.Black)
        graphics.SmoothingMode = SmoothingMode.AntiAlias
        graphics.TextRenderingHint = TextRenderingHint.AntiAlias
        'graphics.DrawString(text, font, New SolidBrush(Color.FromArgb(255, 0, 0)), 0, 0)
        graphics.DrawString(text, font, New SolidBrush(oColor), 0, 0)
        'graphics.DrawString(text, font, New SolidBrush(oColor), 0, 0)
        graphics.Flush()
        graphics.Dispose()
        Return bitmap
   End Function

 

Old way

    Function createTextImage(text As String, oColor As Color) As String

        'Dim text As String = txtText.Text.Trim()

        Dim bitmap As New Bitmap(1, 1)

        Dim font As New Font("Arial", 25, FontStyle.Bold, GraphicsUnit.Pixel)

        Dim graphics As Graphics = Graphics.FromImage(bitmap)

        Dim width As Integer = CInt(graphics.MeasureString(text, font).Width)

        Dim height As Integer = CInt(graphics.MeasureString(text, font).Height)

        bitmap = New Bitmap(bitmap, New Size(width, height))

        graphics = Graphics.FromImage(bitmap)

        graphics.Clear(Color.White)

        graphics.SmoothingMode = SmoothingMode.AntiAlias

        graphics.TextRenderingHint = TextRenderingHint.AntiAlias

        'graphics.DrawString(text, font, New SolidBrush(Color.FromArgb(255, 0, 0)), 0, 0)

        graphics.DrawString(text, font, New SolidBrush(oColor), 0, 0)

        graphics.Flush()

        graphics.Dispose()

        Dim fileName As String = Path.GetFileNameWithoutExtension(Path.GetRandomFileName()) & ".jpg"

        bitmap.Save(Environment.ExpandEnvironmentVariables("%TEMP%") & "\" & fileName, ImageFormat.Jpeg)

        'imgText.ImageUrl = "~/images/" & fileName

        'imgText.Visible = True

        Return Environment.ExpandEnvironmentVariables("%TEMP%") & "\" & fileName

    End Function

https://www.aspsnippets.com/Articles/Create-Text-Image-on-the-fly-with-ASPNet.aspx

ToIcon function icon

<System.Runtime.CompilerServices.Extension> _
Public Shared Function ToIcon(img As Bitmap, makeTransparent As Boolean, colorToMakeTransparent As Color) As Icon
    If makeTransparent Then
        img.MakeTransparent(colorToMakeTransparent)
    End If
    Dim iconHandle = img.GetHicon()
    Return Icon.FromHandle(iconHandle)
End Function

 

Display and render an SVG file

by using a WebBrowser control, as PictureBox cannot do it

WebBrowser1.Navigate(New Uri(jArrayLocation("country_flag")))

    Dim wbWidth As Integer
    Dim wbHeight As Integer
    Dim pswaHeight As Object
    Dim pswaWidth As Object
    Dim pswaHeightInt As Integer
    Dim pswaWidthInt As Integer

    Private Enum Exec
        OLECMDID_OPTICAL_ZOOM = 63
    End Enum

    Private Enum execOpt
        OLECMDEXECOPT_DODEFAULT = 0
        OLECMDEXECOPT_PROMPTUSER = 1
        OLECMDEXECOPT_DONTPROMPTUSER = 2
        OLECMDEXECOPT_SHOWHELP = 3
    End Enum


    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        pswaHeight = WebBrowser1.Height ' Screen.PrimaryScreen.WorkingArea.Height
        pswaWidth = WebBrowser1.Width ' Screen.PrimaryScreen.WorkingArea.Width
        pswaHeightInt = CInt(pswaHeight)
        pswaWidthInt = CInt(pswaWidth)
    End Sub