User-865881720 posted
Would it be easier to set the width of your drawing rectangle to the width of the bitmap and use one of the overloaded methods of DrawString (with the format parameter's Alignment property set to StringAlignment.Center) to draw centered text
within the rectangle? Here's a section of code that I've used before to superimpose centered text over a randomly selected image:
ImageFileName = CStr(RandomRow("ID")) & ".jpg"
ArtistName = FlipName(CStr(RandomRow("Name")))
ArtistCityState = CStr(RandomRow("CS"))
ArtworkCaption = CStr(RandomRow("ArtworkCaption"))
sf = New StringFormat(StringFormatFlags.LineLimit)
sf.Alignment = StringAlignment.Center
Img = New Bitmap(Server.MapPath("./images/artwork/" & ImageFileName))
H = Img.Height
W = Img.Width
WScaled = 250
HScaled = CInt((H * WScaled) / W)
ImgFormat = Img.RawFormat()
Canvas = New Bitmap(WScaled, HScaled + 200)
g = Graphics.FromImage(Canvas)
g.Clear(System.Drawing.Color.Beige)
g.TextRenderingHint = TextRenderingHint.AntiAlias
g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
Dim MyFontCollection As New PrivateFontCollection
MyFontCollection.AddFontFile(Server.MapPath("/") & dataPath & "FRSCRIPT.TTF")
Dim Fnt1 As Font = New Font(MyFontCollection.Families(0), 18, FontStyle.Bold)
Dim Fnt2 As Font = New Font("Arial Black", 10, FontStyle.Regular)
szf = g.MeasureString(ArtworkCaption, Fnt1, WScaled, sf)
rectf = New RectangleF(0, HScaled + 6, WScaled, szf.Height)
g.DrawString(ArtworkCaption, Fnt1, System.Drawing.Brushes.Black, rectf, sf)
rectf.Offset(0, szf.Height + 6)
Leading = Fnt2.Height
rectf.Height = Leading * 2
g.DrawString(ArtistName & vbCrLf & ArtistCityState, Fnt2, System.Drawing.Brushes.Black, rectf, sf)