this seems to be quite trivial and I saw some similar questions online but I couldn't get it to work anyways. I have a C# thread that waits for an incoming TCP client then reads an image over TCP. All of this works fine but I can't get the thread to display the image.
Here's what I have. In the UI Thread:
public partial class ReadImagePage : Page {
//...
listener = new TcpListener(IPAddress.Any, 11114);
listenThread = new Thread(() => AsyncAccept(this, listener));
listenThread.Start();
//...
}
In the AsyncAccept:
private void AsyncAccept(ReadPassportPage readPassportPage, TcpListener listener)
{
listener.Start();
TcpClient client = listener.AcceptTcpClient();
var image = new BitmapImage();
// read bitmap from tcp...
}
I now want to set the image to an Image within my WPF page. I tried multiple variations of this. Even passing the page object to the thread. But I kept getting an InvalidOperationException exception because the Thread is not allowed to access the UI element.
Application.Current.Dispatcher.BeginInvoke(new Action(delegate() {
signatureImage.Source = image;
}));
How can I get this to work? Any help is greatly appreciated. Thanks in advance.