locked
[UWP][XAML] How to build a BitmapImage file from a web request stream RRS feed

  • Question

  • Dear all,

    I am using a web service to generate a QRCOde

    The web service I am using is from this url http://goqr.me/api/doc/create-qr-code

    My code is as follwing :

    string QRServiceAPI = "https://api.qrserver.com/v1/create-qr-code/?data={0}&size={1}x{2}";
    			string q = string.Format(QRServiceAPI, "http://solatys.com", 150, 150);
    			using (var d = new Downloader())
    			{
    				var feed = await d.GetDataFeed(q);
    			}
    
    
    public class Downloader : IDisposable
    		{
    			public async Task<string> GetDataFeed(string feedUrl)
    			{
    				var request = (HttpWebRequest)WebRequest.Create(feedUrl);
    				request.Method = "GET";
    
    				var response = (HttpWebResponse)await request.GetResponseAsync();
    				var stream = new StreamReader(response.GetResponseStream());
    				
    				var result = stream.ReadToEnd();
    				return result;
    			}
    
    			public void Dispose()
    			{
    
    			}
    		}

    WHat I need to do from this is from the return feed, I need to build a BitMapImage to be bind to my ViewModel and get display in my UI.

    How can I build that Image ?

    thanks for help

    Friday, July 1, 2016 3:09 PM

Answers

  • This should work

            private async void Populate()
            {
                string QRServiceAPI = "https://api.qrserver.com/v1/create-qr-code/?data={0}&size={1}x{2}";
                string q = string.Format(QRServiceAPI, "http://solatys.com", 150, 150);
                using (var d = new Downloader())
                {
                    var feed = await d.GetDataFeed(q);
                    feed.Seek(0, SeekOrigin.Begin);
                    BitmapImage bmp = new BitmapImage();
    
                    using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream())
                    {
                        using (DataWriter writer = new DataWriter(ms.GetOutputStreamAt(0)))
                        {
                            writer.WriteBytes((byte[])feed.ToArray());
                            writer.StoreAsync().GetResults();
                        }
                        var image = new BitmapImage();
                        image.SetSource(ms);
                        Image1.Source = image;
                    }
                }
            }

    And Downloader Class

    public class Downloader : IDisposable
        {
            public async Task<MemoryStream> GetDataFeed(string feedUrl)
            {
                var request = (HttpWebRequest)WebRequest.Create(feedUrl);
                request.Method = "GET";
    
                var response = (HttpWebResponse)await request.GetResponseAsync();
    
                MemoryStream ms = new MemoryStream();
                var stream = response.GetResponseStream();
                stream.CopyTo(ms);
                return ms;
            }
    
            public void Dispose()
            {
    
            }
        }


    Gaurav Khanna | Microsoft .NET MVP | Microsoft Community Contributor


    • Edited by Khanna Gaurav Friday, July 1, 2016 6:57 PM
    • Proposed as answer by Khanna Gaurav Friday, July 8, 2016 6:09 AM
    • Marked as answer by Barry Wang Thursday, July 14, 2016 1:41 AM
    Friday, July 1, 2016 6:50 PM

All replies

  • Try following code

        public class Downloader : IDisposable
    		{
    			public async Task<MemoryStream> GetDataFeed(string feedUrl)
    			{
    				var request = (HttpWebRequest)WebRequest.Create(feedUrl);
    				request.Method = "GET";
    
    				var response = (HttpWebResponse)await request.GetResponseAsync();
    
                    MemoryStream ms = new MemoryStream();
                    byte[] bytes = new byte[1024];
                    try
                    {
                        var stream = response.GetResponseStream();
                        var sr = new BinaryReader(stream);
    
                        int bytesRead = stream.Read(bytes, 0, bytes.Length);
                        if (bytesRead > 0)
                        {
                            ms.Write(bytes, 0, bytesRead);
                            bytesRead = stream.Read(bytes, 0, bytes.Length);
                        }
                    }
                    catch (Exception)
                    {
                        //Handle exception
                    }
                    finally
                    {
                        response.Close();
                    }
    
                    return ms;
    			}
    
    			public void Dispose()
    			{
    
    			}
    		}


     string QRServiceAPI = "https://api.qrserver.com/v1/create-qr-code/?data={0}&size={1}x{2}";
                string q = string.Format(QRServiceAPI, "http://solatys.com", 150, 150);
                using (var d = new Downloader())
                {
                    var feed = await d.GetDataFeed(q);
                    feed.Seek(0, SeekOrigin.Begin);
                    BitmapImage bmp = new BitmapImage();
                    bmp.BeginInit();
                    bmp.StreamSource = feed;
                    bmp.EndInit();
                    Image1.Source = bmp;
                }


    Gaurav Khanna | Microsoft .NET MVP | Microsoft Community Contributor

    Friday, July 1, 2016 4:11 PM
  • THe following code is not comptable for UWP

    bmp.BeginInit();
                    bmp
    .StreamSource = feed;
                    bmp
    .EndInit();

    How to do ?

    Friday, July 1, 2016 5:05 PM
  • This should work

            private async void Populate()
            {
                string QRServiceAPI = "https://api.qrserver.com/v1/create-qr-code/?data={0}&size={1}x{2}";
                string q = string.Format(QRServiceAPI, "http://solatys.com", 150, 150);
                using (var d = new Downloader())
                {
                    var feed = await d.GetDataFeed(q);
                    feed.Seek(0, SeekOrigin.Begin);
                    BitmapImage bmp = new BitmapImage();
    
                    using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream())
                    {
                        using (DataWriter writer = new DataWriter(ms.GetOutputStreamAt(0)))
                        {
                            writer.WriteBytes((byte[])feed.ToArray());
                            writer.StoreAsync().GetResults();
                        }
                        var image = new BitmapImage();
                        image.SetSource(ms);
                        Image1.Source = image;
                    }
                }
            }

    And Downloader Class

    public class Downloader : IDisposable
        {
            public async Task<MemoryStream> GetDataFeed(string feedUrl)
            {
                var request = (HttpWebRequest)WebRequest.Create(feedUrl);
                request.Method = "GET";
    
                var response = (HttpWebResponse)await request.GetResponseAsync();
    
                MemoryStream ms = new MemoryStream();
                var stream = response.GetResponseStream();
                stream.CopyTo(ms);
                return ms;
            }
    
            public void Dispose()
            {
    
            }
        }


    Gaurav Khanna | Microsoft .NET MVP | Microsoft Community Contributor


    • Edited by Khanna Gaurav Friday, July 1, 2016 6:57 PM
    • Proposed as answer by Khanna Gaurav Friday, July 8, 2016 6:09 AM
    • Marked as answer by Barry Wang Thursday, July 14, 2016 1:41 AM
    Friday, July 1, 2016 6:50 PM