WPF Skinning your Websites

UPDATE 2: Now able to create “filmstrip” images from WPF Storyboards; some basic documentation available here.

UPDATE: Now able to create animated GIFs from WPF Storyboards.

I’ve written a cool little utility that lets you skin websites with WPF / XAML.  All you need to do is define a XAML scene and specify the rectangular regions you need snapshots of.  Each is saved to its own file (as PNG, JPEG or GIF).

Once you’ve got the files, just set them as background images for appropriately positioned HTML elements 🙂

Some nice benefits are:

  • You can use any WPF content, including 3D.
  • You get edge anti-aliasing for free (see comment below).
  • You can build scenes with multiple layers; gradient fills, alpha blends etc.  If your graphic assets have transparency (PNG files for example) you can do some very nice compositing.  Your graphic designers may soon be twiddling their thumbs…
  • You can design your scene in Blend, Kaxaml, … (insert tool of choice).
  • Rounded corners!  A few CornerRadius attributes and you never need cringe at AJAX Control Toolkit’s blocky attempt again.
  • Browser independent; you can save images as PNG, JPEG or GIF.  It’s your choice whether you include transparency in them or not (IE6 can’t apply transparency in PNGs but IE7 can).  You can compose your scene using all the transparency you like, but slice the result into JPEGs or GIFs that’ll work everywhere 🙂
  • You can include data-binding expressions; a nice example of this is keeping primary site colors in a separate configuration file, giving developers a disturbing amount of control (don’t forget you can manipulate colors with OpacityMasks etc to produce varying shades).
  • Simple WPF Storyboards can be recorded as animated GIFs or “filmstrip” images (the animated frames are vertically spaced in a single image).
  • It’s a command-line utility and is happy with relative paths.  This makes it great for calling from a post-build step in Visual Studio.
  • It’s mainly in its own assembly; you could use it in other applications, or use it as a custom build tool.
  • It’s free!  Source code will be available soon 🙂

Now for the bad news:

  • 3D content isn’t anti-aliased.  This is simply because WPF 3D expects the graphics hardware to do its 3D anti-aliasing, and it only does that when it’s sent directly to the screen.  One solution is to create the RenderTargetBitmap with a much higher DPI (say 300), then downsample it later.  I’ll probably make the DPI configurable in a future update. 
  • I’ve found some properties don’t work with binding expressions (for example the color steps of a LinearGradientFill).  However the same properties work fine in Kaxaml, Blend etc.  This might just be a bug in my code; I’ll post an update if I find a solution.  As usual there are workarounds; instead of a LinearGradientFill you could use several solid fills (with binding) and apply static OpacityMasks with LinearGradientFill.  If that statement made no sense, let me know and I’ll try to describe it better 🙂 (also let me know if you have a solution).
  • Although it can create animated GIFs, currently they only play once (no repeat).  However, it’d be quite easy to add a reference to NGif to re-encode the GIF with the relevant metadata (could do it all through streams; no intermediate files needed).

Finally the goodies 🙂  You can download the utility here.  It includes some sample configuration files to play with (and shows binding to values in a separate XML file).  Extract the files to a folder and type this:

SkinBuilder SkinBuilder.xml

It’ll create around 12 image files in the current folder (including one animated GIF and a filmstrip image).  They probably won’t be much use to you directly, but serve as a good example.  Here’s pretty much what the sample page looks like:

Full

But that’s not all!  You can also get the source code right here 🙂

I’ve not created an installer yet; I could say this is so I don’t burden you with anything unnecessary, but clearly it’s just laziness 🙂

Enjoy!

6 Comments

  1. Ok, so, this is what I’m looking for, sort of. But I want to run it on a server with .aspx and be able to code it so if(iphone) then, render xaml as bitmap, else if(nosilverlight) render xaml as bitmap.
    In this way you could create a configurable xaml design without any client software other than a browser and re-render on the fly to give users a design that has curves and alpha masks and various other effects.
    What is the requirement to do this a on a standard web hosting site? .NET 3.5 ?

    Reply

  2. Trevor – It’d be pretty easy to do. The minimum requirement would be .NET 3.0. You’d probably also want to implement a simple caching mechanism to reduce the server’s workload.

    Note WPF isn’t intended as a server technology. It’s possible that’s largely a butt-cover claim though. In theory, as long as you respect WPF’s single-threaded nature (and only create and use objects on a single request/thread – no sharing) it mgiht work just fine 🙂 I’d be interested in WPF team member views on this.

    In the source I published, all the interesting stuff (really not much) is done in the CJC.XamlCapture assembly. It’s fairly self-contained; should be easy to Response.Write the generated images. Let me know if you could use some more help 🙂

    Reply

Leave a reply to MSDN Blog Postings » Using WPF to help skin your website Cancel reply