locked
Looking for Equivalent to StorageFile class for reading from web RRS feed

  • Question

  • In the codeplex project WinRTXamlToolkit, there is a SetSourceAsync that takes a StorageFile as a parameter. I want to do the same thing with a Uri but can not find the equivalent functionalilty without doing a bunch of work.  Any suggestions would be appreciated.  Below is the class that uses file.  I really am trying to find something that is like "SetSourceAsync(this Bitmap bitmap, Uri uri)

    /// <summary>
            /// Sets the source image for a BitmapImage by opening
            /// a given file and processing the result asynchronously.
            /// </summary>
            /// <param name="bitmap">The bitmap.</param>
            /// <param name="file">The file.</param>
            /// <returns></returns>
            public static async Task<BitmapImage> SetSourceAsync(this BitmapImage bitmap, StorageFile file)
            {
                using (var stream = await file.OpenReadAsync())
                {
                    await bitmap.SetSourceAsync(stream);
                }
    
                return bitmap;
            }


    Peter Kellner http://peterkellner.net Microsoft MVP • ASPInsider

    Tuesday, December 4, 2012 5:18 PM

Answers

  • you will have to make a wrapper around a stream that implements IRandomAccessStream

    this:

    async void Test()
            {
                HttpClient client = new HttpClient();
                var stream = await client.GetStreamAsync("");
                var ms = new MemoryStream();
                await stream.CopyToAsync(ms);
                ms.Position = 0;
                var ram = new StreamRandomAccessStream(ms);
    
                BitmapImage image = new BitmapImage();
                await image.SetSourceAsync(ram);
            }
    
            public class StreamRandomAccessStream : IRandomAccessStream
            {
                private MemoryStream _stream;
    
                public StreamRandomAccessStream(MemoryStream stream)
                {
                    _stream = stream;
                }
    
                public bool CanRead
                {
                    get { return _stream.CanRead; }
                }
    
                public bool CanWrite
                {
                    get { return _stream.CanWrite; }
                }
    
                public IRandomAccessStream CloneStream()
                {
                    var memoryStream = new MemoryStream();
                    _stream.CopyTo(memoryStream);
                    return new StreamRandomAccessStream(memoryStream);
                }
    
                public IInputStream GetInputStreamAt(ulong position)
                {
                    _stream.Position = (long)position;
                    return _stream.AsInputStream();
                }
    
                public IOutputStream GetOutputStreamAt(ulong position)
                {
                    _stream.Position = (long)position;
                    return _stream.AsOutputStream();
                }
    
                public ulong Position
                {
                    get { return (ulong)_stream.Position; }
                }
    
                public void Seek(ulong position)
                {
                    _stream.Seek((long)position, SeekOrigin.Begin);
                }
    
                public ulong Size
                {
                    get
                    {
                        return (ulong)_stream.Length;
                    }
                    set
                    {
                        _stream.SetLength((long)value);
                    }
                }
    
                public void Dispose()
                {
                    _stream.Dispose();
                }
    
                public IAsyncOperationWithProgress<IBuffer, uint> ReadAsync(IBuffer buffer, uint count, InputStreamOptions options)
                {
                    return _stream.AsInputStream().ReadAsync(buffer, count, options);
                }
    
                public IAsyncOperation<bool> FlushAsync()
                {
                    return _stream.AsOutputStream().FlushAsync();
                }
    
                public IAsyncOperationWithProgress<uint, uint> WriteAsync(IBuffer buffer)
                {
                    return _stream.AsOutputStream().WriteAsync(buffer);
                }
            }


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by Min ZhuMember Monday, January 7, 2013 6:12 AM
    Tuesday, December 4, 2012 6:53 PM