public void DownloadImage(string sourceUri, string targetFile)
        {
            try
            {
                _sourceURI = sourceUri;
                // Create a HttpWebrequest object to the desired URL.
                webRequest = (HttpWebRequest) WebRequest.Create(sourceUri);

                var myRequestState = new RequestState
                {
                    request = webRequest,
                    fileDownload = true,
                    destinationFile = targetFile,
                    downloaderObject = this
                };

                IAsyncResult result =
                    (IAsyncResult) webRequest.BeginGetResponse(new
                        AsyncCallback(RespCallback), myRequestState);

                ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle,
                    new WaitOrTimerCallback(TimeoutCallback), this,
                    _defaultTimeout, true);

                allDone.WaitOne();

                if (myRequestState.response != null)
                    myRequestState.response.Close();

                Debug.Print("Cancelled: " + this.cancelled + " Timeout: " + timeout);

                if (timeout || cancelled)
                {
                    if (myRequestState.fileDownload && myRequestState.fileStream != null)
                    {
                        myRequestState.fileStream.Close();
                        if (File.Exists(myRequestState.destinationFile))
                            File.Delete(myRequestState.destinationFile);
                    }
                }

                if (DownloadCompleted != null)
                {
                    DownloadCompleted(this,
                        new HttpDownloadEventArgs(this.timeout, this.cancelled)
                        {
                            Succeeded = !timeout && !cancelled,
                            URI = _sourceURI
                        });
                }
            }
            catch (WebException e)
            {
                Console.WriteLine("\nWeb Exception raised!");
                Console.WriteLine("\nMessage:{0}", e.Message);
                Console.WriteLine("\nStatus:{0}", e.Status);
            }
            catch (Exception e)
            {
                Console.WriteLine("\nMain Exception raised!");
                Console.WriteLine("Source :{0} ", e.Source);
                Console.WriteLine("Message :{0} ", e.Message);

                if (DownloadCompleted != null)
                    DownloadCompleted(this,
                        new HttpDownloadEventArgs(this.timeout, this.cancelled) {Succeeded = false, URI = _sourceURI});
            }
        }
        public void RequestImageSize(string sourceUri, int maxLogoSize)
        {
            try
            {
                _sourceURI = sourceUri;
                // Create a HttpWebrequest object to the desired URL.
                webRequest = (HttpWebRequest)WebRequest.Create(sourceUri);

                // Create an instance of the RequestState and assign the previous myHttpWebRequest
                // object to it's request field.

                var myRequestState = new RequestState
                {
                    request          = webRequest,
                    fileDownload     = false,
                    downloaderObject = this,
                    MaxLogoSize      = maxLogoSize
                };

                webRequest.Method = "HEAD";
                IAsyncResult result =
                    (IAsyncResult)webRequest.BeginGetResponse(new
                                                              AsyncCallback(RespCallback), myRequestState);

                ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle,
                                                       new WaitOrTimerCallback(TimeoutCallback), this,
                                                       _defaultTimeout, true);

                allDone.WaitOne();

                if (myRequestState.response != null)
                {
                    myRequestState.response.Close();
                }

                Debug.Print("Cancelled: " + this.cancelled + " Timeout: " + timeout);

                if (ImageSizeReceived != null)
                {
                    ImageSizeReceived(this, new HttpDownloadEventArgs(this.timeout, this.cancelled)
                    {
                        Succeeded = !timeout && !cancelled
                    });
                }
            }
            catch (WebException e)
            {
                Console.WriteLine("\nWeb Exception raised!");
                Console.WriteLine("\nMessage:{0}", e.Message);
                Console.WriteLine("\nStatus:{0}", e.Status);
            }
            catch (Exception e)
            {
                Console.WriteLine("\nMain Exception raised!");
                Console.WriteLine("Source :{0} ", e.Source);
                Console.WriteLine("Message :{0} ", e.Message);

                if (ImageSizeReceived != null)
                {
                    ImageSizeReceived(this,
                                      new HttpDownloadEventArgs(this.timeout, this.cancelled)
                    {
                        Succeeded = false
                    });
                }
            }
        }
        public void RequestImageSize(string sourceUri, int maxLogoSize)
        {
            try
            {
                _sourceURI = sourceUri;
                // Create a HttpWebrequest object to the desired URL.
                webRequest = (HttpWebRequest)WebRequest.Create(sourceUri);

                // Create an instance of the RequestState and assign the previous myHttpWebRequest
                // object to it's request field.

                var myRequestState = new RequestState
                {
                    request = webRequest,
                    fileDownload = false,
                    downloaderObject = this,
                    MaxLogoSize = maxLogoSize
                };

                webRequest.Method = "HEAD";
                IAsyncResult result =
                    (IAsyncResult)webRequest.BeginGetResponse(new
                        AsyncCallback(RespCallback), myRequestState);

                ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle,
                    new WaitOrTimerCallback(TimeoutCallback), this,
                    _defaultTimeout, true);

                allDone.WaitOne();

                if (myRequestState.response != null)
                    myRequestState.response.Close();

                Debug.Print("Cancelled: " + this.cancelled + " Timeout: " + timeout);

                if (ImageSizeReceived != null)
                    ImageSizeReceived(this, new HttpDownloadEventArgs(this.timeout, this.cancelled) { Succeeded = !timeout && !cancelled });
            }
            catch (WebException e)
            {
                Console.WriteLine("\nWeb Exception raised!");
                Console.WriteLine("\nMessage:{0}", e.Message);
                Console.WriteLine("\nStatus:{0}", e.Status);
            }
            catch (Exception e)
            {
                Console.WriteLine("\nMain Exception raised!");
                Console.WriteLine("Source :{0} ", e.Source);
                Console.WriteLine("Message :{0} ", e.Message);

                if (ImageSizeReceived != null)
                    ImageSizeReceived(this,
                        new HttpDownloadEventArgs(this.timeout, this.cancelled) { Succeeded = false });
            }
        }
        public void DownloadImage(string sourceUri, string targetFile)
        {
            try
            {
                _sourceURI = sourceUri;
                // Create a HttpWebrequest object to the desired URL.
                webRequest = (HttpWebRequest)WebRequest.Create(sourceUri);

                var myRequestState = new RequestState
                {
                    request          = webRequest,
                    fileDownload     = true,
                    destinationFile  = targetFile,
                    downloaderObject = this
                };

                IAsyncResult result =
                    (IAsyncResult)webRequest.BeginGetResponse(new
                                                              AsyncCallback(RespCallback), myRequestState);

                ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle,
                                                       new WaitOrTimerCallback(TimeoutCallback), this,
                                                       _defaultTimeout, true);

                allDone.WaitOne();

                if (myRequestState.response != null)
                {
                    myRequestState.response.Close();
                }

                Debug.Print("Cancelled: " + this.cancelled + " Timeout: " + timeout);

                if (timeout || cancelled)
                {
                    if (myRequestState.fileDownload && myRequestState.fileStream != null)
                    {
                        myRequestState.fileStream.Close();
                        if (File.Exists(myRequestState.destinationFile))
                        {
                            File.Delete(myRequestState.destinationFile);
                        }
                    }
                }

                if (DownloadCompleted != null)
                {
                    DownloadCompleted(this,
                                      new HttpDownloadEventArgs(this.timeout, this.cancelled)
                    {
                        Succeeded = !timeout && !cancelled,
                        URI       = _sourceURI
                    });
                }
            }
            catch (WebException e)
            {
                Console.WriteLine("\nWeb Exception raised!");
                Console.WriteLine("\nMessage:{0}", e.Message);
                Console.WriteLine("\nStatus:{0}", e.Status);
            }
            catch (Exception e)
            {
                Console.WriteLine("\nMain Exception raised!");
                Console.WriteLine("Source :{0} ", e.Source);
                Console.WriteLine("Message :{0} ", e.Message);

                if (DownloadCompleted != null)
                {
                    DownloadCompleted(this,
                                      new HttpDownloadEventArgs(this.timeout, this.cancelled)
                    {
                        Succeeded = false, URI = _sourceURI
                    });
                }
            }
        }