Rendering WPF objects as graphics in ASP.NET

I am trying to display a WPF based object as PNG inside an ASP.NET image handler. We created a constructor in WPF that creates XAML-based templates. Using the XAML serializer to store templates on disk is simple. Pulling out the same XAML-based serial objects and restoring them in an ASP.NET HTTP handler proved difficult.

The XAML deserialization process must be performed on the STA thread. Creating a stream and installing it in the STA and working with deserialization and composition of images for the first time. I am creating a reconstructed XAML image that is sent back and sent to the browser. Subsequent calls to the HTTP handler cause the web server to fail: "The calling thread cannot access this object because it has a different thread."

Are there libraries that will generate an image from a simple XAML-based object?

+3
source share
3 answers

I answered this some time ago on the MSDN forums , but I will copy this and tweak here for the simplest state of StackOverflow. :)

- STA , , . , , WPF . , . , Dispatcher:: Run, . , Invoke Dispatcher ( , Dispatcher:: FromThread), , , . , - Disptacher:: Run, , . , Dispatcher:: InvokeShutdown.

+2

. , , , , , . , , XamlReader.Parse() .

+2

WPF, , , ? , , , .

Then your web application can simply unscrew it as a separate process, wait for it to complete and capture the file. This way, you don’t have to worry about starting multiple STA threads.

0
source

Source: https://habr.com/ru/post/1720206/


All Articles