locked
IInputStream to IRandomAccessStream

    Question

  • Hello,

    I'm using the Background Transfer service to download an image file. I'd like to load the image into a XAML Image control, however the Background Transfer service returns the downloaded data in a a stream of type IInputStream. I'm trying to load this data into the BitmapImage class, however BitmapImage.SetSource() takes in a stream of type IRandomAccessStream.

    How would I convert a stream of type IInputStream to IRandomAccess stream?

     

    Thanks.

    Saturday, November 5, 2011 10:09 PM

Answers

  • Hi,

    Do you know where you are saving this file?  if yes, then you have a StorageFile^ corresponding to this file.  In that case, you can create a IRandomAccessStream for this file and associate with the image element in the following way:

    		auto file = op->GetResults();
    		auto openOp = file->OpenAsync(FileAccessMode::Read);
    		openOp->Completed = ref new AsyncOperationCompletedHandler<Streams::IRandomAccessStream^>([file, this](IAsyncOperation<Streams::IRandomAccessStream^> ^stop)
    		{
    			auto strm = stop->GetResults();
    			CapturedVideo->SetSource(strm, file->FileType);
    		});
    		openOp->Start();
    

    Thanks


    Raman Sharma | Program Manager, Visual C++ | @rasharm_msft

    (if my post has answered your question, please consider using the 'mark as answer' feature in the forums to help others)
    Sunday, November 6, 2011 4:31 AM