Friday, August 07, 2009

Silverlight Nine Grid

I was recently working on a project where I ran into a need for a nice nine grid (or scale 9 grid) and realized that with some of the new additions in Silverlight 3 and Blend 3 that one could put together a pretty sweet solution based on pixel shaders and the new design-time extensibility in Blend.

Nine Grids

Just in case everyone isn’t aware of what a nine grid is, it’s a technique for splitting up an image into 9 pieces then scaling those pieces to simulate layout-aware resizing rather than uniform scaling. Nine grids are very useful because they allow graceful resizing of otherwise non-resizable images.

Normally nine grids are done using either one image duplicated, scaled and cropped into 9 parts but I realized that this could be done much more elegantly with a single shader to handle the appropriate stretching. The real big benefit of using an effect is that vector path data from tools such as Illustrator can be nine-gridded in addition to images. A pixel shader also results in a much lower framework element count which helps keep control instantiation time zippy.

Design-Time

Using Blend 3’s new design-time extensions I was also able to put together a solid editing experience complete with adorners on the design-surface- makes it quick and easy to configure.

 

Source and binaries.

Labels: , , ,

12 Comments:

Anonymous Josh Santangelo said...

Wonderful. There is an approximately 100% chance that I will use this in a future project. Does it work in WPF too?

6:23 PM  
Blogger Pete said...

Cool that it sounds useful!

I haven't done the WPF version yet but it shouldn't be too difficult. It sounds like it'd be useful so I'll see what I can do.

8:44 PM  
Blogger Sanjay Patel said...

This looks great. Where can I find the download link?

8:46 PM  
Blogger Pete Blois said...

Download link is at the bottom-
http://blois.us/Silverlight/Scale9/Scale9.zip

2:08 PM  
Anonymous Jared Bienz said...

Amazing. Especially seeing it in action on the design surface. Amazing.

10:19 AM  
Blogger Josh Santangelo said...

So how do you install and use it?

11:10 AM  
Anonymous Mark Easton said...

This is really great thanks. I tried it out with a vector background but unfortunately (and I guess as expected) you lose all the resizable vectory goodness and so in my case the result was pixelated and unusable. I tried making the original vector huge and wrapping it all in a grid with a scale transform to reduce it and this helped somewhat.

1:57 AM  
Anonymous Mark said...

You are the man!!! This is suuuper useful. Would you convert this to WPF or explain briefly how to do that? Thanx a lot.

8:21 AM  
Blogger Jonathan said...

I'm attempting to use just the ShaderEffect, without using blend (everything is loaded at runtime in my project so design-time doesn't really exist). Do you think this is going to be doable?

8:25 AM  
Anonymous Kai said...

How hard would it be to add an option to "tile" the middle pieces instead of "stretching" them?

11:18 PM  
Anonymous Cathal said...

When running in Blend I'm getting an error 'The component 'NineGrid.Design.UI.NineGridAdorner2' does not have a resource identified by the URI '/NineGrid.Design.UI;component/ninegridadorner2.xaml'

Any ideas?
Thanks

3:31 AM  
Anonymous Khaos said...

Thanks. Just getting started on a UI to replace an older pure html on. This could be a great addition to a scale and center behaviour.

4:22 PM  

Post a Comment

<< Home