Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have run into the same issue while trying to programmatically retrieve images from Facebook. The strange part is that if you point a Silverlight image control to the Facebook image url, the image is retrieved and displayed without error. This got me thinking and I have come up with a viable workaround that seems to work consistently for my situation. I hope you find it useful too.</p> <pre><code>var uri = new Uri("http://graph.facebook.com/mglace/picture/", UriKind.Absolute); var bmp = new BitmapImage(); bmp.ImageOpened += (sender, e) =&gt; { /* Do something here with the sender (which is the BitmapImage) */ }; bmp.CreateOptions = BitmapCreateOptions.None; bmp.UriSource = uri; </code></pre> <p>Create a <code>BitmapImage</code> object, set an event handler for the <code>ImageOpened</code> event and set the <code>CreateOptions</code> property to <code>BitmapCreateOptions.None</code>. Finally, set the <code>UriSource</code> to the Facebook image you want to retrieve. The image is downloaded immediately because we set the <code>CreateOptions</code> to <code>None</code> (the default value is <code>DelayedCreation</code>). You can then perform any actions you would like in the <code>ImageOpened</code> event handler.</p> <p>I wanted to encapsulate this logic in my service layer and beef up the error handling and such so I wrapped it in a Reactive Extensions observable to make it easier to use. Here is my final code snippet:</p> <pre><code>public IObservable&lt;BitmapImage&gt; GetProfilePhoto(string profileId) { return Observable.Create&lt;BitmapImage&gt;( observer =&gt; { // This handler handles a successful fetch EventHandler&lt;RoutedEventArgs&gt; openedHandler = (sender, args) =&gt; { try { observer.OnNext(sender as BitmapImage); observer.OnCompleted(); } catch (Exception ex) { observer.OnError(ex); } }; // This handler handle a failure EventHandler&lt;ExceptionRoutedEventArgs&gt; failedHandler = (sender, args) =&gt; observer.OnError(args.ErrorException); var url = string.Format("http://graph.facebook.com/{0}/picture/", profileId); var uri = new Uri(url, UriKind.Absolute); BitmapImage bmp = null; try { Deployment.Current.Dispatcher.BeginInvoke( () =&gt; { bmp = new BitmapImage(); bmp.ImageOpened += openedHandler; bmp.ImageFailed += failedHandler; bmp.CreateOptions = BitmapCreateOptions.None; bmp.UriSource = uri; }); } catch (Exception ex) { observer.OnError(ex); } return () =&gt; { // Cleanup the event handlers if (bmp != null) { bmp.ImageOpened -= openedHandler; bmp.ImageFailed -= failedHandler; } }; }); } </code></pre> <p>And usage:</p> <pre><code>GetProfilePhoto("mglace") .Subscribe(image =&gt; { /* Do something with the image in here*/ }, error =&gt; { /* Handle any errors in here */ }, () =&gt; { /* Finalization code goes here */ }); </code></pre> <p>I hope someone out there finds this useful.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload