Example #1
0
        public static object GetIcon(string iconName, double imageHeight = 16, double imageWidth = 16)
        {
            if (iconName.IsNullOrEmpty())
            {
                return(null);
            }

            var resource = ResourceProvider.GetResource(iconName);

            if (resource != null)
            {
                if (resource is string stringIcon)
                {
                    return(Images.GetImageFromFile(ThemeFile.GetFilePath(stringIcon), BitmapScalingMode.Fant, imageHeight, imageWidth));
                }
                else if (resource is BitmapImage bitmap)
                {
                    var image = new System.Windows.Controls.Image()
                    {
                        Source = bitmap
                    };
                    RenderOptions.SetBitmapScalingMode(image, RenderOptions.GetBitmapScalingMode(bitmap));
                    return(image);
                }
                else if (resource is TextBlock textIcon)
                {
                    var text = new TextBlock
                    {
                        Text       = textIcon.Text,
                        FontFamily = textIcon.FontFamily,
                        FontStyle  = textIcon.FontStyle
                    };

                    if (textIcon.ReadLocalValue(TextBlock.ForegroundProperty) != DependencyProperty.UnsetValue)
                    {
                        text.Foreground = textIcon.Foreground;
                    }

                    return(text);
                }
            }
            else if (System.IO.File.Exists(iconName))
            {
                return(BitmapExtensions.BitmapFromFile(iconName)?.ToImage());
            }
            else
            {
                var themeFile = ThemeFile.GetFilePath(iconName);
                if (themeFile != null)
                {
                    return(Images.GetImageFromFile(themeFile, BitmapScalingMode.Fant, imageHeight, imageWidth));
                }
            }

            return(null);
        }
        private void ButtonSelectImage_Click(object sender, RoutedEventArgs e)
        {
            var path = Dialogs.SelectImageFile(this);

            if (!string.IsNullOrEmpty(path))
            {
                ImageImage.Source = BitmapExtensions.BitmapFromFile(path);
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(ImageImage.Tag.ToString()));
            }
        }
Example #3
0
        private void PART_AddCustomIcon_Click(object sender, RoutedEventArgs e)
        {
            var result = API.Instance.Dialogs.SelectIconFile();

            if (!result.IsNullOrEmpty())
            {
                string PathDest = Path.Combine(plugin.GetPluginUserDataPath(), Path.GetFileName(result));
                FileSystem.CopyFile(result, PathDest);

                PART_IconCustom.Tag    = PathDest;
                PART_IconCustom.Source = BitmapExtensions.BitmapFromFile(PathDest);
            }

            PART_TextChanged(null, null);
        }
        private void ButtonExecIcon_Click(object sender, RoutedEventArgs e)
        {
            if (TempPlayTask == null || TempPlayTask.Type == GameTaskType.URL)
            {
                return;
            }

            var icon = SaveFileIconToTemp(TempPlayTask.Path);

            if (string.IsNullOrEmpty(icon))
            {
                return;
            }

            ImageIcon.Source = BitmapExtensions.BitmapFromFile(icon);
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(ImageIcon.Tag.ToString()));
        }
        private void PreviewGameData(IGame game)
        {
            var listConverter = new ListToStringConverter();
            var dateConverter = new NullableDateToStringConverter();

            TextName.Text = string.IsNullOrEmpty(game.Name) ? TextName.Text : game.Name;

            if (game.Developers != null && game.Developers.Count != 0)
            {
                TextDeveloper.Text = (string)listConverter.Convert(game.Developers, typeof(string), null, null);
            }

            if (game.Publishers != null && game.Publishers.Count != 0)
            {
                TextPublisher.Text = (string)listConverter.Convert(game.Publishers, typeof(string), null, null);
            }

            if (game.Genres != null && game.Genres.Count != 0)
            {
                TextGenres.Text = (string)listConverter.Convert(game.Genres, typeof(string), null, null);
            }

            TextReleaseDate.Text = (string)dateConverter.Convert(game.ReleaseDate, typeof(DateTime?), null, null);
            TextDescription.Text = string.IsNullOrEmpty(game.Description) ? TextName.Text : game.Description;

            if (game.Links != null)
            {
                TempLinks            = game.Links;
                CheckLinks.IsChecked = true;
            }

            if (!string.IsNullOrEmpty(game.Image))
            {
                var extension = Path.GetExtension(game.Image);
                var tempPath  = Path.Combine(Paths.TempPath, "tempimage" + extension);
                if (File.Exists(tempPath))
                {
                    File.Delete(tempPath);
                }

                Web.DownloadFile(game.Image, tempPath);
                ImageImage.Source = BitmapExtensions.BitmapFromFile(tempPath);
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(ImageImage.Tag.ToString()));
            }
        }
        private void ButtonSelectIcon_Click(object sender, RoutedEventArgs e)
        {
            var path = Dialogs.SelectIconFile(this);

            if (!string.IsNullOrEmpty(path))
            {
                if (path.EndsWith("exe", StringComparison.CurrentCultureIgnoreCase))
                {
                    path = SaveFileIconToTemp(path);

                    if (string.IsNullOrEmpty(path))
                    {
                        return;
                    }
                }

                ImageIcon.Source = BitmapExtensions.BitmapFromFile(path);
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(ImageIcon.Tag.ToString()));
            }
        }
