Пример #1
0
        /// <summary>
        /// 根据http url地址获取gif
        /// </summary>
        /// <param name="uri">http url地址</param>
        private void GetGifStreamFromHttp(Uri uri)
        {
            try
            {
                WebReadState webReadState = new WebReadState();
                webReadState.MemoryStream       = new MemoryStream();
                webReadState.WebRequest         = WebRequest.Create(uri);
                webReadState.WebRequest.Timeout = 10000;

                webReadState.WebRequest.BeginGetResponse(new AsyncCallback(WebResponseCallback), webReadState);
            }
            catch (SecurityException)
            {
                CreateNonGifAnimationImage();
            }
        }
Пример #2
0
        /// <summary>
        /// web数据响应回调
        /// </summary>
        /// <param name="asyncResult">异步回调结果</param>
        private void WebResponseCallback(IAsyncResult asyncResult)
        {
            WebReadState webReadState = (WebReadState)asyncResult.AsyncState;
            WebResponse  webResponse;

            try
            {
                webResponse             = webReadState.WebRequest.EndGetResponse(asyncResult);
                webReadState.ReadStream = webResponse.GetResponseStream();
                webReadState.Buffer     = new byte[100000];
                webReadState.ReadStream.BeginRead(webReadState.Buffer, 0, webReadState.Buffer.Length, new AsyncCallback(WebReadCallback), webReadState);
            }
            catch (WebException exp)
            {
                this.Dispatcher.Invoke(DispatcherPriority.Render, new WebRequestErrorDelegate(WebRequestError), exp);
            }
        }
Пример #3
0
        /// <summary>
        /// web数据读取
        /// </summary>
        /// <param name="asyncResult">异步数据结构</param>
        private void WebReadCallback(IAsyncResult asyncResult)
        {
            WebReadState webReadState = (WebReadState)asyncResult.AsyncState;
            int          count        = webReadState.ReadStream.EndRead(asyncResult);

            if (count > 0)
            {
                webReadState.MemoryStream.Write(webReadState.Buffer, 0, count);
                try
                {
                    webReadState.ReadStream.BeginRead(webReadState.Buffer, 0, webReadState.Buffer.Length, new AsyncCallback(WebReadCallback), webReadState);
                }
                catch (WebException exp)
                {
                    this.Dispatcher.Invoke(DispatcherPriority.Render, new WebRequestErrorDelegate(WebRequestError), exp);
                }
            }
            else
            {
                this.Dispatcher.Invoke(DispatcherPriority.Render, new WebRequestFinishedDelegate(WebRequestFinished), webReadState.MemoryStream);
            }
        }