I translated this method mentioned in this answer to the VB.NET method. Maybe this helps someone.
Public Function DrawText(ByVal text As String, ByRef font As Font, ByRef textColor As Color, ByRef backColor As Color) As Image ' first, create a dummy bitmap just to get a graphics object Dim img As Image = New Bitmap(1, 1) Dim drawing As Graphics = Graphics.FromImage(img) ' measure the string to see how big the image needs to be Dim textSize As SizeF = drawing.MeasureString(Text, Font) ' free up the dummy image and old graphics object img.Dispose() drawing.Dispose() ' create a new image of the right size img = New Bitmap(CType(textSize.Width, Integer), CType(textSize.Height, Integer)) drawing = Graphics.FromImage(img) ' paint the background drawing.Clear(BackColor) ' create a brush for the text Dim textBrush As Brush = New SolidBrush(textColor) drawing.DrawString(text, font, textBrush, 0, 0) drawing.Save() textBrush.Dispose() drawing.Dispose() Return img End Function
Change Typo fixed.
Freddy Oct 27 '15 at 23:53 on 2015-10-27 23:53
source share