public void BitmapLoadPropertiesEquabilityTest()
        {
            var np1 = new BitmapLoadProperties(1, 1, new DpiScale(1, 1));
            var np2 = new BitmapLoadProperties(1, 1, new DpiScale(1, 1));
            var np3 = new BitmapLoadProperties(2, 2, new DpiScale(2, 2));

            Assert.AreEqual(np1, np2);
            Assert.IsTrue(np1 == np2);

            Assert.AreNotEqual(np1, np3);
            Assert.IsTrue(np1 != np3);
        }
Exemple #2
0
        public void BitmapLoadPropertiesEquabilityTest()
        {
            var np1 = new BitmapLoadProperties(1, 1, new DpiScale(1, 1));
            var np2 = new BitmapLoadProperties(1, 1, new DpiScale(1, 1));
            var np3 = new BitmapLoadProperties(2, 2, new DpiScale(2, 2));
            BitmapLoadProperties np4 = null;
            var np5 = new BitmapLoadProperties(2, 2, new DpiScale(2, 2), ImageLoadScaling.BitmapDotNet);
            var np6 = new BitmapLoadProperties(2, 2, new DpiScale(2, 2), ImageLoadScaling.None);

            Assert.AreEqual(np1, np2);
            Assert.IsTrue(np1 == np2);
            Assert.IsTrue(np4 == null);
            Assert.IsTrue(np3 != null);
            Assert.IsTrue(np3 == np5);
            Assert.IsTrue(np5 != np6);

            Assert.AreNotEqual(np1, np3);
            Assert.IsTrue(np1 != np3);
        }
        public static BitmapImage GetResourceImage(string resourceKey, bool cached, BitmapLoadProperties loadProperties = null)
        {
            if (cached && Cache.TryGet(resourceKey, out var image))
            {
                BitmapLoadProperties existingMetadata = null;
                if (image.Metadata.TryGetValue(btmpPropsFld, out object metaValue))
                {
                    existingMetadata = (BitmapLoadProperties)metaValue;
                }

                if (existingMetadata?.MaxDecodePixelWidth == loadProperties?.MaxDecodePixelWidth)
                {
                    return(image.CacheObject as BitmapImage);
                }
                else
                {
                    Cache.TryRemove(resourceKey);
                }
            }

            var resource = ResourceProvider.GetResource(resourceKey) as BitmapImage;

            if (loadProperties?.MaxDecodePixelWidth > 0 && resource?.PixelWidth > loadProperties?.MaxDecodePixelWidth)
            {
                resource = resource.GetClone(loadProperties);
            }

            if (cached)
            {
                Cache.TryAdd(resourceKey, resource, resource.GetSizeInMemory(),
                             new Dictionary <string, object>
                {
                    { btmpPropsFld, loadProperties }
                });
            }

            return(resource);
        }
        public static BitmapImage GetImage(string source, bool cached, BitmapLoadProperties loadProperties = null)
        {
            if (DesignerTools.IsInDesignMode)
            {
                cached = false;
            }

            if (source.IsNullOrEmpty())
            {
                return(null);
            }

            if (cached && Cache.TryGet(source, out var image))
            {
                BitmapLoadProperties existingMetadata = null;
                if (image.Metadata.TryGetValue(btmpPropsFld, out object metaValue))
                {
                    existingMetadata = (BitmapLoadProperties)metaValue;
                }

                if (existingMetadata == loadProperties)
                {
                    return(image.CacheObject as BitmapImage);
                }
                else
                {
                    Cache.TryRemove(source);
                }
            }

            if (source.StartsWith("resources:") || source.StartsWith("pack://"))
            {
                try
                {
                    var imagePath = source;
                    if (source.StartsWith("resources:"))
                    {
                        imagePath = source.Replace("resources:", "pack://application:,,,");
                    }

                    var streamInfo = Application.GetResourceStream(new Uri(imagePath));
                    using (var stream = streamInfo.Stream)
                    {
                        var imageData = BitmapExtensions.BitmapFromStream(stream, loadProperties);
                        if (imageData != null)
                        {
                            if (cached)
                            {
                                Cache.TryAdd(source, imageData, imageData.GetSizeInMemory(),
                                             new Dictionary <string, object>
                                {
                                    { btmpPropsFld, loadProperties }
                                });
                            }

                            return(imageData);
                        }
                    }
                }
                catch (Exception e) when(!PlayniteEnvironment.ThrowAllErrors)
                {
                    logger.Error(e, "Failed to create bitmap from resources " + source);
                    return(null);
                }
            }

            if (StringExtensions.IsHttpUrl(source))
            {
                try
                {
                    var cachedFile = HttpFileCache.GetWebFile(source);
                    if (string.IsNullOrEmpty(cachedFile))
                    {
                        logger.Warn("Web file not found: " + source);
                        return(null);
                    }

                    return(BitmapExtensions.BitmapFromFile(cachedFile, loadProperties));
                }
                catch (Exception exc) when(!PlayniteEnvironment.ThrowAllErrors)
                {
                    logger.Error(exc, $"Failed to create bitmap from {source} file.");
                    return(null);
                }
            }

            if (File.Exists(source))
            {
                try
                {
                    var imageData = BitmapExtensions.BitmapFromFile(source, loadProperties);
                    if (imageData != null)
                    {
                        if (cached)
                        {
                            Cache.TryAdd(source, imageData, imageData.GetSizeInMemory(),
                                         new Dictionary <string, object>
                            {
                                { btmpPropsFld, loadProperties }
                            });
                        }

                        return(imageData);
                    }
                }
                catch (Exception e) when(!PlayniteEnvironment.ThrowAllErrors)
                {
                    logger.Error(e, "Failed to create bitmap from " + source);
                    return(null);
                }
            }

            try
            {
                if (database == null)
                {
                    logger.Error("Cannot load database image, database not found.");
                    return(null);
                }

                try
                {
                    var imageData = database.GetFileAsImage(source, loadProperties);
                    if (imageData == null)
                    {
                        logger.Warn("Image not found in database: " + source);
                        return(null);
                    }
                    else
                    {
                        if (cached)
                        {
                            Cache.TryAdd(source, imageData, imageData.GetSizeInMemory(),
                                         new Dictionary <string, object>
                            {
                                { btmpPropsFld, loadProperties }
                            });
                        }

                        return(imageData);
                    }
                }
                catch (Exception exc) when(!PlayniteEnvironment.ThrowAllErrors)
                {
                    logger.Error(exc, $"Failed to get bitmap from {source} database file.");
                    return(null);
                }
            }
            catch (Exception exc) when(!PlayniteEnvironment.ThrowAllErrors)
            {
                logger.Error(exc, "Failed to load image from database.");
                return(null);
            }
        }