Image Brush Background set in Windows Phone 7

In this article you will learn about how to set Background using Image Brush in Windows Phone 7.
  • 3911

Image Brush

The ImageBrush object is assigned to the AlbumData object's ImageBrush property so that the appropriate image is displayed in the application. Although the remainder of the code won't be shown here, the sample project available contains everything you'll need to see how users can interact with an album once it's rendered. As you can see below, the  background is applied as an image with an ImageBrush, If you use a background image, its Build Action should be set to Resource to ensure that it is shown as soon as the first page of the application starts.

Example:

<phone:PhoneApplicationPage
    x:Class="WindowsPhoneApplication3.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True"> 
    <!--LayoutRoot is the root grid where all page content is placed-->
    <Rectangle
    Width="480"
    Height="768"
    Stroke="Black"
    StrokeThickness="4">
        <Rectangle.Fill> 
            <ImageBrush ImageSource="a.jpg" /> 
        </Rectangle.Fill> 
    </Rectangle> 
</phone:PhoneApplicationPage>

// Mainpage.xaml.vb code

Imports System.Windows.Media.Imaging

Partial Public Class MainPage
    Inherits PhoneApplicationPage
    ' Constructor
    Public Sub New()
        InitializeComponent()
    End Sub
    Public Sub CreateAnImageBrush()
        ' Create a Rectangle
        Dim blueRectangle As New Rectangle()
        blueRectangle.Height = 100
        blueRectangle.Width = 200 
        ' Create an ImageBrush
        Dim imgBrush As New ImageBrush()
        imgBrush.ImageSource = New BitmapImage(New Uri("a.jpg", UriKind.Relative))
        ' Fill rectangle with an ImageBrush
        blueRectangle.Fill = imgBrush
    End Sub
End Class 

Output Window

Background set using Image Brush in Windows Phone 7.gif

Conclusion

Hope this article help you to understand how to set Background using Image Brush in Windows Phone 7.

Categories

More Articles

© 2013 dotNetheaven. All rights reserved.