Click here to Skip to main content
13,091,108 members (55,645 online)
Click here to Skip to main content
Add your own
alternative version


15 bookmarked
Posted 5 Jun 2008

ASP.NET WPF Image Handler

, 5 Jun 2008
Rate this:
Please Sign up or sign in to vote.
An ASP.NET handler to apply WPF XAML templates on images.


This article explains the use of an ASP.NET handler to serve up images using WPF and XAML templates to manipulate images. The results are incredible, and not comparable with any commercial component.

Remember: the root element of the XAML page must be a "Page".


WPF works in an STA thread, ASP.NET in an MTA, and this is a limit.

Using the code

The handler works on IIS 6. On IIS 7, I have some problems rendering the 3D objects (I'm working on it).

To use the handler, you need to call "Xaml.ashx", passing some mandatory parameters:

  • w: width
  • h: height
  • xaml: name on the XAML file without extension
  • enc: the encoder; available options: PNG, JPG, TIF, BMP, WMP, GIF

As an example URL: "xaml.ashx?w=500&h=300&xaml=xamlshield&enc=png".

By modifying the handler, you can also pass the image name via URL (look for the commented code).

How it works

There are three stages on the handler:

  1. Set the thread and start it.
  2. Load the XAML file with a XAML reader.
  3. Capture the rendered output, encode it, and send it.

The first stage is in the sub StartThread():

Dim thread As System.Threading.Thread = _
  New System.Threading.Thread(_
  New System.Threading.ThreadStart(AddressOf GimmeMore))
thread.IsBackground = True

I'm setting the thread ApartmentState to STA and starting it.

The second stage is in the sub GimmeMore():

Dim sr As New System.IO.StreamReader(_
  CurrentContext.Request.PhysicalApplicationPath + XamlDoc + ".xaml")
Dim xaml As String = sr.ReadToEnd()
Dim ms As New System.IO.MemoryStream(xaml.Length)
Dim sw As New System.IO.StreamWriter(ms)
ms.Seek(0, System.IO.SeekOrigin.Begin)
Dim parserContext As New ParserContext()
parserContext.BaseUri = New Uri(CurrentContext.Request.PhysicalApplicationPath)
MyImage = CType(System.Windows.Markup.XamlReader.Load(ms, parserContext), _
'Dim ib As ImageBrush = MyImage.FindName("img")
'ib.ImageSource = New BitmapImage(New Uri(_
  CurrentContext.Request.PhysicalApplicationPath + "sunset.jpg"))
Dim slt As PageDelegate
slt = New PageDelegate(AddressOf Capture)
MyImage.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, _
                          slt, MyImage)

I load the XAML file and call, with Dispatcher.Invoke, the capture sub.

Note: I commented some code, you can use it to dynamically assign the image to the XAML file (in the examples, use the file xamlshield.xaml).

In the last stage (sub Capture()), I capture the rendered output with the prefered encoder.


Why do I use Dispatcher.Invoke? It is the only tested method to capture the binding in the rendered XAML.

On IIS 7, the handler works, but only the Viewport3D is not rendered (no 3D effects). Searching for a solution....

History and Credits

  • Marco Zhou, MSFT and MSDN forum moderator.
  • Cristian Civera, MVP.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Claudio Pizzillo
Software Developer (Senior) KefaOnLine di Techworld S.r.l.
Italy Italy
VB.NET, C# Developer.
Skills: VB.Net, ASP.Net, SQL 2000/2005.

You may also be interested in...

Comments and Discussions

QuestionAny luck? Pin
Roasted Amoeba3-Jul-08 23:22
memberRoasted Amoeba3-Jul-08 23:22 
AnswerRe: Any luck? Pin
Claudio Pizzillo4-Jul-08 0:40
memberClaudio Pizzillo4-Jul-08 0:40 
GeneralRe: Any luck? - SOLUTION Pin
Roasted Amoeba4-Jul-08 2:12
memberRoasted Amoeba4-Jul-08 2:12 
GeneralRe: Any luck? - SOLUTION Pin
Claudio Pizzillo6-Jul-08 22:28
memberClaudio Pizzillo6-Jul-08 22:28 
GeneralRe: Any luck? Pin
havana712-Nov-09 11:51
memberhavana712-Nov-09 11:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.170813.1 | Last Updated 5 Jun 2008
Article Copyright 2008 by Claudio Pizzillo
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid