I'm writing a program that can print some text (with a special font) on an existing image. My problem is that I, somehow, need to get the width of the text that was written on the picture. As you can see, I already have found out how to get the height, but
I also need the width.
Thanks in advance.
Sub Page_Load(sender As Object, e As EventArgs)
Dim bmp As New Bitmap(Server.MapPath(Request.QueryString("i")))
'Declare the bitmap. The querystring i tells where the picture is located
Dim g As Graphics = Graphics.FromImage(bmp)
g.SmoothingMode = SmoothingMode.AntiAlias
'Set the smoothing mode to antialias
Dim text_string As String = "The text goes here"
'This is the text that will be printed on the picture
Dim font_transparency As Integer = 100
'The opacity of the text. 0-100%.
Dim pointF As New PointF(80, 40)
'The position of the text. x, y.
Dim solidBrush As New SolidBrush(Color.FromArgb(font_transparency*2.55,250,216,22))
'The last three numbers represent the value of Red, Green and Blue. 0-255.
Dim font_size As Integer = 24
'The size of the text
Dim privateFontCollection As New PrivateFontCollection()
'to load in the font add the font file to the private collection.
privateFontCollection.AddFontFile(Server.MapPath("./fonts/CHERL.TTF"))
Dim thisFont As FontFamily = privateFontCollection.Families(0)
'Declare the font
Dim regFont As New Font(thisFont, font_size, FontStyle.Bold, GraphicsUnit.Pixel)
'family font size font style graphics unit set to pixel
'Slap the text on the picture
g.DrawString(text_string, regFont, solidBrush, pointF)
'text font color position
'Response.Write(regFont.Height)
'To get the height of the text in pixels... but I need to get the width! How?
Response.ContentType = "image/Jpeg"
bmp.Save(Response.OutputStream, ImageFormat.Jpeg)
End Sub
iver56
Member
13 Points
35 Posts
Get the width of a piece of text
Sep 16, 2008 11:03 AM|LINK
Hi!
I'm writing a program that can print some text (with a special font) on an existing image. My problem is that I, somehow, need to get the width of the text that was written on the picture. As you can see, I already have found out how to get the height, but I also need the width.
Thanks in advance.
Sub Page_Load(sender As Object, e As EventArgs) Dim bmp As New Bitmap(Server.MapPath(Request.QueryString("i"))) 'Declare the bitmap. The querystring i tells where the picture is located Dim g As Graphics = Graphics.FromImage(bmp) g.SmoothingMode = SmoothingMode.AntiAlias 'Set the smoothing mode to antialias Dim text_string As String = "The text goes here" 'This is the text that will be printed on the picture Dim font_transparency As Integer = 100 'The opacity of the text. 0-100%. Dim pointF As New PointF(80, 40) 'The position of the text. x, y. Dim solidBrush As New SolidBrush(Color.FromArgb(font_transparency*2.55,250,216,22)) 'The last three numbers represent the value of Red, Green and Blue. 0-255. Dim font_size As Integer = 24 'The size of the text Dim privateFontCollection As New PrivateFontCollection() 'to load in the font add the font file to the private collection. privateFontCollection.AddFontFile(Server.MapPath("./fonts/CHERL.TTF")) Dim thisFont As FontFamily = privateFontCollection.Families(0) 'Declare the font Dim regFont As New Font(thisFont, font_size, FontStyle.Bold, GraphicsUnit.Pixel) 'family font size font style graphics unit set to pixel 'Slap the text on the picture g.DrawString(text_string, regFont, solidBrush, pointF) 'text font color position 'Response.Write(regFont.Height) 'To get the height of the text in pixels... but I need to get the width! How? Response.ContentType = "image/Jpeg" bmp.Save(Response.OutputStream, ImageFormat.Jpeg) End Sub