Click here to Skip to main content
6,305,776 members and growing! (17,477 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Advanced License: The Code Project Open License (CPOL)

ASP.NET WPF image handler

By Claudio Pizzillo

An ASP.NET handler to apply WPF XAML templates on images.
VB (VB 7.x, VB 8.0, VB 9.0, VB 6), Windows, .NET (.NET 3.0, .NET 3.5), ASP.NET, XAML, WPF, Dev
Posted:5 Jun 2008
Views:9,845
Bookmarked:10 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
4 votes for this article.
Popularity: 1.96 Rating: 3.25 out of 5

1
1 vote, 25.0%
2
1 vote, 25.0%
3

4
2 votes, 50.0%
5

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.

License

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

About the Author

Claudio Pizzillo


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

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
GeneralAny luck? PinmemberRoasted Amoeba0:22 4 Jul '08  
GeneralRe: Any luck? PinmemberClaudio Pizzillo1:40 4 Jul '08  
GeneralRe: Any luck? - SOLUTION PinmemberRoasted Amoeba3:12 4 Jul '08  
GeneralRe: Any luck? - SOLUTION PinmemberClaudio Pizzillo23:28 6 Jul '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 5 Jun 2008
Editor:
Copyright 2008 by Claudio Pizzillo
Everything else Copyright © CodeProject, 1999-2009
Web13 | Advertise on the Code Project