示例#1
0
        private void Startup( )
        {
            EndInvoke(_ar);

            _pbProgress.Visible = _fileName != null;

            Application.DoEvents();
            _canceled = false;

            WebResponse response = null;
            FileStream  fs       = null;
            Stream      strm     = null;

            try
            {
                WebRequest request;

                int length;

                if (_url != null)
                {
                    request = WebRequest.Create(_url);
                    if (request.Proxy != null)
                    {
                        request.Proxy.Credentials = CredentialCache.DefaultCredentials;
                    }

                    // reduce the timeout to 20sec
                    request.Timeout = 20000;
                    // you cannot cancel during GetResponse()
                    _btnCancel.Enabled = false;
                    response           = request.GetResponse();
                    _btnCancel.Enabled = true;
                    strm   = response.GetResponseStream();
                    length = -1;
                }
                else
                {
                    fs     = File.OpenRead(_fileName);
                    length = (int)fs.Length;
                    strm   = fs;
                    _pbProgress.Maximum = length;
                }

                const int bufferSize = 1024;
                byte[]    buffer     = new byte[bufferSize];

                int read;
                int count = 0;

                _codecs.StartFeedLoad(0, CodecsLoadByteOrder.BgrOrGray);

                do
                {
                    Application.DoEvents();

                    if (!_canceled)
                    {
                        read = strm.Read(buffer, 0, bufferSize);
                        if (read > 0)
                        {
                            Application.DoEvents();

                            if (!_canceled)
                            {
                                _codecs.FeedLoad(buffer, 0, read);
                            }

                            count += read;
                        }
                    }
                    else
                    {
                        read = 0;
                    }

                    if (!_canceled)
                    {
                        if (_url != null)
                        {
                            _lblProgress.Text = string.Format("Downloading {0} bytes", count);
                        }
                        else
                        {
                            _lblProgress.Text = string.Format("Loading {0} of {1} bytes", count, length);
                            _pbProgress.Value = count;
                        }
                    }
                }while(read > 0 && !_canceled);


                if (!_canceled)
                {
                    Image = _codecs.StopFeedLoad();
                    Close(DialogResult.OK);
                }
                else
                {
                    _codecs.CancelFeedLoad();
                    Image = null;
                    Close(DialogResult.Cancel);
                }
            }
            catch (Exception ex)
            {
                Messager.ShowError(this, ex);
                Close(DialogResult.Cancel);
            }
            finally
            {
                if (strm != null)
                {
                    strm.Close();
                }
                if (response != null)
                {
                    response.Close();
                }
                if (fs != null)
                {
                    fs.Close();
                }
            }
        }