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".
WPF works in a STA thread, ASP.NET in MTA, this is a limit.
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:
w : width h : height xaml : name on the xaml file without extension 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).
There are 3 stages on the handler:
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 ....
Marco Zhou, MSFT and MSDN forum moderator.
Cristian Civera, MVP.
| You must Sign In to use this message board. | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||