Asked by:
Render FrameworkElement without outside WPF application

Question
-
We have a Class, written in F# which is basically a FrameworkElement. Internal there is a VisualCollection which holds the rendered parts - for compositioning i guess.
The class itself represent a Graphic-To-Be-Rendered, which in this special case, has to be done without a UI thread and outside of a WPF application.
What we do is the following:
- Spawn an STA Thread
- - Instantiate the Graphics
- - Populate with Data
- - Render to Bitmap
- - Return Synchronized stream
- Outside the STA Thread
- - Write stream to File
The file is empty but has the correct width & length.
I believe that it is not possible to do this without an App() or a WPF Context of some kind. We're kind of at a loss here.
Any Suggestions are welcome, since i've got nothing a the moment. The goal is basically to render something WPF into a FileStream from withing a thread inside a console application.
- Edited by This_display_name_is_already_in_use_They_all_are Tuesday, July 9, 2019 8:54 AM cleanup
- Moved by Yong LuMicrosoft contingent staff Monday, July 22, 2019 2:13 AM off topic
Tuesday, July 9, 2019 8:53 AM
All replies
-
Hi Friend,
>>We have a Class, written in F# which is basically a FrameworkElement. Internal there is a VisualCollection which holds the rendered parts - for compositioning i guess.
I'm not familiar with F#, you may can go to the F# forum for getting suitable.
>>Any Suggestions are welcome, since i've got nothing a the moment. The goal is basically to render something WPF into a FileStream from withing a thread inside a console application.
The following links described how to render a WPF UserControl to a bitmap(C#). It may can give you some help.
How to render a WPF UserControl to a bitmap without creating a window
Server-side rendering of WPF UserControl
Best regards
Yong LuMSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.Wednesday, July 10, 2019 6:48 AM -
Hi and Thanks for your reply.
With combinding some of the solutions i got it working - but with a hack i cannot use in production. But at least now we know a little bit more about the Problem.
In F#, whenever we calculate stuff, we run
do! Async.SwitchToThreadPool()
When we are ready for actually drawing stuff, we run
do! Async.SwitchToContext guiSynchronizationContext
When debugging, i can get as far as to switching back to the UI Context, but there the execution stops - or the Debugger cannot hit any further breakpoint.
No Exception, no Debug output - nothing to work with. The rendered output is empty.
Now if we replace all the SwitchToThreadPool commands with SwitchToUIContext ones, the rendering suddenly starts working. Meaning that there is some kind of configuration issue that prevents us from reliably switching between the STA thread and the thread pool.
Thursday, July 11, 2019 12:36 PM -
Hi and Thanks for your reply.
With combinding some of the solutions i got it working - but with a hack i cannot use in production. But at least now we know a little bit more about the Problem.
In F#, whenever we calculate stuff, we run
do! Async.SwitchToThreadPool()
When we are ready for actually drawing stuff, we run
do! Async.SwitchToContext guiSynchronizationContext
When debugging, i can get as far as to switching back to the UI Context, but there the execution stops - or the Debugger cannot hit any further breakpoint.
No Exception, no Debug output - nothing to work with. The rendered output is empty.
Now if we replace all the SwitchToThreadPool commands with SwitchToUIContext ones, the rendering suddenly starts working. Meaning that there is some kind of configuration issue that prevents us from reliably switching between the STA thread and the thread pool.
Hi Friend,
I suggest you can go to the F# forum for getting suitable help.
Thank you for your understanding.
Best regards
Yong Lu
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.Friday, July 12, 2019 3:06 AM