Fixing ellipses drawn with GDI+ in user control

Blake81

Well-known member
Joined
Feb 23, 2006
Messages
304
Location
Georgia, USA
Programming Experience
1-3
Most of this user control works (that I've written about in other posts), but I'm still having a problem with two of the ellipses not resizing properly when I resize the control at design time. I've changed the values for width and height on everything to math expressions that should make them relative to the size of the control, but they still don't look right. The problem is when the height of the control exceeds some number (around 154, I think). The two ellipses that are giving me problems are Ellipse1 and Ellipse2. I'd appreciate any help with this one.

This is where everthing is declared.

VB.NET:
Private stroke3 As System.Drawing.Pen = New System.Drawing.Pen(System.Drawing.Color.FromArgb(0, 255, 255, 255), 1.0!)

    Private Ellipse3Brush As System.Drawing.SolidBrush = New System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(255, 255, 255, 255))

    Private Ellipse3Pen As System.Drawing.Pen = New System.Drawing.Pen(System.Drawing.Color.DimGray, 2.0!)

    Protected Ellipse3 As System.Drawing.Rectangle = New System.Drawing.Rectangle(0, 1, Me.Width - 6, Me.Height)

    Protected Ellipse1 As System.Drawing.Rectangle = New System.Drawing.Rectangle(3, 4, Me.Width * 0.98, Me.Height * 0.93)

    Private Ellipse1Brush As System.Drawing.Drawing2D.LinearGradientBrush = New System.Drawing.Drawing2D.LinearGradientBrush(New System.Drawing.Rectangle(0, 0, Me.Width * 0.98, Me.Height * 0.93), System.Drawing.Color.FromArgb(215, 255, 0, 0), System.Drawing.Color.FromArgb(255, 0, 0, 0), System.Drawing.Drawing2D.LinearGradientMode.Vertical)

    Protected Text3 As String = "TESTING"

    Private Text3Format As System.Drawing.StringFormat = New System.Drawing.StringFormat(System.Drawing.StringFormat.GenericTypographic)

    Private Text3Font As System.Drawing.Font = New System.Drawing.Font("DS-Digital", 48.0!, CType(1, System.Drawing.FontStyle), System.Drawing.GraphicsUnit.Point, 1)


    Private Text3Rect As System.Drawing.RectangleF = New System.Drawing.RectangleF(0, Me.ClientSize.Height - 57, Me.ClientSize.Width + 3, Me.ClientSize.Height - 57)

    Private Text3Brush As System.Drawing.Drawing2D.LinearGradientBrush = New System.Drawing.Drawing2D.LinearGradientBrush(Me.ClientRectangle, bcolor, System.Drawing.Color.FromArgb(110, 255, 255, 255), System.Drawing.Drawing2D.LinearGradientMode.Vertical)

    Private Ellipse2Brush As System.Drawing.Drawing2D.LinearGradientBrush = New System.Drawing.Drawing2D.LinearGradientBrush(New System.Drawing.Rectangle(42, 3, 212, 83), System.Drawing.Color.FromArgb(255, 255, 255, 255), System.Drawing.Color.FromArgb(0, 255, 255, 255), System.Drawing.Drawing2D.LinearGradientMode.Vertical)

    Protected Ellipse2 As System.Drawing.Rectangle = New System.Drawing.Rectangle(42, 3, Me.Width * 0.55, Me.Height * 0.47)

    Private Ellipse5Brush As System.Drawing.SolidBrush = New System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(0, 255, 255, 255))

    Private Ellipse5Pen As System.Drawing.Pen = New System.Drawing.Pen(System.Drawing.Color.Black, 2.0!)

    Protected Ellipse5 As System.Drawing.Rectangle = New System.Drawing.Rectangle(1, 0, Me.Width - 6, Me.Height)

I call this sub before the graphics object does its painting

VB.NET:
 Private Sub InitializeGraphics()
        Ellipse1 = New System.Drawing.Rectangle(3, 4, Me.Width * 0.98, Me.Height * 0.93)
        Me.Text3Format.Alignment = System.Drawing.StringAlignment.Center
        Me.BackColor = Color.Transparent
        Text3Rect = New System.Drawing.RectangleF(0, Me.ClientSize.Height / 2 - 24, Me.ClientSize.Width + 3, Me.ClientSize.Height - 57)
        Ellipse3 = New System.Drawing.Rectangle(0, 1, Me.Width - 6, Me.Height)
        Ellipse2 = New System.Drawing.Rectangle(42, 3, Me.Width - 85, Me.Height - 72)
        Ellipse5 = New System.Drawing.Rectangle(1, 0, Me.Width - 6, Me.Height)

    End Sub
 
