Wahlin on .NET

Silverlight XAML Primer 3: Working with Image Brushes

Dan shows you how to use Silverlight's brushes to create custom images.

Silverlight makes it easy to define different types of shapes within a XAML canvas and apply various colors and borders. In my last article, I showed how ellipses, rectangles and text could be defined using XAML and how background colors could be applied. But while applying standard colors to shapes is certainly useful in many situations, there will be times when you'll need more control over the look and feel of a particular shape. In this article, I'll explore one way this can be accomplished using Silverlight image brushes. Future articles will discuss other types of brushes, such as gradient and video brushes.

Working with Image Brushes
Silverlight exposes an element named ImageBrush that can be used to show images within XAML objects. The ImageBrush element is useful any time you'd like to "paint" a PNG or JPEG image into a Silverlight object. Use it whenever you'd like to show a custom image as the foreground of a TextBlock element or want to assign an image as the background of a particular object.

The code below shows an example of using the ImageBrush element with a TextBlock to add an image as the foreground of the text. Notice that the ImageBrush is defined as a child of the TextBlock.Foreground property element and that the path to the image is defined using the ImageSource attribute.

<TextBlock Name="tbImageBrush"
 Canvas.Left="36" Canvas.Top="80"
 FontFamily="Verdana" FontSize="60" FontWeight="Bold"
 Text="Text with Brush">

 <TextBlock.Foreground>
  <ImageBrush ImageSource="Images/GreenPlant.jpg" />   
 </TextBlock.Foreground>

</TextBlock>

The image added to the TextBlock in the code above automatically fills the text's foreground based upon the TextBlock's size. The ImageBrush element can also be used to define a background for a canvas (or any other XAML element that supports a background). Listing 1 shows how to provide a background image for a canvas using the ImageBrush element.

The ImageBrush element defines a Stretch attribute that can be used to control how an image is displayed (as shown in Listing 1). The Silverlight 1.0 documentation provides the following insight into the values that can be supplied to the Stretch attribute:

  • None: The content does not stretch to fill the output dimensions, and the applied brush area will come from the matching sized area of the image.
  • Uniform: The content is scaled to fit the output dimensions. However, the aspect ratio of the content is preserved. (This means that if the dimensions of the image don't match the dimensions of the target control -- the target control is wider, for example -- then extra space will show in the dimension that doesn't match. In other words, the image won't try to stretch to fit into the non-matching dimension.)
  • UniformToFill: The content is scaled so that it completely fills the output area but preserves its original aspect ratio.
  • Fill: The content is scaled to fit the output dimensions. Because the content's height and width are scaled independently, the original aspect ratio of the content might not be preserved. That is, the content might be distorted in order to completely fill the output area.

In cases where an ImageBrush uses an image that's smaller than the target element, the Stretch attribute's Fill or UniformToFill values can be used to ensure that the image takes up the entire area of the target element. Keep in mind that, in some cases, this may result in undesirable stretching of the image as its aspect ratio changes.

The placement of the image within its container can also be influenced using the AlignmentX and AlignmentY attributes. The AlignmentX attribute can be assigned values of Left, Center or Right, while the AlignmentY attribute can be assigned values of Top, Center or Bottom.

Figure 1 shows an example of the output generated by using the ImageBrush to apply a TextBlock with a foreground image and a canvas with a background image. The background image brush has its Stretch attribute assigned to a value of Fill.

In the next article, I'll explore another type of brush called the GradientBrush and explain how it can be used to apply gradients to XAML elements.

About the Author

Dan Wahlin (Microsoft MVP for ASP.NET and XML Web Services) is the founder of The Wahlin Group which specializes in .NET and SharePoint onsite, online and video training and consulting solutions. Dan also founded the XML for ASP.NET Developers Web site, which focuses on using ASP.NET, XML, AJAX, Silverlight and Web Services in Microsoft's .NET platform. He's also on the INETA Speaker's Bureau and speaks at conferences and user groups around the world. Dan has written several books on .NET including "Professional Silverlight 2 for ASP.NET Developers," "Professional ASP.NET 3.5 AJAX, ASP.NET 2.0 MVP Hacks and Tips," and "XML for ASP.NET Developers." Read Dan's blog here.

comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube