Silverlight Brass Tacks

Bill Reiss' Silverlight Ramblings
My upcoming Silverlight book for beginners Hello! Silverlight 2 with Dave Campbell, available online now!



Pages

Recent posts

Navigation

Archive

Blogroll

Tampa Divorce Lawyer

North of Tampa in Lutz, Florida. A Tampa Divorce Lawyer focusing on family, divorce, and real estate law.

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

Viewbox in the Silverlight Toolkit

It may be one of the simpler controls in the new Silverlight Toolkit http://www.codeplex.com/silverlight but it’s one that I have to keep writing myself so I’m happy to see the Viewbox in the Silverlight Toolkit. So what’s a Viewbox? The Viewbox has been available in WPF, and it takes one child element and automatically stretches or scales it to fit the size of the Viewbox.

Consider the following Page.xaml:

<UserControl x:Class="ViewboxSample.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:scp="clr-namespace:Microsoft.Windows.Controls;assembly=Microsoft.Windows.Controls">
    <Grid x:Name="LayoutRoot" Background="Black">
        <scp:Viewbox Stretch="Uniform"> 
            <Image Source="dog.jpg"/>
        </scp:Viewbox> 
    </Grid>
</UserControl>

The Viewbox is set to Stretch=”Uniform”, so the image is scaled to fit the viewbox, taking as much space as possible while still showing the entire image and preserving the aspect ratio.

uniform

This isn’t super interesting since a grid cell can do this. However, the Viewbox has some other options. If you use UniformToFill for Stretch, the aspect ratio is still preserved, but there is no letterboxing. This is sometimes called “Zoom mode” on an HDTV widescreen when displating standard definition content:

<Grid x:Name="LayoutRoot" Background="Black">
    <scp:Viewbox Stretch="UniformToFill" HorizontalAlignment="Center" VerticalAlignment="Center"> 
        <Image Source="dog.jpg"/>
    </scp:Viewbox> 
</Grid>

This gives the following effect:

uniformtofill

You can also use Stretch=”Fill”, which fills the entire space but does not preserve the aspect ratio, so things can get stretched.

fill

Now where this gets more interesting is if the child of the Viewbox is a Panel of some kind. Let’s wrap the image in a Grid and add a TextBlock as well.

<Grid x:Name="LayoutRoot" Background="Black">
    <scp:Viewbox Stretch="Uniform" HorizontalAlignment="Center" VerticalAlignment="Center"> 
        <Grid Width="640" Height="480">
            <Image Source="dog.jpg"/>
            <TextBlock Text="Our Puppy" Margin="10" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="30" Foreground="White"/>
        </Grid>
    </scp:Viewbox> 
</Grid>

This produces the following result:

gridwithscaling

This key point here is to notice that the text scales along with the image. So if you want to scale your entire application to any size, for example to go into full screen mode, you can use the Viewbox to do this easily.

Posted: Oct 28 2008, 08:49 by Bill Reiss | Comments (3) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under:

Related posts

Comments

jeff.wilcox.name said:

pingbackPingback from jeff.wilcox.name

Sorry boss, I just released the new Silverlight Controls source code to the world. - Jeff Wilcox

# October 28 2008, 11:29

geekswithblogs.net said:

pingbackPingback from geekswithblogs.net

Silverlight Cream for October 29, 2008 -- #411

# October 29 2008, 07:14

mgalinks.wordpress.com said:

pingbackPingback from mgalinks.wordpress.com

2008 October 30 - Links for today « My (almost) Daily Links

# October 29 2008, 23:17

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

said:

# January 05 2009, 12:52