HomeSilverlight Complete Guide to Gradients Using LinearGr...
Complete Guide to Gradients Using LinearGradientBrush in Silverlight 4.0
This is my second article in a series focusing on the basics of painting shapes in a Silverlight application using both declarative and code-behind methods. In this article, we will cover gradient painting in depth just by using the LinearGradientBrush in Silverlight.
If you are new to brushes in Silverlight, please refer my previous article which covers basics of Silverlight painting along with SolidColorBrush and LinearGradientBrush. You can find it here.
The entire source code for this article is available in the form of a free downloadable zip file. The solution was developed using Microsoft Visual Studio 2010 Ultimate Edition with Microsoft Silverlight 4.0 RC2 on Windows 7 Ultimate edition. I didn’t really test it in any other environment. I request that you post in the discussion area if you have any problems with execution.
Introducing Gradient Axis with the LinearGradientBrush
As we covered “GradientStop” in my previous article, let's focus on something more. By default, the above applies diagonal gradient painting (from top-left to bottom-right). In other words, the entire gradient pattern, which includes all defined GradientStops, gets painted from top-left to bottom-right. You can always control this axis using the “StartPoint” and “EndPoint” attributes of “LinearGradientBrush”.
Note: The last Offset always ends with EndPoint.
Let us rewrite the above code using “StartPoint” and “EndPoint”:
The above gives you output identical to the previous, but you have to switch your understanding from GradientStop to “StartPoint and EndPoint” as shown below:
Let us achieve the above using code-behind:
Private Sub MainPage_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
'start: specifying StartPoint and EndPoint (from code-behind)
Dim brBRYGrad = New LinearGradientBrush()
brBRYGrad.StartPoint = New Point(0, 0)
brBRYGrad.EndPoint = New Point(1, 1)
Dim GradBlue As New GradientStop()
GradBlue.Color = ColorHelper.Blue
GradBlue.Offset = 0
brBRYGrad.GradientStops.Add(GradBlue)
Dim GradRed As New GradientStop()
GradRed.Color = ColorHelper.Red
GradRed.Offset = 0.5
brBRYGrad.GradientStops.Add(GradRed)
Dim GradYellow As New GradientStop()
GradYellow.Color = ColorHelper.Yellow
GradYellow.Offset = 1
brBRYGrad.GradientStops.Add(GradYellow)
Me.Rectangle1.Fill = brBRYGrad
'end: specifying StartPoint and EndPoint (from code-behind)
In the previous section, we saw how to specify the Gradient Axis for a LinearGradientBrush. In this section, we will look into different combinations to use with the gradient axis to achieve different gradient effects.
Top-Down Gradient
The following is the code you'd use to achieve a top-down gradient:
We did not examine transparent effects using gradients. A transparent effect in a gradient pattern can be easily achieved by not specifying any color for gradient stops.
The important thing to note in the previous code is the second GradientStop, defined with no color. As the second GradientStop has no color, the first GradientStop (colored red) would naturally blend with the transparency, and finally, would let the objects behind the current shape be shown.
You can also have more GradientStops while making the middle GradientStop transparent, as shown below:
There is one more trick to achieving gradients with transparency. You can specify a GradientStop with a color along with a bit of transparency. I already explained the “transparency HEX bit” in my previous article; please refer to that if you find this difficult to understand.
To specify “Opacity” in the code-behind, you can directly access “obj.Opacity” property (where “obj” would be an instance of type “LinearGraidentBrush” class).
You can achieve more effects using the “SpreadMethod” attribute available as part of LinearGradientBrush. This method offers three options: Pad, Repeat, and Reflect.
The “Pad” option makes the last GradientStop color continue until the end. The following is an example:
In all of the above examples, we specified the end point using logical values between 0 and 1 (0 being the beginning and 1 being the end of the shape's area). This is called “RelativeToBoundingBox” (which is the default) and is set via the “MappingMode” of LinearGradientBrush.
You have one more option in “MappingMode,” called “Absolute.” This gives us the flexibility to specify the end point in specific pixels.
Observe the attributes EndPoint and MappingMode in the above code. The same can be achieved using code-behind, as shown below:
Private Sub MainPage_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
'start: Mapping Mode - Absolute (code-behind)
Dim brRYGrad = New LinearGradientBrush()
brRYGrad.StartPoint = New Point(0, 0)
brRYGrad.EndPoint = New Point(110, 50)
brRYGrad.MappingMode = BrushMappingMode.Absolute
Dim GradRed As New GradientStop()
GradRed.Color = ColorHelper.Red
GradRed.Offset = 0
brRYGrad.GradientStops.Add(GradRed)
Dim GradYellow As New GradientStop()
GradYellow.Color = ColorHelper.Yellow
GradYellow.Offset = 1
brRYGrad.GradientStops.Add(GradYellow)
Me.Rectangle1.Fill = brRYGrad
'end: Mapping Mode - Absolute (code-behind)
End Sub
In my upcoming articles, we will see more examples of Silverlight 4.0 development. I hope you enjoyed the article and any suggestions, bugs, errors, enhancements etc. are highly appreciated at http://jagchat.spaces.live.com