Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XAML Performance: Set alpha channel of a brush vs. Opacity of FrameworkElement

For a given XAML layout that requires a semi-transparent white (to provide a glass-like effect in this case), which method would be preferable to set the transparency and why?:

  1. Set the alpha channel of the brush. E.g. #12FFFFFF
  2. Set the opacity of the FrameworkElement itself, leaving the brush as 100% white.

Also, is the answer different between WPF (4.0) and Silverlight?

like image 629
Neil Barnwell Avatar asked Oct 10 '11 09:10

Neil Barnwell


1 Answers

Straight from MSDN:

Opacity on Brushes versus Opacity on Elements

When you use a Brush to set the Fill or Stroke of an element, it is better to set the Brush.Opacity value rather than the setting the element's Opacity property. Modifying an element's Opacity property can cause WPF to create a temporary surface.

But regardless, note that you're still causing the rendering pipeline to do more work. Opacity implies it must combine pixels from multiple sources in order to come up with the final RGB value. Thus, if it's at all possible to remove the transparency altogether and instead simulate it with an opaque brush, you should. It sounds like that may not be possible in your case, but it often can be.

like image 81
Kent Boogaart Avatar answered Nov 14 '22 22:11

Kent Boogaart