Link to home
Start Free TrialLog in
Avatar of brian2k1
brian2k1

asked on

Drawing Text within a Circle in VB.NET

I've found an example of placing Text within a Rectangle (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/_gdiplus_the_state_of_a_graphics_object_usecsharp.asp)

However I need to place text within a Circle and also a polygon shape. I've tried to find examples using the eclipse but no luck yet.

To be clear I need the text to auto-wrap within the circle not just clip it off. I've been able to create a clipping region but not the wrapping effect I need within a circle.

Avatar of Bob Learned
Bob Learned
Flag of United States of America image

I have figured out one way of wrapping the text, but it is not an elegant solution, but it worked for me.  I needed to put multi-line text into a diamond shape.  The direction I took was to break the shape up into a series of rectangles that have coordinates calculated from the slope of the edges, with each one the heighth of the measured text.

Bob
Avatar of RonaldBiemans
RonaldBiemans

In don't think setclip will work for this because it makes the underlying graphic only draw within the clip area but it will start outside of it using the original region (does this make sense).

This will draw and wrap text within the boundaries of a elipse (or circle) (in this case I use a panel to draw on)

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim g As Graphics
        g = Panel1.CreateGraphics
        Dim myrect As New RectangleF(0, 0, 100, 100)
        Dim w As Single = CSng(((myrect.Width / 2) * Math.Cos(135 * (Math.PI / 180))) + (myrect.Width / 2))
        Dim myrect2 As New RectangleF(w, w, myrect.Width - w,myrect.Height  - w)

        g.DrawEllipse(New Pen(Color.Black), myrect)
        g.DrawString("bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla", Me.Font, New SolidBrush(Color.Black), myrect2)
    End Sub



See, I told you that it wasn't the best way :)  I never pretend to be the graphics expert, but I am trying to learn.

Bob
Avatar of brian2k1

ASKER

That works to some degree however when I change the eclipse to 475x475 (CD size) the text is off to the right a little bit and is in a rectangle shape instead of the circle.
I got totally psyched out, and thought that Ronald suggested was going to work, since it usually does :)

Bob
Sorry to let you down TheLearnedOne ;-).

And sorry Brian2k1, I though that was what you wanted, it is indeed in a rectangle since drawstring only excepts a rectangle or a point. About the slightly to the right that has probably to do with the single instead of a double.

I will have a look and see if I can make it to fill the whole circle (not much hope though without splitting the string)





No problem, I just struggled with a solution, and to find out that it could have been that easy--it was inticing :)

Bob
Hi Brian2k,

I'm almost there, I think :-)  just a question.

The method I've been working on starts at the top of the circle and goes down from there, so if there is little text it will all be at the top of the circle, is that a problem ?
no that will work. :-)
Ok, I'll have it finished tomorrow morning (Going home now).
Bye the way I think I'm able to make you choose if you want to start at the top, center or bottom.



ASKER CERTIFIED SOLUTION
Avatar of RonaldBiemans
RonaldBiemans

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ronald,

I'm not sure if it works exactly yet but I'm giving you the points anyway for effort ;-)

I'll follow up in this message with any changes should they be required.

Thanks a ton for your effort it has helped me understand the GDI better.
Well if you do experience problems let me know and we will figure it out. One thing though to keep in mind is I use
myArray = Split(MyString) (which splits the string on spaces which means no CRLF are allowed in the string)


TheLearnedOne, am I back in your grace yet ?

you were never out of it. you the man!
Thanks Brian2k1, Let me know how you are getting along with your project.

another note, I put in this piece of code to center the string if it is too short to fill the complete rectangle if you don't want that
              Dim ff As Single
                Dim ww As SizeF
                ww = e.Graphics.MeasureString(StringsToPrint(y), Myfont)
                ff = ((CType(Points(y), RectangleF).Width - ww.Width) / 2)
                Points(y) = New RectangleF(ff + CType(Points(y), RectangleF).X, CType(Points(y), RectangleF).Y, CType(Points(y), RectangleF).Width, CType(Points(y), RectangleF).Height)
                e.Graphics.DrawString(StringsToPrint(y), Myfont, New SolidBrush(Color.Black), Points(y))

use just this part

                e.Graphics.DrawString(StringsToPrint(y), Myfont, New SolidBrush(Color.Black), Points(y))