WPF Brushes: Solid, Gradient, Image, and More!

If you’ve ever tried to set a foreground or background property for an object in WPF, you’ll notice that it takes an instance of the abstract type Brush. This cryptic class cannot be instantiated; so how do we use it?

The best resource is the MSDN overview of WPF brushes. It lists various sub-classes of brushes (visually!):

I won’t go into too many details (since the MSDN page does a great job of that); suffice to say that the main brushes you need to know about are:

  • SolidColorBrush: For a single, solid colour. Use new SolidColorBrush(Color.FromRGB(r, g, b)).
  • LinearGradientBrush: For a two-colour gradient. Use new LinearGradientBrush(Color, Color, angle) (where angle = 0 for horizontal, 90 for vertical).
  • ImageBrush: Uses an image as a (repeatable) pattern. Use new ImageBrush(ImageSource) (where ImageSource is probably a BitmapSource or DrawingImage).

Best of all is that, like the rest of WPF, these are vector-style brushes designed to scale up or down with your application control size. So use them with impunity, but test, test, test!

About Ashiq Alibhai, PMP

Ashiq has been coding C# since 2005. A desktop, web, and RIA application developer, he's touched ASP.NET MVC, ActiveRecord, Silverlight, NUnit, and all kinds of exciting .NET technologies. He started C# City in order to accelerate his .NET learning.
This entry was posted in Core .NET, WPF and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *