Comment out this:
// base.BackgroundImage = this._BGImage;
It's causing it to recursively paint itself. You should never set properties in a paint routine.
Furthermore, you aren't accomplishing anything by overriding the BackgroundImage, and if you do override the property, that should be the only place where you assign the base.BackgroundImage value. Consider removing that.
I reworked your code to this:
protected override void OnPaintBackground(PaintEventArgs e) {
if (this.DrawStyle == DrawStyle.Picture) {
base.OnPaintBackground(e);
}
}
protected override void OnPaint(PaintEventArgs e) {
Rectangle rc = this.ClientRectangle;
if (this.DrawStyle == DrawStyle.Solid) {
using (SolidBrush brush = new SolidBrush(this.PrimaryColor))
e.Graphics.FillRectangle(brush, rc);
} else if (this.DrawStyle == DrawStyle.Gradient) {
using (LinearGradientBrush brush =
new LinearGradientBrush(
rc, this.PrimaryColor, this.SecondaryColor, this.Angle))
e.Graphics.FillRectangle(brush, rc);
}
base.OnPaint(e);
}
Make sure to add this.DoubleBuffered = true;
and this.ResizeRedraw = true;
in your panel's constructor to avoid unnecessary flicker.