dcsimg
Results 1 to 4 of 4

Thread: Center Drawn Text

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2016
    Posts
    18

    Center Drawn Text

    I am need of some help. I am drawing a picture onto another picture and then placing text onto the picture. Basically its like a mugshot picture and the person is holding a placard and their pedigree info is on the placard. The problem I am having is placing the pedigree info on the placard centered. The lengths will always be different and I can't figure out how to center the text. Can anyone help me out on this? I would really appreciate it.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,569

    Re: Center Drawn Text

    The obvious thing that you'll need to do is measure how long your String will be and then subtract half of the length from the center of whatever it is that you're drawing it on.

    To measure the String, you can call Graphics.MeasureString. The method returns a SizeF which then you can get the Width from. Subtract the Width from the center of object that you're drawing on(drawing canvas's width divided by 2).

  3. #3
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,110

    Re: Center Drawn Text

    MeasureString is usable for a single line of text, but there are better ways especially if your text is longer. One of them is to draw the text into the placard rectangle with Graphics.DrawString with a StringFormat argument. You specify StringFormat.Alignment = Center for horizontal centering and StringFormat.LineAlignment for vertical centerring, and there are many other properties you can modify through StringFormat.FormatFlags. Here's an example in the Paint event of a PictureBox.
    Code:
    Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
          Dim placard As New Rectangle(50, 50, 150, 100)
          e.Graphics.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit
          Using sf As New StringFormat With _
                {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center}
             e.Graphics.DrawString( _ 
               "Here you may read several lines of my pedigree text, automatically wrapped and centered.", _
                Me.Font, Brushes.Blue, placard, sf)
          End Using
       End Sub
    An even easier way is to use a Label, which can also automatically center text. If you want it to appear on top of a PictureBox, you must make the label a Child control by setting its Parent property to the PictureBox; if you added the Label in the designer, you must adjust its X and Y properties accordingly.

    BB

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Sep 2016
    Posts
    18

    Re: Center Drawn Text

    boops boops, thank you so much!!! Your code worked perfect for me and is exactly what I needed! I couldn't figure it out but I am thankful you took the time to help me out. Thanks to all on this board for help when needed!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width