Creating image from text - how to ensure image size is correct based on size of text

Darren Rose 311 Reputation points
2022-08-04T15:38:31.02+00:00

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
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 90,681 Reputation points
    2022-08-04T15:51:38.067+00:00

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.