locked
How to Share image from Web in Share Contract RRS feed

  • Question

  • Using the Share-To contract in an HTML5 app Metro App, how do you share an image from an image URL (like http://whatever.com/test.png) using the setBitmap() function? 

    Thanks,

    Michael

     

     


    Michael S. Scherotter
    Principal Architect Evangelist Microsoft Corporation,
    Blog, Twitter, Facebook

    Thursday, December 8, 2011 7:36 PM

Answers

  • I was able to get it working with the following code:

    var deferral = request.getDeferral();
    
    var imageSource = Windows.Storage.Streams.StreamReference.createFromUri(new Windows.Foundation.Uri(imageUrl));
    
    imageSource.getStreamAsync().then(function (randomAccessStream) {
        request.data.setBitmap(randomAccessStream);
        deferral.complete();
    });
    
    


    Thanks for the help.

    Michael


    Michael S. Scherotter
    Principal Architect Evangelist Microsoft Corporation,
    Blog, Twitter, Facebook

    Friday, December 9, 2011 5:51 PM

All replies

  • Hi Michael,

    I think you are well on your way.  I think you understand what needs to be done, but you want to know how to set a stream on the setBitmap function from the downloaded image.  Is that correct?

    -Jeff


    Jeff Sanders (MSFT)
    Friday, December 9, 2011 3:02 PM
    Moderator
  • Yes,

    I need to know how to get the stream of the image: sample code in JavaScript would help.

    Thanks,

    Michael


    Michael S. Scherotter
    Principal Architect Evangelist Microsoft Corporation,
    Blog, Twitter, Facebook

    Friday, December 9, 2011 3:10 PM
  • Michael,

    You can get the stream from the URI using this call:

    http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.streams.streamreference.createfromuri.aspx

    That may be enough for you to get going.  If not I will see if I can get a quick sample together for you.

    -Jeff


    Jeff Sanders (MSFT)
    Friday, December 9, 2011 5:18 PM
    Moderator
  • I was able to get it working with the following code:

    var deferral = request.getDeferral();
    
    var imageSource = Windows.Storage.Streams.StreamReference.createFromUri(new Windows.Foundation.Uri(imageUrl));
    
    imageSource.getStreamAsync().then(function (randomAccessStream) {
        request.data.setBitmap(randomAccessStream);
        deferral.complete();
    });
    
    


    Thanks for the help.

    Michael


    Michael S. Scherotter
    Principal Architect Evangelist Microsoft Corporation,
    Blog, Twitter, Facebook

    Friday, December 9, 2011 5:51 PM
  • Awesome!  The code looks good.  Thanks for sharing!
    Jeff Sanders (MSFT)
    Friday, December 9, 2011 6:59 PM
    Moderator