Skip to main content
Email Password   helpLost your password?

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 .

Remenber: the root element of the xaml page MUST BE a "Page".

Background

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

Using the code

The handler works on IIS6, on IIS7 i have some problems to render 3d objects (i'm working on it).

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

  1. w : width
  2. h : height
  3. xaml : name on the xaml file without extension
  4. enc : choose the encoder, available options: png, jpg, tif, bmp, wmp, gif

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

Modifing the handler, you can also pass the image name via url (look for the commented code).

How it works

There are 3 stages on the handler:

  1. Set the thread and start it
  2. Load the xaml file with xamlreader
  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 start 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 dinamically 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 i use Dispatcher.Invoke? Is the only tested method to capture the binding in the rendered xaml.

On IIS7 the handler works, but ONLY the Viewport3D are not rendered (no 3 effects). Searching ....

History and Credits

Marco Zhou, MSFT and MSDN forum moderator.

Cristian Civera, MVP.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralAny luck? Pin
Roasted Amoeba
0:22 4 Jul '08  
GeneralRe: Any luck? Pin
Claudio Pizzillo
1:40 4 Jul '08  
GeneralRe: Any luck? - SOLUTION Pin
Roasted Amoeba
3:12 4 Jul '08  
GeneralRe: Any luck? - SOLUTION Pin
Claudio Pizzillo
23:28 6 Jul '08  
GeneralRe: Any luck? Pin
havana7
12:51 12 Nov '09  


Last Updated 5 Jun 2008 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009