3. Using the GraphicsPath
This example requires you to import System.Drawing.Drawing2D.
A GraphicsPath is a simple, but useful object that allows you to define a shape (or path of lines). It has AddXXX methods that are pretty much the same as the DrawXXX methods of the Graphics object, but you just don't define a color, there are no FillXXX, and there's no DrawImage. It's nice and simple. I assume that if you're using these examples, you probably already know something about GDI+ and the Graphics object. If you don't, I'm making another code/tutorial thing later for beginners. Anyways, since all the methods are self-explanatory, I won't go into detail. I'll just say "they're the same".
You create a GraphicsPath object like so:
You can also specify a FillMode, which is used for when your shapes cross themselves. Normally, (FillMode.Alternate) the pattern would be drawn in every second square... it's hard to explain without seeing it. The general way of doing things is to say "if you have shapes that cross themselves, use FillMode.Winding". It's the parameter of the constructor.
Dim gp As New GraphicsPath()
You close your current shape using CloseFigure():
Even if you think you've manually closed your figure, it's a good idea to use this anyways.
You can also create a Region object from a GraphicsPath (more on that later):
The GraphicsPath has many interesting properties, but they're a bit complicated and a bit of a waste of time to cover because they're rarely used. I'll cover them much later.
Dim r As New Region(gp)
Here's how you could create a rounded rectangle using the AddLine and AddArc methods of the GraphicsPath (you could even make an extension to the Graphics object, DrawRoundRectangle).
This function accepts a location, size and a radius and returns a GraphicsPath:
You may notice that I've made an odd choice of values for AddArc. This is important because you must draw the corners in the same "direction" as you're going. For example, if you draw a line from point a to point b, then point b to point d, then point a to e, you'll end up drawing another line from d to a, probably not what you want. You can see for yourself by changing the values in that code.
Private Function GetRoundRect(ByVal x As Integer, ByVal y As Integer, ByVal width As Integer, ByVal height As Integer, ByVal radius As Integer) As Drawing2D.GraphicsPath
Dim gp As New GraphicsPath()
Dim half As Integer = radius \ 2
'Add across the top
gp.AddLine(x + width - half, y, x + half, y)
'Add the upper-left hand corner
gp.AddArc(x, y, radius, radius, 270, -90)
'Add across the left side
gp.AddLine(x, y + half, x, y + height - half)
'Add the lower-left hand corner
gp.AddArc(x, y + height - 1 - radius, radius, radius, 180, -90)
'Add across the bottom
gp.AddLine(x + half, y + height - 1, x + width - half, y + height - 1)
'Add the lower-right hand corner
gp.AddArc(x + width - 1 - radius, y + height - 1 - radius, radius, radius, 90, -90)
'Add across the right side
gp.AddLine(x + width - 1, y + height - half, x + width - 1, y + half)
'Add the upper-right hand corner
gp.AddArc(x + width - 1 - radius, y, radius, radius, 0, -90)
'Close the figure
'Return the path