Exemplo n.º 1
0
        /// <summary>
        /// Download the remote or local image located at the specified url.
        /// </summary>
        public void DownloadData(Uri imageUrl)
        {
            // is it a local path?
            if (imageUrl.IsFile)
            {
                // replace string %20 in LocalPath by daviderapicavoli (patch #15938)
                String localPath = Uri.UnescapeDataString(imageUrl.LocalPath);

                try
                {
                    // just read the picture from the file system
                    imageInfo.RawData = File.ReadAllBytes(localPath);
                }
                catch (IOException exc)
                {
                    if (Logging.On) Logging.PrintError("ImageDownloader.DownloadData(\"" + localPath + "\")", exc);
                }
                catch (SystemException exc)
                {
                    if (Logging.On) Logging.PrintError("ImageDownloader.DownloadData(\"" + localPath + "\")", exc);

                    if (exc is UnauthorizedAccessException || exc is System.Security.SecurityException || exc is NotSupportedException)
                        return;
                    throw;
                }

                return;
            }

            // data inline, encoded in base64
            if (imageUrl.Scheme == "data")
            {
                DataUri dataUri = DataUri.Parse(imageUrl.OriginalString);
                DownloadData(dataUri);
                return;
            }

            System.Net.WebClient webClient = new WebClientEx(proxy);
            try
            {
                imageInfo.RawData = webClient.DownloadData(imageUrl);

                // For requested url with no filename, we need to read the media mime type if provided
                imageInfo.Type = InspectMimeType(webClient);
            }
            catch (System.Net.WebException exc)
            {
                if (Logging.On) Logging.PrintError("ImageDownloader.DownloadData(\"" + imageUrl.AbsoluteUri + "\")", exc);
            }
        }
        //____________________________________________________________________
        //
        // Public Functionality

        #region DownloadData

        /// <summary>
        /// Download the remote or local image located at the specified url.
        /// </summary>
        public void DownloadData(Uri imageUrl)
        {
            // is it a local path?
            if (imageUrl.IsFile)
            {
                // replace string %20 in LocalPath by daviderapicavoli (patch #15938)
                String localPath = Uri.UnescapeDataString(imageUrl.LocalPath);

                try
                {
                    // just read the picture from the file system
                    imageInfo.RawData = File.ReadAllBytes(localPath);
                }
                catch (IOException exc)
                {
                    if (Logging.On)
                    {
                        Logging.PrintError("ImageDownloader.DownloadData(\"" + localPath + "\")", exc);
                    }
                }
                catch (SystemException exc)
                {
                    if (Logging.On)
                    {
                        Logging.PrintError("ImageDownloader.DownloadData(\"" + localPath + "\")", exc);
                    }

                    if (exc is UnauthorizedAccessException || exc is System.Security.SecurityException || exc is NotSupportedException)
                    {
                        return;
                    }
                    throw;
                }

                return;
            }

            // data inline, encoded in base64
            if (imageUrl.Scheme == "data")
            {
                DataUri dataUri = DataUri.Parse(imageUrl.OriginalString);
                DownloadData(dataUri);
                return;
            }

            System.Net.WebClient webClient = new WebClientEx(proxy);
            try
            {
                imageInfo.RawData = webClient.DownloadData(imageUrl);

                // For requested url with no filename, we need to read the media mime type if provided
                imageInfo.Type = InspectMimeType(webClient);
            }
            catch (System.Net.WebException exc)
            {
                if (Logging.On)
                {
                    Logging.PrintError("ImageDownloader.DownloadData(\"" + imageUrl.AbsoluteUri + "\")", exc);
                }
            }
        }