Windows phone 7 在Windows Phone7中读取流

Windows phone 7 在Windows Phone7中读取流,windows-phone-7,stream,Windows Phone 7,Stream,我正在使用WindowsPhoneSDK7,并试图实现图像文件的下载。我无法使用标准BitmapImage对象,因为我的服务器使用表单身份验证cookie,而且据我所知,无法向浏览器控件或BitmapImage对象传递cookie容器。。。(顺便说一句,如果有办法做到这一点,我也想知道——这将使我的代码更简单!) 不管怎样,我尝试做的应该是可能的-我得到一个响应流,现在我需要从中读取图像数据 豪沃 _clientData.BeginRead(_buffer, _currentPositi

我正在使用WindowsPhoneSDK7,并试图实现图像文件的下载。我无法使用标准BitmapImage对象,因为我的服务器使用表单身份验证cookie,而且据我所知,无法向浏览器控件或BitmapImage对象传递cookie容器。。。(顺便说一句,如果有办法做到这一点,我也想知道——这将使我的代码更简单!)

不管怎样,我尝试做的应该是可能的-我得到一个响应流,现在我需要从中读取图像数据

豪沃

    _clientData.BeginRead(_buffer, _currentPosition, _buffer.Length, new AsyncCallback(ReadCallback), null);
返回错误:

    Specified argument was out of the range of valid values.
    Parameter name: count
       at MS.Internal.InternalNetworkStream.BeginRead(Byte[] buffer, Int32 offset, Int32 count,         AsyncCallback callback, Object state)
       at TestCode.ItemViewModel.ReadImageByChunks()
       at TestCode.ItemViewModel.ReadCallback(IAsyncResult ar)
       at MS.Internal.InternalNetworkStream.StreamAsyncResult.Complete(Int32 bytesProcessed,         Boolean synchronously, Exception error)
       at MS.Internal.InternalNetworkStream.ReadOperation(Object state)
       at MS.Internal.InternalNetworkStream.BeginRead(Byte[] buffer, Int32 offset, Int32 count,         AsyncCallback callback, Object state)
       at TestCode.ItemViewModel.ReadImageByChunks()
       at TestCode.ItemViewModel.<>c__DisplayClassb.<LoadImageFromServer>b__a(IAsyncResult rspAR)
       at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClassa.<InvokeGetResponseCallback>b__8(Object state2)
       at System.Threading.ThreadPool.WorkItem.doWork(Object o)
       at System.Threading.Timer.ring()

根据我自己的直觉和在互联网上找到的代码,我已经多次重写了这段代码(但没有一段代码是针对WindowsPhone7的)。上面的版本是模仿post的。但到目前为止运气不好。任何帮助都将不胜感激

不要将索引传递到BeginRead,因为它指的是要开始写入的缓冲区的索引。您当前请求向缓冲区外部程序写入其边界


将_currentPosition替换为0,应该可以解决问题。事实上,您根本不需要跟踪_currentPosition,因为流保持自己的状态。

谢谢Richard-这解决了问题!不幸的是,我没有为你的答案投票所需的分数:(@user917153你以新的匿名用户的身份发布了该评论。如果你以发布问题的用户(LWP)的身份重新登录,你可以将其标记为“答案”。如果你使用与以前相同的计算机/浏览器,你仍然会登录:)
    private void ReadImageByChunks()
    {
        try
        {
            _clientData.BeginRead(_buffer, _currentPosition, _buffer.Length, new AsyncCallback(ReadCallback), null);

        }
        catch (Exception error)
        {
            int i = 1; 
        }
    }

    private void ReadCallback(IAsyncResult ar)
    {
        try
        {
            int bytesRead = _clientData.EndRead(ar);

            if (bytesRead > 0)
            {
                _imageStream.Write(_buffer, _currentPosition, bytesRead);
                _currentPosition = _currentPosition + bytesRead;
            }

            if (bytesRead == _buffer.Length)
                ReadImageByChunks();
            else
            {
                //do stuff 
            }
        }
        catch (Exception error)
        {
            int i = 1;
        }
    }