SolidBrush Class
Represents a brush with a single, solid color. This class is used to fill shapes, text, and other graphical elements with a uniform color.
Namespace
System.Drawing
Inheritance
System.Object → System.Drawing.Brush → System.Drawing.SolidBrush
Syntax
Class Declaration
public sealed class SolidBrush : Brush
Constructors
SolidBrush(Color color)- Initializes a new instance of the
SolidBrushclass with the specifiedColor. SolidBrush(KnownColor color)- Initializes a new instance of the
SolidBrushclass with the specifiedKnownColorenumeration value.
Description
The SolidBrush class provides a simple way to apply a solid color fill to graphical objects.
It inherits from the abstract Brush class and implements the necessary methods for color filling.
This brush is efficient for solid color fills and is commonly used for drawing text, rectangles, ellipses, and other shapes.
Properties
Color
Gets or sets the Color of this SolidBrush.
Clone()
Creates an identical copy of this SolidBrush.
Methods
Dispose()
Releases the unmanaged resources used by the SolidBrush and optionally releases the managed resources.
Example Usage
C# Example
using System.Drawing;
using System.Windows.Forms;
public class MyForm : Form
{
public MyForm()
{
this.Paint += new PaintEventHandler(MyForm_Paint);
}
private void MyForm_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
// Create a solid blue brush
using (SolidBrush blueBrush = new SolidBrush(Color.Blue))
{
// Draw a filled rectangle
g.FillRectangle(blueBrush, 10, 10, 100, 50);
}
// Create a solid red brush using a KnownColor
using (SolidBrush redBrush = new SolidBrush(KnownColor.Red))
{
// Draw a filled ellipse
g.FillEllipse(redBrush, 150, 10, 80, 80);
}
// Draw some text with a solid green brush
using (SolidBrush greenBrush = new SolidBrush(Color.FromArgb(100, 0, 255, 0))) // Semi-transparent green
{
g.DrawString("Hello, SolidBrush!", this.Font, greenBrush, 10, 120);
}
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MyForm());
}
}