c# - How do you pass a BitmapImage from a background thread to the UI thread in WPF? -


I have a background thread that generates a line of bitmap image objects whenever the background Thread generates a bitmap, I want to show this bitmap to the user.

This is an MVVM project, so in my opinion, there is Image , the problem is how to pass BitmapImage from the background thread to the UI thread. Code> Element:

  & lt; Image Source = "{Binding Generated Image}" />  

My visual-model has a property Generated Image :

  Private bitmap image -granted image; Public Bitmapmise GeneratedImage {get {return_generatedImage; } Set {if (value == _generatedImage) return; _generatedImage = value; RaisePropertyChanged ("GeneratedImage"); }}  

There is also a code in my view-model that creates a background thread:

  Start public Zero: Garmin Avenues (list & lt; coordinate & Gt; coordinate) {Thread Start Generator Thread Sterer = Representative {GenerateImages (Coordinate); }; Var Generator Thread = New Thread (Generator Threadstarter); Generator thread. Aparbestrate = apartmentstate. STA; Generator Thread Ibsackground = true; GeneratorThread.Start (); } Private Void Generated Images (List & Lt; Coordinate & gt; Coordinate) {foreach (Sync with Win coordinate) {var backgroundThreadImage = GenerateImage (Coordination); // I'm stuck here ... how can I pass it on the UI thread? }}  

I want to pass the user thread to backgroundThreadImage , where it will become the uiThreadImage , then Generated Image = UTTHREEDIT IMAGE so that the view can be updated. I have seen some examples dealing with WPF Dispatcher , but I can not find any such example that addresses this issue. Please advise.

Uses the sender to execute an action representative on the following UI thread. It uses a synchronous model, the optional representative will execute asynchronous.

  var pagmittreredies = generate image (coordination); Generated Image Dispatcher. Invoke (Dispair PreRetrie. Normal, New Action ((=) = {Generated Image = Background Tride Image;})); As discussed in the comments,  

UPDATE is not being made BitmapImage on the UI thread because the above will not work alone . If you do not intend to modify the image, you can create and freeze it and then allocate the image generated in the Dispatch Representative (bitmap image becomes readable only and thus the thread as a result of freeze) . The second option would be to load the image in memorystream on the background thread and then create a bitmap image on the UI thread in the Dispatch Representative with the stream resource property of that stream and bitmap image.


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -