0

I'm having some trouble achieving some drawing in images in visual basic. I would like to write text totally centered in the image as automatic as possible. The best I can achieve is start the writing in the middle of the image. I would like the text centered in the image.

What I have right now:

Try
    Dim ficheiro As New WebClient
    Dim file = TextBox1.Text & "\" & jss.hits(Index).id & ".jpg"
    ficheiro.DownloadFile(jss.hits(Index).webformatURL, file)
    ficheiro.Dispose()

    Dim bmp As New Bitmap(Image.FromFile(file))
    Dim g As Graphics = Graphics.FromImage(bmp)

    g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
    g.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
    g.CompositingQuality = Drawing2D.CompositingQuality.HighQuality

    Dim drawFormat As New StringFormat()
    drawFormat.Alignment = StringAlignment.Center

    g.DrawString(strarr(1), New Font("Montserrat Bold", 20), Brushes.White, New RectangleF(0, bmp.Height / 2, bmp.Width, bmp.Height), drawFormat)
    Dim newpath = TextBox2.Text & "\" & strarr(0)
    If (Not System.IO.Directory.Exists(newpath)) Then
        System.IO.Directory.CreateDirectory(newpath)
    End If
    bmp.Save(newpath & "\" & jss.hits(Index).id & ".jpg")

Catch ex As Exception
    MsgBox(ex.Message)
End Try

As an output:

example text in image

As you can see, it starts the text in the middle of the image. I would like to get the center of the text, centered in the image. The text and image size could change, so it would be nice to automatically change the font size to fit the image if possible.

Also, after this, I would like to add another line, after this text, but starting in the middle to right only. I can't figure how I could do it. Thanks.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Tiago
  • 625
  • 5
  • 16
  • 2
    Probably you need to get the height of the string using [Graphics.MeasureString](https://learn.microsoft.com/en-us/dotnet/api/system.drawing.graphics.measurestring?view=net-5.0#System_Drawing_Graphics_MeasureString_System_String_System_Drawing_Font_System_Int32_System_Drawing_StringFormat_) – Steve May 18 '21 at 14:35
  • By the way, you should really use this code in the Paint event handler and use the Graphics object given to you in that event handler and do not forget to Dispose the Graphics. https://stackoverflow.com/questions/1572804/do-i-need-to-call-graphics-dispose#:~:text=Always%20call%20Dispose%20%28%29%20on%20any%20object%20that,practice%20is%20to%20wrap%20in%20a%20using%20block. – Steve May 18 '21 at 14:41
  • 1
    @Tiago - Feeling a lot confused by this, 'it starts the text in the middle of the image. I would like to get the center of the text, centered in the image'. – dbasnett May 18 '21 at 14:42
  • @dbasnett Yeah I feel I didn't explained well. As you can see in the output, the text starts in the center of the image (from center to bottom), but I would like the text to be centered as well in the image depending on the string size. I guess the MeasureString is what I need, need to investigate further since I was confused with the explanation in MS page :) – Tiago May 18 '21 at 14:48
  • 1
    Oh. You want the text centered vertically and horizontally, but right now only the first line of text is centered vertically. Multi-line text is off-center. – tgolisch May 18 '21 at 15:02
  • @tgolisch Yes that's right. Basically, I want the center point of the text, centered with the center point of the image and I'm having big troubles with that. – Tiago May 18 '21 at 15:08
  • 1
    You can concatenate the strings with a line feed (`String.Join()`), then draw the whole thing. No need to measure the strings. Add `drawFormat.LineAlignment = StringAlignment.Center` (to align also vertically) and use the whole Size of the Image as bounds. -- Note that the Graphics and StringFormat objects are disposable, so you **need** to call their `Dispose()` method. – Jimi May 18 '21 at 15:11

0 Answers0