Why is the circle I drew larger than expected?

67 views Asked by At

I drew an ellipse/circle with Graphics. It is the right size when I am in the WYSIWYG editor. However, when I run the program, the circle is larger.

correct sized circle in editor

too large sized circle

The "too large sized circle" is also of lower resolution and has jagged edges.

How do I make it the same size?

    Protected Overrides Sub OnPaint(
    ByVal pEvent _
        As PaintEventArgs)

    Me.Size = New Size(heightWidth, heightWidth)
    'MyBase.OnPaint(pEvent)

    Dim CenterCircle _
        As New Rectangle(0, 0, heightWidth, heightWidth)


    Dim colorBigCircle As Color = Color.FromArgb(218, 227, 243)
    Dim colorSmallCircle As Color = Color.FromArgb(67, 99, 155)
    pEvent.Graphics.FillEllipse(
            New SolidBrush(
                colorBigCircle
                ),
            CenterCircle)
    pEvent.Graphics.DrawEllipse(
            New Pen(
                colorSmallCircle, 3
                ),
            CenterCircle)
2

There are 2 answers

0
DVELPR On

The "too large sized circle" is also of lower resolution and has jagged edges.

    pEvent.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
0
LarsTech On

Setting the size of the form in the paint event is a big no-no. Either set the form's dimensions in the designer with a form border that isn't sizable, or set the form's MinimumSize and MaximumSize to the same dimension.

You should dispose of your drawing objects. Setting the SmoothingMode will fix the jagged edges of the circle. Your paint code should look something like this:

Protected Overrides Sub OnPaint(e As PaintEventArgs)
  MyBase.OnPaint(e)
  e.Graphics.SmoothingMode = SmoothingMode.AntiAlias

  Dim centerCircle As New Rectangle(0, 0, Me.ClientSize.Width, Me.ClientSize.Height)
  Dim colorBigCircle As Color = Color.FromArgb(218, 227, 243)
  Dim colorSmallCircle As Color = Color.FromArgb(67, 99, 155)
  Using br As New SolidBrush(colorBigCircle)
    e.Graphics.FillEllipse(br, centerCircle)
  End Using
  Using p As New Pen(colorSmallCircle, 3) With {.Alignment = PenAlignment.Inset}
    e.Graphics.DrawEllipse(p, centerCircle)
  End Using
End Sub