예제 #1
0
        /// <summary>
        /// Gets the cached bitmap image representing the card object. Will query the API if it has not been loaded, otherwise gets the cached version.
        /// </summary>
        public static async Task <Bitmap> GetImageAsync(string imageUri, IProgress <string> reporter)
        {
            if (string.IsNullOrWhiteSpace(imageUri))
            {
                throw new ArgumentNullException(nameof(imageUri), @"Image request URI cannot be null or empty/whitespace. Consumer must check before using this method.");
            }

            while (IsCacheBeingAccessed)
            {
                await Task.Delay(1);
            }

            IsCacheBeingAccessed = true;

            if (ImageCache.ContainsKey(imageUri))
            {
                await Task.Delay(1);

                IsCacheBeingAccessed = false;
                return(ImageCache[imageUri]);
            }

            Bitmap bitmap = await ScryfallService.GetImageAsync(imageUri, reporter);

            ImageCache.Add(imageUri, bitmap);

            if (ImageCache.Count > 100)
            {
                ImageCache.Remove(ImageCache.First().Key);
            }

            IsCacheBeingAccessed = false;
            return(bitmap);
        }
예제 #2
0
        public async Task LoadImagesFromCards()
        {
            for (var i = 0; i < Cards.Count; i++)
            {
                Card card = Cards[i];
                if (string.IsNullOrWhiteSpace(card?.ImageUris?.Small))
                {
                    continue;
                }

                BitmapSource image = ImageHelper.LoadBitmap(await ScryfallService.GetImageAsync(card.ImageUris.BorderCrop, Reporter));

                if (image != null)
                {
                    Images.Add(new ImageWithIndex {
                        Image = image, Index = i
                    });
                }
            }
        }