So, I want to send the image from my WPF app to the Web API controller. When I get the image as BitmapImage and try to send it, I get the error "the calling thread cannot access this object because a different thread owns it". I don't see how am I modifying UI and why do I get the error. Here's the code:
WPF code:
private void BtnSendImage_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = Environment.SpecialFolder.MyDocuments.ToString();
BitmapImage slika = null;
if (ofd.ShowDialog() == true)
{
odabranaSlika = ofd.FileName;
Uri adresa = new Uri(odabranaSlika, UriKind.Absolute);
slika = new BitmapImage(adresa);
ItemDAL idal = new ItemDAL();
if (idal.SendImage(slika))
{
MessageBox.Show("Yay!");
}
else
{
MessageBox.Show("Awwww");
}
}
}
Method SendImage from the ItemDAL class (the code stops at postResult.Wait() part):
public bool SendImage(BitmapImage bmp)
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:52407/");
var postResult = client.PostAsJsonAsync<BitmapImage>("api/values/postimage", bmp);
postResult.Wait();
var result = postResult.Result;
if (result.IsSuccessStatusCode)
{
return true;
}
else
{
return false;
}
}
}
What am I doing wrong? I know I'm not suppose to modify UI, unless it's from the main thread, bot how is this doing anything to the UI?