Look into using RectangleF instead of Rectangle. Also I think you're using too many constant integers. Use a calculation instead such as a percentage of the clientsize width or height.

Why do you do this:
VB.NET:
Private Ellipse3Brush As System.Drawing.SolidBrush = New System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(255, 255, 255, 255))
instead of using a property of the System.Drawing.Brushes class:
VB.NET:
Brushes.White
 
I only found one problem with your code that you have shown. You did not show how you plotted your ellipses. I did not want to create a control so I just used a form. The enclosed code shows what I did and it worked.
On your "Private Ellipse5Brush As New SolidBrush(Color.FromArgb(0, 255, 255, 255))" you will never see this as the alpha is set to 0 which is transparent.
Also, nothing to do with anything that is right or wrong. I feel it is easier reading a program if you import your namespaces, aside from doing so much typing. In the code I shortened up a most of your code. i.e.
VB.NET:
Private Ellipse3Pen As New Pen(Color.DimGray, 2.0!)
is much easier to read than
Private Ellipse3Pen As System.Drawing.Pen = New System.Drawing.Pen(System.Drawing.Color.DimGray, 2.0!)
Well here is the code I used:
VB.NET:
Imports System.Drawing
Imports System.Drawing.Drawing2D

Public Class Form1
	Private bcolor As Color = Color.Green
	Private stroke3 As New Pen(Color.FromArgb(0, 255, 255, 255), 1.0!)
	Private Ellipse3Brush As New SolidBrush(Color.FromArgb(255, 255, 255, 255))
	Private Ellipse3Pen As New Pen(Color.DimGray, 2.0!)
	Protected Ellipse3 As New Rectangle(0, 1, Me.Width - 6, Me.Height)
	Protected Ellipse1 As New Rectangle(3, 4, Me.Width * 0.98, Me.Height * 0.93)
	Private Ellipse1Brush As New LinearGradientBrush(New Rectangle(0, 0, Me.Width * 0.98, Me.Height * 0.93), Color.FromArgb(215, 255, 0, 0), Color.FromArgb(255, 0, 0, 0), LinearGradientMode.Vertical)
	Protected Text3 As String = "TESTING"
	Private Text3Format As New StringFormat(StringFormat.GenericTypographic)
	Private Text3Font As New Font("DS-Digital", 48.0!, CType(1, FontStyle), GraphicsUnit.Point, 1)
	Private Text3Rect As New RectangleF(0, Me.ClientSize.Height - 57, Me.ClientSize.Width + 3, Me.ClientSize.Height - 57)
	Private Text3Brush As New LinearGradientBrush(Me.ClientRectangle, bcolor, Color.FromArgb(110, 255, 255, 255), LinearGradientMode.Vertical)
	Private Ellipse2Brush As New LinearGradientBrush(New Rectangle(42, 3, 212, 83), Color.FromArgb(255, 255, 255, 255), Color.FromArgb(0, 255, 255, 255), LinearGradientMode.Vertical)
	Protected Ellipse2 As New Rectangle(42, 3, Me.Width * 0.55, Me.Height * 0.47)
	Private Ellipse5Brush As New SolidBrush(Color.FromArgb(0, 255, 255, 255))	'problem: This you will never see
	Private Ellipse5Pen As New Pen(Color.Black, 2.0!)
	Protected Ellipse5 As New Rectangle(1, 0, Me.Width - 6, Me.Height)

	Private Sub InitializeGraphics()
		Ellipse1 = New Rectangle(3, 4, Me.Width * 0.98, Me.Height * 0.93)
		Me.Text3Format.Alignment = StringAlignment.Center
		'Me.BackColor = Color.Transparent
		Text3Rect = New RectangleF(0, Me.ClientSize.Height / 2 - 24, Me.ClientSize.Width + 3, Me.ClientSize.Height - 57)
		Ellipse3 = New Rectangle(0, 1, Me.Width - 6, Me.Height)
		Ellipse2 = New Rectangle(42, 3, Me.Width - 85, Me.Height - 72)
		Ellipse5 = New Rectangle(1, 0, Me.Width - 6, Me.Height)
	End Sub

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		InitializeGraphics()
		Dim g As Graphics = Me.CreateGraphics()
		'g.FillEllipse(Ellipse1Brush, Ellipse1)
		'g.FillEllipse(Ellipse2Brush, Ellipse2)
		'g.DrawEllipse(Ellipse3Pen, Ellipse3)
		'g.FillEllipse(Ellipse5Brush, Ellipse5)
		'g.DrawEllipse(Ellipse5Pen, Ellipse5)
	End Sub
End Class
Hope this helps.
 
Paszt, thanks, I'll try changing some of the values to percentages. Wayne, you can actually see that ellipse, because it's drawn in black. Just the inside is transparent.
 
Back
Top