Click here to Skip to main content
15,881,172 members
Articles / Web Development / ASP.NET

ASP.NET WPF Image Handler

Rate me:
Please Sign up or sign in to vote.
3.25/5 (4 votes)
5 Jun 2008CPOL2 min read 39.8K   415   15   5
An ASP.NET handler to apply WPF XAML templates on images.

Image 1

Introduction

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".

Background

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():

VB
Dim thread As System.Threading.Thread = _
  New System.Threading.Thread(_
  New System.Threading.ThreadStart(AddressOf GimmeMore))
thread.SetApartmentState(System.Threading.ApartmentState.STA)
thread.IsBackground = True
thread.Start()
thread.Join()

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

The second stage is in the sub GimmeMore():

VB
Dim sr As New System.IO.StreamReader(_
  CurrentContext.Request.PhysicalApplicationPath + XamlDoc + ".xaml")
Dim xaml As String = sr.ReadToEnd()
sr.Close()
Dim ms As New System.IO.MemoryStream(xaml.Length)
Dim sw As New System.IO.StreamWriter(ms)
sw.Write(xaml)
sw.Flush()
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), _
                System.Windows.Controls.Page)
'Dim ib As ImageBrush = MyImage.FindName("img")
'ib.ImageSource = New BitmapImage(New Uri(_
  CurrentContext.Request.PhysicalApplicationPath + "sunset.jpg"))
ms.Dispose()
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.

Notes:

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.

License

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


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

Comments and Discussions

 
QuestionAny luck? Pin
Roasted Amoeba3-Jul-08 23:22
Roasted Amoeba3-Jul-08 23:22 
AnswerRe: Any luck? Pin
Claudio Pizzillo4-Jul-08 0:40
Claudio Pizzillo4-Jul-08 0:40 
GeneralRe: Any luck? - SOLUTION Pin
Roasted Amoeba4-Jul-08 2:12
Roasted Amoeba4-Jul-08 2:12 
GeneralRe: Any luck? - SOLUTION Pin
Claudio Pizzillo6-Jul-08 22:28
Claudio Pizzillo6-Jul-08 22:28 
GeneralRe: Any luck? Pin
havana712-Nov-09 11:51
havana712-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.