SpriteHand
Module Border
  Silverlight 3: HitTest with WriteableBitmap in Silverlight 3
Module Border
Location: BlogsAndy's Blog    
Posted by: host 3/18/2009 1:26 PM


UPDATE 7/10/2009: See THIS BLOG POST regarding Silverlight 3 RTW, we can now read the Pixels after calling Render()!

 

Ok, the title of this post is a tease. Read on and find out why!

A new feature in Silverlight 3 Beta 1 is the WriteableBitmap class, which allows pixel-level image manipulation at runtime. It turns out you can also do pixel-level image manipulation in Silverlight 2, using methods such as Joe Stegman’s Editable Image samples.  But there are some new features to WriteableBitmap which weren’t possible using these methods.

First, you can use WriteableBitmap to render a portion of the Visual Tree to a bitmap.

WriteableBitmap wb = new WriteableBitmap(800, 600, PixelFormats.Bgr32);

wb.Render(canvas1, new TranslateTransform());

 

…you can then assign the WriteableBitmap to the Source value of an Image like so:

img.Source = wb;


HitTest a Raster (Bitmap) Image in Silverlight 3

In a previous blog post, I showed how you could use the VisualTreeHelper.FindElementsInHostCoordinates to do a HitTest on a XAML element in Silverlight 2. But in Silverlight 2, there was no way of doing a HitTest on a Raster (Bitmap) image, which makes it difficult to do collision detection on Image elements.

In Silverlight 3, WriteableBitmap will “eventually” make this possible because you can access Pixels of the WriteableBitmap at runtime! Unfortunately, in Beta 1 of Silverlight 3, this functionality is disabled in most cases L The reason being that there needs to be security put into place to ensure that a hacker could not request 3rd party images through a Silverlight app and then access secure information.

So say you have an image which you plant on a Canvas like so:

        <Image Source="/fish.png" x:Name="imgFish" />


And then you can set up a WriteableBitmap as follows:

        BitmapSource bitmapSource = imgFish.Source as BitmapSource;

        WriteableBitmap wb = new WriteableBitmap(bitmapSource);

 

… In Silverlight 3 BETA 1, this will actually bring back the correct PixelWidth and PixelHeight into bmPixels1,but when you try to access the pixel array, you will get a Null Reference Exception:

int x = wb[x];   // gives a Null Ref

Also, after calling the Render method on WriteableBitmap (such as in the previous example to render the visual tree), the pixel array will be unavailable and will give a Null Reference Exception.

Oh well… Silverlight 3 _is_ a Beta at this point, so we can only hope this functionality will be available by  RTM!

 

Permalink |  Trackback

Comments (1)   Add Comment
Links to Silverlight 3.0 resources    By TrackBack on 3/19/2009 3:26 AM
Links to Silverlight 3.0 resources
# Silverlight Playground


Title:
Comment:
Add Comment   Cancel 
Module Border Module Border
Module Border
  Subscribe
Module Border
RSS   Twitter
Module Border Module Border
Module Border
  Diversions
Module Border

TALKING RAGDOLL
This Windows Phone 7 App was created using Silverlight, the  Physics Helper Library,  and the Farseer Physics Engine. It gets interesting when you import your friends photos and have your way with them!

MORE INFO



DROPPYPOP
This Windows Phone 7 game was created using Silverlight, the  Physics Helper Library,  and the Farseer Physics Engine.
DEMO

MORE INFO



BOSS LAUNCH
This physics game won first place in the Server Quest Contest. Created using Silverlight 2, the Physics Helper Library,  and the Farseer Physics Engine.
PLAY IT

MORE INFO



DESTROY ALL INVADERS
A scrolling shooter game where the objective is to destroy the invading UFO's flying over a neighborhood of your choosing. Imagery provided by Microsoft Virtual Earth. Created using Silverlight 2.
PLAY IT

INFO AND CODE



PHYSICS HELPER DEMOS
These demos were created for the Physics Helper Library, which makes it easy to create physics games and simulations using Expression Blend, Silverlight, and the Farseer Physics Engine.
PLAY IT

INFO AND CODE



HOOK SHOT
This little basketball game took first place in the TeamZoneSports Silverlight Contest. Created using Silverlight 2 and the Farseer Physics engine.
PLAY IT

MORE INFO



SORT THE FOOBARS
A game where you need to sort the good foobars from the bad ones. Created using Silverlight 2 and the Farseer Physics engine.
PLAY IT

MORE INFO



POLYGON PHYSICS DEMO
A demo showing polygon physics where the user draws physics objects with the mouse. Created using Silverlight 2 and the Farseer Physics engine.
PLAY IT

MORE INFO



SILVERLIGHT ROCKS!
Destroy the asteroids before they destroy your ship! Created using Silverlight 2.
PLAY IT

INFO AND CODE



FISH GAME
A simple game of harpoon-the-fish. Written using the AJAX Sprite Toolkit.
PLAY IT

INFO AND CODE

Module Border Module Border
Module Border
  Search_Blog
Module Border
Module Border Module Border
Module Border
  Blog_Archive
Module Border
Module Border Module Border
Copyright (c) 2012 andy.beaulieu.com - Login