I have a textbox in my page named LinkURL
. I'm trying to create Uri with its Text value. And use the Uri for BitmapImage
. So the user can see the image while typing the URL.
But my UI freezes and application stops running. I'd like to use it for preview image.
public void LoadImage(object sender, TextChangedEventArgs e)
{
Uri uri = null;
Dispatcher.BeginInvoke(new Action(delegate
{
uri = new Uri(this.LinkURL.Text);
WebClient wc = new WebClient();
wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
wc.OpenReadAsync(uri, wc);
}));
}
private void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = e.Result;
this.Preview.Source = bi;
}
I tried these but didn't work.
UI freezes and gives me System.Reflection.TargetInvocationException
or ArgumentNullException
.