Vectorlight News

  • Chat App Converted to HTML and JQuery
    Sep 08, 2011

    Converted from Silverlight to HTML and Javascript/JQuery is the Vectorlight Chat App. Login using your Vectorlight password to chat using your username and avatar.

  • HTML5 iPhone,Android Big Guns Tower Defense
    Jul 02, 2011

    Big Guns has made the leap from Windows Phone 7 (XNA) to HTML5 so you can now play it on your iPhone, Android and other HTML5 compatible devices.

  • HTML5 Games - Word Poppers and Batty
    Jun 04, 2011

    As the take-up of HTML5 quickens (74% of users currently have a browser capable of HTML5 Canvas) we present two more games for both your browser and mobile.

  • Big Guns Tower Defense on Windows Phone 7
    May 06, 2011

    Coming soon to Windows Phone 7 is an XNA port of the popular Vectorlight tower defense game Super Tower Defense. Whilst retaining many of the graphical and gameplay features of the original Silverlight game.

  • Wakacube WP7 Update
    Apr 26, 2011

    Released to the Windows Phone 7 marketplace today is Version 1.1 of Wakacube the 3D physics game of skill. Included in the update are more levels (30 in total) and new mode Wakatime which generates random crate structures to keep players entertained long after the levels have been completed.

  • Home Page News

Silverlight 3 Writable Bitmaps

A very useful feature of Silverlight 3 is its ability to render any UI element to a Bitmap.  This capability is already used internally by Silverlight 3 to cache rendered controls but the possibilities extend much further than performance improvements.  In games programming for example if you need to create a reflection in a scene in real-time, this is now an easy task using the WritableBitmap class.


To start with we have a grid containing four cells.  The top-left cell is rendered as normal using a Button, TextBlock, TextBox and an Image that can be rotated.  The three remaining grid cells contain ContentControl elements with rotations applied to give the impression of reflection, the content property of these is set at runtime:

<UserControl x:Class="WritableBitmap.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="400" Height="300">
    <Canvas x:Name="LayoutRoot">
        <Grid Canvas.Left="100" Canvas.Top="10" Background="White">
            <Grid.Resources>
                <Storyboard x:Name="RotateYStoryBoard">
                    <DoubleAnimation Storyboard.TargetName="rotation" Storyboard.TargetProperty="RotationY" From="0.0" To="360.0" Duration="0:0:10" RepeatBehavior="Forever" />
                </Storyboard>
            </Grid.Resources>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="200" />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>
            <Grid x:Name="scene" Grid.Row="0" Grid.Column="0" Background="White">
                <Border CornerRadius="4" BorderBrush="#888888" BorderThickness="2">
                    <StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                        <Button Content="Rotate Image" Width="100" Height="30" Margin="2" Click="Button_Click" />
                        <TextBlock Text="Some Text!" FontSize="16" Margin="2">
                    <TextBlock.Effect>
                        <DropShadowEffect ShadowDepth="4" />
                    </TextBlock.Effect>
                        </TextBlock>
                        <TextBox Width="100" Height="30" FontSize="18" Margin="2" />
                        <Image Source="Sydney.JPG" Width="100" Margin="2">
                            <Image.Projection>
                                <PlaneProjection x:Name="rotation" />
                            </Image.Projection>
                        </Image>
                    </StackPanel>
                </Border>
            </Grid>
            <Grid Grid.Row="0" Grid.Column="1">
                <Grid.Projection>
                    <PlaneProjection RotationY="180" />
                </Grid.Projection>
                <ContentControl x:Name="reflection1" Opacity="0.6" />
                <Rectangle Fill="#20ff0000" />
            </Grid>
            <Grid Grid.Row="1" Grid.Column="0">
                <Grid.Projection>
                    <PlaneProjection RotationX="180" />
                </Grid.Projection>
                <ContentControl x:Name="reflection2" Opacity="0.6" />
                <Rectangle Fill="#2000ff00" />
            </Grid>
            <Grid Grid.Row="1" Grid.Column="1">
                <Grid.Projection>
                    <PlaneProjection RotationX="180" RotationY="180" />
                </Grid.Projection>
                <ContentControl x:Name="reflection3" />
                <Rectangle Fill="#200000ff" />
            </Grid>
        </Grid>
    </Canvas>
</UserControl>


The C# simply contains the Click event to handle the image rotation, the interesting part is the use of the WritableImage class to render the UI Element scene to a static bitmap:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace WritableBitmap
{
    public partial class MainPage : UserControl
    {
        private DispatcherTimer _timer = new DispatcherTimer();
        private WriteableBitmap _reflectedImage = new WriteableBitmap(200, 100, PixelFormats.Bgr32);

        public MainPage()
        {
            InitializeComponent();

            _timer.Interval = new TimeSpan(0, 0, 0, 0, 20);
            _timer.Tick += new EventHandler(Tick);
            _timer.Start();
        }

        protected void Tick(object sender, EventArgs e)
        {
            _reflectedImage = new WriteableBitmap((int)scene.RenderSize.Width, (int)scene.RenderSize.Height, PixelFormats.Bgr32);

            _reflectedImage.Render(scene, new TranslateTransform());

            reflection1.Content = new Image() { Source = _reflectedImage };
            reflection2.Content = new Image() { Source = _reflectedImage };
            reflection3.Content = new Image() { Source = _reflectedImage };
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            RotateYStoryBoard.Begin();
        }
    }
}


We use a DispatcherTimer that frequently calls the Tick() method, and in this method we create our WritableBitmap instance and call its Render method which simply converts any UI Element into a bitmap.  We then set the source of our three reflected ContentControl elements to this bitmap.

Rendering UI Elements to bitmaps is a simple but powerful addition to Silverlight and opens up a host of new applications such as graphic packages and as already mentioned special effects in games.

Your Comments

No comments found.

 

Post your Comments

Rate this: 

1 Star 2 Star 3 Star 4 Star 5 Star
15 Ratings / 3.2 Average

Tweets

Silverlight Controls

  • Rich TextBox

    Create and edit rich content with this slick and expandable Rich TextBox...

  • TreeView

    This easy to use TreeView comes with drag and drop, sorting, searching and much more...

  • Context Menu

    You too can have cool popup context menus in your Silverlight applications...

  • Resizable Dialog

    Draggable and resizable popup dialogs are what serious Silverlight developers need...

  • Spell Checker

    Real-time spell checking in Silverlight? We did it first here...