You can use Graphics.MeasureString
Creating image from text - how to ensure image size is correct based on size of text
Darren Rose
311
Reputation points
Hi
I am using the below example code to create an image from text, which works fine.
But I want to ensure that the size of the image can be determined automatically based on the string provided, the font and the font size, rather than having to stipulate the size.
Any ideas please.
Thanks
Dim bmp As Bitmap = New Bitmap(300, 50)
Using graphics As Graphics = System.Drawing.Graphics.FromImage(bmp)
Dim font As Font = New Font("Lucida Handwriting", 12)
graphics.FillRectangle(New SolidBrush(System.Drawing.Color.Transparent), 0, 0, bmp.Width, bmp.Height)
graphics.DrawString("Darren Rose", font, New SolidBrush(System.Drawing.Color.Black), 0, 0)
graphics.Flush()
font.Dispose()
graphics.Dispose()
End Using
Developer technologies | VB
2,892 questions