locked
How To Convert Stream To IRandomAccessStream?

    Pregunta

  • i use BitmapImage to SetSource,and the method's parameter is IRandomAccessStream. but here is  variable of Stream Type, how can i Convert Stream To IRandomAccessStream?

    Thanks~~~~

    lunes, 05 de marzo de 2012 8:47

Respuestas

Todas las respuestas

  • if your project has

    using System.IO;

    then on the various stream objects you should see some extension methods to help you out.

    Tim Heuer | Program Manager, XAML | http://timheuer.com/blog | @timheuer

    (if my post has answered your question, please consider using the 'mark as answer' feature in the forums to help others)

    lunes, 05 de marzo de 2012 16:35
  • Such as? I have the same issue and nothing is jumping out at me.

    EDIT:

    Nevermind, it's just a bit convoluted. AsStreamForRead and casts and so forth.

    lunes, 05 de marzo de 2012 21:52
  • Ok, I give up.  How do you convert a Stream to an IRandomAccessStream?

    I see stream.AsInputStream(), but that gives you an IInputStream which you can't cast to IRandomAccessStream.

    There's nothing there to convert to an IRandomAccessStream.

    I have an image I'm reading from a ZipArchive which comes in Stream format, which I want to load into a BitmapImage via BitmapImage.SetSource.  SetSource takes an IRandomAccessStream.

    jueves, 29 de marzo de 2012 6:26
  • Any resolution on this?  I'm having the same issue.

    MarkM

    sábado, 31 de marzo de 2012 15:36
  • You can use an InMemoryRandomAccessStream.  For example:

          StorageFile                 sf = await Package.Current.InstalledLocation.GetFileAsync(@"Assets\Logo.png");
          InMemoryRandomAccessStream  ras = new InMemoryRandomAccessStream();
    
          using (Stream stream = await sf.OpenStreamForReadAsync())
          {
            await stream.CopyToAsync(ras.AsStreamForWrite());
          }
    
          BitmapImage     bi = new BitmapImage();
          bi.SetSource(ras);
          img.Source = bi;

    Joe
    sábado, 31 de marzo de 2012 18:45
  • Thanks for the reply Joe.  That is what we decided to do in the mean time.  But reading the entire resource into memory might not always work for us.  

    We decided to just implement the IRandomAccessStream interface around a Stream object but got stuck when implementing the ReadAsync and WriteAsync methods.  Don't have any idea how to create or implement an IAsyncOperationWithProgress.

    IAsyncOperationWithProgress<IBuffer, UInt32> ReadAsync(
      IBuffer buffer, 
      uint count, 
      InputStreamOptions options
    )

    sábado, 31 de marzo de 2012 19:39
  • See AsInputStream and others conversion functions. I think this is what Tim was referring to.

    lunes, 16 de abril de 2012 1:13
  • Hey Chris!  How've you been?

    The problem with AsInputStream is that it converts a Stream to an IInputStream.  What I need is a Stream converted to an IRandomAccessStream.  For some reason BitmapImage.SetSource takes an IRandomAccessStream rather than an IInputStream.

    Is there a way to convert an IInputStream to an IRandomAccessStream?

    • Propuesto como respuesta sanath shetty lunes, 19 de noviembre de 2012 6:46
    • Votado como útil sanath shetty lunes, 19 de noviembre de 2012 6:47
    lunes, 16 de abril de 2012 17:52
  • doing well...

    turns out these conversion function convert to/from IInputStream and IOutputStream. some APIs (the Bitmap SetSource) only accept IRandomAccessStream and are not satisfied by these conversions.

    the work around is to copy the data into an in memory stream.

    RandomAccessStream is seekable, Input/output stream are not, so a conversion function must be able to buffer the whole stream and is not simple to implement.


    • Editado Chris Guzak lunes, 16 de abril de 2012 19:37
    • Propuesto como respuesta sanath shetty lunes, 19 de noviembre de 2012 6:46
    • Votado como útil sanath shetty lunes, 19 de noviembre de 2012 6:47
    lunes, 16 de abril de 2012 19:30