[RESOLVED] [2008] Drawing rounded rectangle.
Hi All,
I have a porblem with drawing a rounded rectangle.
The function gives me an error:
Quote:
Function 'GetRoundedRectPath' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.
Here's my code;
Code:
Public Class Form1
Dim g As Graphics
Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
Application.Exit()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim width As Integer = Me.ClientRectangle.Width
Dim heigth As Integer = Me.ClientRectangle.Height
Dim rect As Rectangle = New Rectangle(10, 20, width - 20, heigth - 20)
Dim path As GraphicsPath = getRoundedRectPath(rect, CInt(width / 10))
g.FillPath(Brushes.LavenderBlush, path)
g.DrawPath(Pens.Peru, path)
End Sub
Function GetRoundedRectPath(rect As Rectangle, ByVal radius As Integer) As GraphicsPath
Dim diameter As Integer = 2 * radius
Dim arcrect As Rectangle = New Rectangle(rect.Location, New Size(diameter, diameter))
Dim path As GraphicsPath = New GraphicsPath()
' top left
path.AddArc(arcrect, 180, 90)
' top right
path.AddArc(arcrect, 270, 90)
' bottem right
path.AddArc(arcrect, 0, 90)
' bottem left
path.AddArc(arcrect, 90, 90)
path.CloseFigure()
End Function
End Class
Thanks in advance,
sparrow1
Re: [2008] Drawing rounded rectangle.
Your function is not returning anything.
Add
Return path
at the end of the Function.
Re: [2008] Drawing rounded rectangle.
shouldn't you use a return path in struction in the 'GetRoundedRectPath' function? Mayybe i'm missing something.
Re: [2008] Drawing rounded rectangle.
Mendhak,
Thanks for your quick reply.
But now, I'm having trouble with this line:
Code:
g.FillPath(Brushes.LavenderBlush, Path) ' Object reference not set to an instance of an object.
Thanks,
sparrow1
Re: [2008] Drawing rounded rectangle.
How exactly does your code look right now?
Re: [2008] Drawing rounded rectangle.
your global variable g as not been initialize before you use in the paint event of the form
Re: [2008] Drawing rounded rectangle.
Quote:
Originally Posted by Atheist
How exactly does your code look right now?
Hi,
Here's my changed code;
Code:
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim g As Graphics = e.Graphics ' this was missing as object reference!
Dim width As Integer = Me.ClientRectangle.Width
Dim heigth As Integer = Me.ClientRectangle.Height
Dim rect As Rectangle = New Rectangle(10, 10, width - 20, heigth - 20)
Dim path As GraphicsPath = GetRoundedRectPath(rect, (width / 10))
g.FillPath(Brushes.Yellow, Path)
g.DrawPath(Pens.Black, path)
End Sub
Function GetRoundedRectPath(ByVal rect As Rectangle, ByVal radius As Integer) As GraphicsPath
Dim diameter As Integer = 20 * radius
Dim arcrect As Rectangle = New Rectangle(rect.Location, New Size(diameter, diameter))
Dim path As GraphicsPath = New GraphicsPath()
' top left
path.AddArc(arcrect, 180, 90)
' top right
path.AddArc(arcrect, 270, 90)
' bottem right
path.AddArc(arcrect, 0, 90)
' bottem left
path.AddArc(arcrect, 90, 90)
path.CloseFigure()
Return path
End Function
The result however isn't the one I need.
It draws an ellipise instead of a roundedrectangle.
Re: [2008] Drawing rounded rectangle.
try this:
vb Code:
Public Function roundedRectangle(ByVal X As Integer, ByVal Y As Integer, _
ByVal Width As Integer, ByVal Height As Integer, ByVal diameter As Integer) As System.Drawing.Drawing2D.GraphicsPath
''the 'diameter' parameter changes the size of the rounded region
Dim graphics_path As New System.Drawing.Drawing2D.GraphicsPath
Dim BaseRect As New RectangleF(X, Y, Width, Height)
Dim ArcRect As New RectangleF(BaseRect.Location, New SizeF(diameter, diameter))
'top left Arc
graphics_path.AddArc(ArcRect, 180, 90)
graphics_path.AddLine(X + CInt(diameter / 2), _
Y, X + Width - CInt(diameter / 2), Y)
' top right arc
ArcRect.X = BaseRect.Right - diameter
graphics_path.AddArc(ArcRect, 270, 90)
graphics_path.AddLine(X + Width, _
Y + CInt(diameter / 2), X + Width, _
Y + Height - CInt(diameter / 2))
' bottom right arc
ArcRect.Y = BaseRect.Bottom - diameter
graphics_path.AddArc(ArcRect, 0, 90)
graphics_path.AddLine(X + CInt(diameter / 2), _
Y + Height, X + Width - CInt(diameter / 2), _
Y + Height)
' bottom left arc
ArcRect.X = BaseRect.Left
graphics_path.AddArc(ArcRect, 90, 90)
graphics_path.AddLine(X, Y + CInt(diameter / 2), _
X, Y + Height - CInt(diameter / 2))
Return graphics_path
End Function
Re: [2008] Drawing rounded rectangle.
Thanks to your example .paul. , I found that I didn't draw the arcrect path.
I've changed my code in that way and now I have a good looking roundedrectangle.
Here's my code:
Code:
Imports System.Drawing
Imports System.Drawing.Drawing2D
Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
Application.Exit()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim g As Graphics = e.Graphics
Dim width As Integer = Me.ClientRectangle.Width
Dim heigth As Integer = Me.ClientRectangle.Height
Dim rect As Rectangle = New Rectangle(10, 20, width - 50, heigth - 50)
Dim path As GraphicsPath = GetRoundedRectPath(rect, (width / 10))
g.FillPath(Brushes.Yellow, Path)
g.DrawPath(Pens.Black, path)
path.Dispose()
End Sub
Function GetRoundedRectPath(ByVal rect As Rectangle, ByVal radius As Integer) As GraphicsPath
Dim diameter As Integer = 2 * radius
Dim arcrect As Rectangle = New Rectangle(rect.Location, New Size(diameter, diameter))
Dim path As GraphicsPath = New GraphicsPath()
' top left
path.AddArc(arcrect, 180, 90)
' top right
arcrect.X = rect.Right - diameter
path.AddArc(arcrect, 270, 90)
' bottom right
arcrect.Y = rect.Bottom - diameter
path.AddArc(arcrect, 0, 90)
' bottom left
arcrect.X = rect.Left
path.AddArc(arcrect, 90, 90)
path.CloseFigure()
Return path
End Function
End Class
Thanks for the hints,
sparrow1