Pokechu22
Pokechu22

Reputation: 5046

What is the proper way to create a brush from a color?

What is the proper way to create a brush from a color? I've tried Brush b = new Brush(color); but Brush but that isn't allowed. I can get existing colors using Brushes, but that doesn't have a way to create a specific brush. I'm using my to fill a rectangle with solid color.

The current code I have involves creating a Pen and then taking its Brush, but that doesn't seem like the right way:

Brush b = new Pen(color).Brush;

What is the correct way I should go about doing this?

Upvotes: 1

Views: 1306

Answers (3)

Jon Hanna
Jon Hanna

Reputation: 113342

The two current answers cover the two likely possibilities; if you're using GDI+ you want new SolidBrush(color) and if you're using WPF you want new SolidColorBrush(color).

The reason is that with either, Brush is an abstract class that covers a range of possible brushes that could tile a bitmap or apply a gradient or otherwise paint with something other than just a single colour.

Upvotes: 2

Daniel A. White
Daniel A. White

Reputation: 190996

Try creating a new SolidBrush for GDI+:

 SolidBrush mySolidBrush = new SolidBrush(color);

Upvotes: 2

Athari
Athari

Reputation: 34293

You need to create SolidColorBrush:

Brush b = new SolidColorBrush(color);

Upvotes: 1

Related Questions