Click here to Skip to main content
11,718,289 members (85,160 online)
Click here to Skip to main content

ASP.NET WPF Image Handler

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

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

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

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)

Share

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    Rant Rant    Admin Admin   

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

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