Any WPF control can display an image. Example program FormImage, which is available for download on the book’s website, uses the following XAML code to fill a Grid control’s background:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
Title="FormImage" Height="300" Width="300">
<Window.Resources>
<ImageBrush ImageSource="smile.bmp" x:Key="brSmile" />
</Window.Resources>
<Grid Background="{StaticResource brSmile}">
</Grid>
</Window>
Although a Grid control can display an image or other graphic, its real purpose is to arrange other controls. The following table describes controls whose main purpose is to present graphics and media.
CONTROL | PURPOSE |
Ellipse | Displays an ellipse. |
Image | Displays a bitmap image, for example, from a .bmp, .jpg, or .png file. Can optionally stretch the image with or without distortion. |
Line | Draws a line segment. |
MediaElement | Presents audio and video. To let you control the media, it provides Play, Pause, and Stop methods, and Volume and SpeedRatio properties. (See example program UseMediaElement, available for download on the book’s website.) |
Path | Draws a series of drawing instructions. |
Polygon | Draws a closed polygon. |
Polyline | Draws a series of connected line segments. |
Rectangle | Draws a rectangle, optionally with rounded corners. |
The shape drawing objects (Ellipse, Line, Path, Polygon, Polyline, and Rectangle) all provide Stroke, StrokeThickness, and Fill properties to let you control their appearance. Although these controls are primarily intended to draw simple (or not so simple) shapes, like any other control they provide a full assortment of events. For example, they provide an IsMouseOver property and a MouseUp event that you can use to make these objects behave like simple buttons.
Example program DrawingShapes, which is available for download on the book’s website, demonstrates several of these shape controls. Program EllipseClick, which is also available for download, uses triggers to change the color of an Ellipse when the mouse is over it, and displays a message when you click the Ellipse.