Exemple #1
0
        private byte[] Download(GalleryAgent agent, ImageViewState imageViewState, GalleryImagePath imagePath, GalleryParameterValues values)
        {
            using (var source = new CancellationTokenSource())
            {
                var cancelSources = this.CancelSources;

                try
                {
                    lock (cancelSources)
                    {
                        this.ThrowIfCancelRequested();
                        cancelSources.Add(source);
                    }

                    var downloadRequest = agent.CreateImageRequest(imageViewState.View, imagePath, values);

                    using (var response = agent.Explorer.Request(downloadRequest, source))
                    {
                        if (response.StatusCode == HttpStatusCode.ServiceUnavailable)
                        {
                            throw new HttpStatusCodeException(response.StatusCode);
                        }

                        imageViewState.Length   = response.ContentLength;
                        imageViewState.Position = 0L;

                        using (var responseStream = response.ReadAsStream())
                        {
                            return(this.Download(imageViewState, responseStream));
                        }
                    }
                }
                finally
                {
                    lock (cancelSources)
                    {
                        cancelSources.Remove(source);
                    }
                }
            }
        }
Exemple #2
0
        public static byte[] GetFirstThumbnail(this GalleryInfo info, GalleryAgent agent)
        {
            foreach (var url in info.ThumbnailUrls)
            {
                if (string.IsNullOrWhiteSpace(url) == false)
                {
                    try
                    {
                        var bytes = agent.GetGalleryThumbnail(url);

                        using (var image = new MagickImage(bytes))
                        {
                            return(bytes);
                        }
                    }
                    catch
                    {
                    }
                }
            }

            return(null);
        }