Pages

Tuesday 12 March 2013

ViewBox control in WPF

ViewBox control in WPF Window Based

The Viewbox element automatically scales its content to fill the space available.the Viewbox control, which expands a control to fill the available space (while keeping the aspect ratio) but the control can assume absolute size. The view box could only have one child; else if a view box contains more than one child, an argument exception will be thrown. Viewbox and Viewport are the properties of ImageBursh, DrawingBrush, TileBrush and VisualBrush elements in Windows Presentation Foundation. With these two attributes we can crop images and shapes.

Example of  ViewBox Control In WPF 
 
Real Image:


Figure 2: See the image after selecting an area in ViewBox Control.


XAML Code:

<Window x:Class="WpfApplication7.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="370" Width="417">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="400" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="400" />
        </Grid.ColumnDefinitions>
        <Rectangle Grid.Row="0" Grid.Column="0" Stretch="Uniform"  >
            <Rectangle.Fill>
                <!-- Here By passing the four value in ViewBox I will select a area from Real Image-->
                <ImageBrush ImageSource="D:\Documents and Settings\All Users\Documents\My Pictures
\Sample Pictures\Sunset.jpg" Viewbox="0.2,0.3,0.1,0.5"   />
            </Rectangle.Fill>
        </Rectangle>
    </Grid>
</Window> 
 
After set the property Stretch="Uniform" of image we will get:

No comments:

Post a Comment