Exemplo n.º 1
0
        public BitmapSource GetImage(string fullPath)
        {
            if (!_cache.TryGetValue(fullPath, out var result))
            {
                if (!File.Exists(fullPath))
                {
                    return(null);
                }

                try
                {
                    var image = GraphicsUtils.Downsize(fullPath, MaxImageWidth, MaxImageHeight);
                    if (image != null)
                    {
                        result = new ImageAndLastUsed {
                            BitmapImage = image, LastUsedUtc = DateTime.UtcNow
                        };

                        _cache.AddOrUpdate(
                            fullPath,
                            result,
                            (s, value) =>
                        {
                            value.LastUsedUtc = DateTime.UtcNow;
                            return(value);
                        });
                    }

                    if (_cache.Count > MaxItemCount)
                    {
                        RemoveOldImages();
                    }
                }
                catch (Exception ex)
                {
                    Log.Logger.Error(ex, $"Could not cache image {fullPath}");
                }
            }
            else
            {
                Log.Logger.Debug($"Image in cache: {fullPath}");
                result.LastUsedUtc = DateTime.UtcNow;
            }

            return(result?.BitmapImage);
        }
Exemplo n.º 2
0
        private static ImageSource CreateThumbnailImage(Bitmap image, int thumbnailWidth, int thumbnailHeight)
        {
            var bmp = GraphicsUtils.BitmapToBitmapImage(image);

            return(GraphicsUtils.Downsize(bmp, thumbnailWidth, thumbnailHeight));
        }