无法从'System.IO.Stream'转换为'Windows.Storage.Streams.IRandomAccessStream' [英] Cannot convert from 'System.IO.Stream' to 'Windows.Storage.Streams.IRandomAccessStream'

查看:70
本文介绍了无法从'System.IO.Stream'转换为'Windows.Storage.Streams.IRandomAccessStream'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Httpwebrequest从网页下载图片 - 我必须手动执行此操作,而不是仅仅将源设置为uri,因为网站管理员希望我使用POST。

I'm try to use Httpwebrequest to download an image from a webpage - I have to do this manually rather than just setting the source to a uri as the webmaster would like me to use POST.

所以我一直在尝试使用httpwebrequest响应中的流并将其设置为新的位图源。我通过以下方式使用Windows Phone应用程序完成此操作:

So I've been trying to use the stream from the httpwebrequest response and set that as the new bitmap source. I have done this with a Windows Phone app via the following:

public MainPage()
        {
            InitializeComponent();          

                HttpWebRequest req = HttpWebRequest.CreateHttp(new Uri("imagelocation.com"));
                req.Method = "POST";
                req.BeginGetRequestStream(new AsyncCallback(OnRequestReceived), req);
            
        }


        void OnRequestReceived(IAsyncResult result)
        {
            HttpWebRequest req = (HttpWebRequest)result.AsyncState;

            Stream s = req.EndGetRequestStream(result);
            s.Close();

            req.BeginGetResponse(new AsyncCallback(OnResponseReceived), req);
        }


        void OnResponseReceived(IAsyncResult result)
        {

            try
            {
                HttpWebRequest req = (HttpWebRequest)result.AsyncState;
                HttpWebResponse response = (HttpWebResponse)req.EndGetResponse(result);


                Stream s = response.GetResponseStream();

                MemoryStream ms = new MemoryStream();
                byte[] bData = new byte[1024 * 1024];
                int iToRead = bData.Length;
                while (iToRead > 0)
                {
                    iToRead = s.Read(bData, 0, (int)bData.Length);
                    ms.Write(bData, 0, iToRead);
                }
                ms.Seek(0, SeekOrigin.Begin);

                Dispatcher.BeginInvoke(new Action<Stream>(
                        delegate(Stream stream)
                        {
                            BitmapImage bi = new BitmapImage();
                            bi.SetSource(stream);
                            todayGeomagneticAct.Source = bi;
                        })
                        ,
                        new object[] { ms });

                response.Dispose();
            }
            catch (WebException){ }
                
            
        }

使用Windows 8,我可以' t(或者说我认为我不能)使用调度程序和deligate,所以我只是尝试使用响应流作为位图源,但是我得到了上述错误:

With Windows 8 though, I can't (or rather I don't think I can) use the dispatcher and deligate, so I instead simply tried using the response stream as the bitmap source but I get the aforementioned error:

"无法从'System.IO.Stream'转换为'Windows.Storage.Streams.IRandomAccessStream'"

"Cannot convert from 'System.IO.Stream' to 'Windows.Storage.Streams.IRandomAccessStream'"

然后我试图"转换"到IRandomAccess流的IO流,见下文,但没有运气......

I then tried to "convert" the IO stream to the IRandomAccess stream, see below, but with no luck...

 InMemoryRandomAccessStream ims = new InMemoryRandomAccessStream();
                var stream2 = ims.OpenWrite();
                
                await Task.Factory.StartNew(() => stream.CopyTo(stream2));

有人有什么建议吗?非常感谢他们。

Does anybody have any suggestions? They would be gratefully appreciated.

凯西

推荐答案

我发现了以下内容:
http://stackoverflow.com/questions/8678080/how-to-convert-a-simple- streamhttp-webresponse-to-bitmapimage-in-c-sharp-windo

我假设"命令"应该是响应流,但是我收到以下错误:

I assume "command" should be the response stream, but I get the following error:

错误  1 无法将类型'System.Net.HttpWebResponse'转换为'byte []'

Error 1 Cannot convert type 'System.Net.HttpWebResponse' to 'byte[]'

非常感谢任何见解,谢谢:)

Any insight would be gratefully appreciated, thank you :)


这篇关于无法从'System.IO.Stream'转换为'Windows.Storage.Streams.IRandomAccessStream'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