Example #7
0
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
            {
                return(DependencyProperty.UnsetValue);
            }

            var imageId = (string)value;

            if (string.IsNullOrEmpty(imageId))
            {
                return(DependencyProperty.UnsetValue);
            }

            if (imageId.StartsWith("resources:"))
            {
                return(imageId.Replace("resources:", ""));
            }

            if (imageId.StartsWith("http", StringComparison.InvariantCultureIgnoreCase))
            {
                try
                {
                    var cachedFile = Web.GetCachedWebFile(imageId);
                    if (string.IsNullOrEmpty(cachedFile))
                    {
                        logger.Warn("Web file not found: " + imageId);
                        return(DependencyProperty.UnsetValue);
                    }

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

            if (File.Exists(imageId))
            {
                try
                {
                    return(BitmapExtensions.BitmapFromFile(imageId));
                }
                catch (Exception e) when(!PlayniteEnvironment.ThrowAllErrors)
                {
                    logger.Error(e, "Failed to create bitmap from " + imageId);
                    return(DependencyProperty.UnsetValue);
                }
            }

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

                try
                {
                    var imageData = Database.GetFileImage(imageId);
                    if (imageData == null)
                    {
                        logger.Warn("Image not found in database: " + imageId);
                        return(DependencyProperty.UnsetValue);
                    }
                    else
                    {
                        return(imageData);
                    }
                }
                catch (Exception exc) when(!PlayniteEnvironment.ThrowAllErrors)
                {
                    logger.Error(exc, $"Failed to get bitmap from {imageId} database file.");
                    return(DependencyProperty.UnsetValue);
                }
            }
            catch (Exception exc) when(!PlayniteEnvironment.ThrowAllErrors)
            {
                logger.Error(exc, "Failed to load image from database.");
                return(DependencyProperty.UnsetValue);
            }
        }
Example #8
0
        public static object GetImageFromSource(string source)
        {
            if (string.IsNullOrEmpty(source))
            {
                return(null);
            }

            var imageId = source;

            if (string.IsNullOrEmpty(imageId))
            {
                return(null);
            }

            if (imageId.StartsWith("resources:"))
            {
                return(imageId.Replace("resources:", ""));
            }

            if (imageId.StartsWith("http", StringComparison.InvariantCultureIgnoreCase))
            {
                try
                {
                    var cachedFile = HttpDownloader.GetCachedWebFile(imageId);
                    if (string.IsNullOrEmpty(cachedFile))
                    {
                        logger.Warn("Web file not found: " + imageId);
                        return(null);
                    }

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

            if (File.Exists(imageId))
            {
                try
                {
                    return(BitmapExtensions.BitmapFromFile(imageId));
                }
                catch (Exception e) when(!PlayniteEnvironment.ThrowAllErrors)
                {
                    logger.Error(e, "Failed to create bitmap from " + imageId);
                    return(null);
                }
            }

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

                try
                {
                    var imageData = Database.GetFileImage(imageId);
                    if (imageData == null)
                    {
                        logger.Warn("Image not found in database: " + imageId);
                        return(null);
                    }
                    else
                    {
                        return(imageData);
                    }
                }
                catch (Exception exc) when(!PlayniteEnvironment.ThrowAllErrors)
                {
                    logger.Error(exc, $"Failed to get bitmap from {imageId} database file.");
                    return(null);
                }
            }
            catch (Exception exc) when(!PlayniteEnvironment.ThrowAllErrors)
            {
                logger.Error(exc, "Failed to load image from database.");
                return(null);
            }
        }
        public static object GetImageFromSource(string source)
        {
            if (string.IsNullOrEmpty(source))
            {
                return(null);
            }

            var imageId = source;

            if (string.IsNullOrEmpty(imageId))
            {
                return(null);
            }

            if (imageId.StartsWith("resources:"))
            {
                if (cache.TryGet(imageId, out var image))
                {
                    return(image);
                }
                else
                {
                    try
                    {
                        var imagePath = imageId.Replace("resources:", "pack://application:,,,");
                        var imageData = BitmapExtensions.BitmapFromFile(imagePath);
                        cache.TryAdd(imageId, imageData, imageData.GetSizeInMemory());
                        return(imageData);
                    }
                    catch (Exception e) when(!PlayniteEnvironment.ThrowAllErrors)
                    {
                        logger.Error(e, "Failed to create bitmap from resources " + imageId);
                        return(null);
                    }
                }
            }

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

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

            if (File.Exists(imageId))
            {
                try
                {
                    return(BitmapExtensions.BitmapFromFile(imageId));
                }
                catch (Exception e) when(!PlayniteEnvironment.ThrowAllErrors)
                {
                    logger.Error(e, "Failed to create bitmap from " + imageId);
                    return(null);
                }
            }

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

                try
                {
                    if (cache.TryGet(imageId, out var image))
                    {
                        return(image);
                    }

                    var imageData = database.GetFileAsImage(imageId);
                    if (imageData == null)
                    {
                        logger.Warn("Image not found in database: " + imageId);
                        return(null);
                    }
                    else
                    {
                        cache.TryAdd(imageId, imageData, imageData.GetSizeInMemory());
                        return(imageData);
                    }
                }
                catch (Exception exc) when(!PlayniteEnvironment.ThrowAllErrors)
                {
                    logger.Error(exc, $"Failed to get bitmap from {imageId} database file.");
                    return(null);
                }
            }
            catch (Exception exc) when(!PlayniteEnvironment.ThrowAllErrors)
            {
                logger.Error(exc, "Failed to load image from database.");
                return(null);
            }
        }