Beispiel #1
0
        BitmapSource TryGetImage(string uriString, InternalImageOptions options)
        {
            if (uriString == null)
            {
                return(null);
            }

            var           key = new ImageKey(uriString, options);
            WeakReference weakImage;
            BitmapSource  image;

            if (imageCache.TryGetValue(key, out weakImage))
            {
                image = weakImage.Target as BitmapSource;
                if (image != null)
                {
                    return(image);
                }
            }

            image = TryLoadImage(uriString, options.PhysicalSize);
            if (image == null)
            {
                return(null);
            }

            if (options.BackgroundColor != null)
            {
                image = ThemedImageCreator.CreateThemedBitmapSource(image, options.BackgroundColor.Value, isHighContrast);
            }
            imageCache[key] = new WeakReference(image);
            return(image);
        }
Beispiel #2
0
 public ImageKey(string uri, InternalImageOptions options)
 {
     this.uri     = uri;
     this.options = options;
 }
Beispiel #3
0
        public BitmapSource GetImage(ImageReference imageReference, ImageOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            if (imageReference.Name == null)
            {
                return(null);
            }

            var internalOptions = new InternalImageOptions();

            internalOptions.BackgroundColor = options.BackgroundColor ?? GetColor(options.BackgroundBrush);
            var logicalSize = options.LogicalSize;

            if (logicalSize == new Size(0, 0))
            {
                logicalSize = new Size(16, 16);
            }
            internalOptions.Dpi = GetDpi(options.DpiObject, options.Dpi);
            if (options.Zoom != new Size(0, 0))
            {
                internalOptions.Dpi = new Size(internalOptions.Dpi.Width * options.Zoom.Width, internalOptions.Dpi.Height * options.Zoom.Height);
            }
            internalOptions.Dpi = Round(internalOptions.Dpi);
            if (internalOptions.Dpi.Width == 0 || internalOptions.Dpi.Height == 0)
            {
                return(null);
            }
            internalOptions.PhysicalSize = Round(new Size(logicalSize.Width * internalOptions.Dpi.Width / 96, logicalSize.Height * internalOptions.Dpi.Height / 96));

            if (internalOptions.PhysicalSize.Width == 0 || internalOptions.PhysicalSize.Height == 0)
            {
                return(null);
            }

            if (imageReference.Assembly != null)
            {
                var name = imageReference.Name;
                foreach (var provider in GetProviders(imageReference.Assembly))
                {
                    var infos = provider.Value.GetImageSourceInfos(name);
                    if (infos == null)
                    {
                        continue;
                    }

                    var infoList = new List <ImageSourceInfo>(infos);
                    infoList.Sort((a, b) => {
                        if (a.Size == b.Size)
                        {
                            return(0);
                        }

                        // Try exact size first
                        if ((a.Size == internalOptions.PhysicalSize) != (b.Size == internalOptions.PhysicalSize))
                        {
                            return(a.Size == internalOptions.PhysicalSize ? -1 : 1);
                        }

                        // Try any-size (xaml images)
                        if ((a.Size == ImageSourceInfo.AnySize) != (b.Size == ImageSourceInfo.AnySize))
                        {
                            return(a.Size == ImageSourceInfo.AnySize ? -1 : 1);
                        }

                        // Closest size (using height)
                        if (a.Size.Height >= internalOptions.PhysicalSize.Height)
                        {
                            if (b.Size.Height < internalOptions.PhysicalSize.Height)
                            {
                                return(-1);
                            }
                            return(a.Size.Height.CompareTo(b.Size.Height));
                        }
                        else
                        {
                            if (b.Size.Height >= internalOptions.PhysicalSize.Height)
                            {
                                return(1);
                            }
                            return(b.Size.Height.CompareTo(a.Size.Height));
                        }
                    });

                    foreach (var info in infoList)
                    {
                        var bitmapSource = TryGetImage(info.Uri, internalOptions);
                        if (bitmapSource != null)
                        {
                            return(bitmapSource);
                        }
                    }

                    return(null);
                }
                return(null);
            }
            else
            {
                return(TryGetImage(imageReference.Name, internalOptions));
            }
        }