public static ImageSource GetOrCreateImage(string imageName) { if (string.IsNullOrEmpty(imageName)) { return(default(ImageSource)); } if (!ImageSourceCache.ContainsKey(imageName)) { ImageSourceCache[imageName] = ImageSource.FromFile(imageName); } return(ImageSourceCache[imageName]); }
public static ImageSource GetImage(FileModel fileModel, IconSize iconSize) { if (fileModel.FullPath == FileSystemHelper.ComputerPath) { return(iconSize == IconSize.Small ? ComputerSmall : ComputerMedium); } if (fileModel.FullPath == FileSystemHelper.QuickAccessPath) { return(iconSize == IconSize.Small ? QuickAccessSmall : QuickAccessMedium); } string key = GetKey(fileModel, iconSize); if (ImageSourceCache.TryGetValue(key, out ImageSource imageSource)) { return(imageSource); } switch (iconSize) { case IconSize.Small: imageSource = GetImage(fileModel.FullPath, 16); break; case IconSize.Medium: imageSource = GetImage(fileModel.FullPath, 32); break; case IconSize.Large: imageSource = GetImage(fileModel.FullPath, 128); break; case IconSize.ExtraLarge: imageSource = GetImage(fileModel.FullPath, 256); break; } if (imageSource != null) { ImageSourceCache.TryAdd(key, imageSource); } return(imageSource); }
public static SKBitmapImageSource Load(string source, Aspect aspect, double width, double height) { // No image needed? if (string.IsNullOrWhiteSpace(source) || width <= 0 || height <= 0) { return(null); } // Read the image string key = GetKey(source); SkiaSharp.Extended.Svg.SKSvg svg = LoadSVG(key); CalculateBounds(true, svg, aspect, width, height, out SKMatrix drawMatrix, out SKSizeI pixelSize); // Check the cache if (ImageSourceCache.TryGetValue(key, out Tuple <SKSizeI, SKBitmapImageSource> cacheEntry)) { if (cacheEntry.Item1.Width >= pixelSize.Width * (1 - CacheTolerance) && cacheEntry.Item1.Height >= pixelSize.Height * (1 - CacheTolerance)) { // Already cached return(cacheEntry.Item2); } else { // Dispose/remove current entry cacheEntry.Item2.Bitmap?.Dispose(); ImageSourceCache.Remove(key); } } // Convert to an SKBitmapImageSource using (SKImage image = SKImage.FromPicture(svg.Picture, pixelSize, drawMatrix)) { SKBitmapImageSource imageSource = new SKBitmapImageSource() { Bitmap = SKBitmap.FromImage(image) }; ImageSourceCache.Add(key, Tuple.Create(pixelSize, imageSource)); return(imageSource); } }
public static ImageSource GetImage(FileModel fileModel, IconSize iconSize) { string path = Windows; if (fileModel.IsDirectory) { if (fileModel.IsRoot) { switch (fileModel.FullPath) { case FileSystemHelper.QuickAccessPath: path = KnownFolders.QuickAccess; break; case FileSystemHelper.ComputerPath: path = KnownFolders.Computer; break; case FileSystemHelper.NetworkPath: path = KnownFolders.Network; break; } if (fileModel.ParentPath == FileSystemHelper.NetworkPath) { path = KnownFolders.Computer; } } else if (FileSystemHelper.IsNetworkShare(fileModel.FullPath)) { path = @"\\a\b"; } else if (fileModel.IsDrive || fileModel.ParentPath.OrdinalEquals(UserProfile)) { path = fileModel.FullPath; } } else { if (fileModel.Extension.OrdinalEquals(".exe") || fileModel.Extension.OrdinalEquals(".ico") || fileModel.Extension.OrdinalEquals(".lnk") || fileModel.Extension.OrdinalEquals(".cur")) { path = fileModel.FullPath; } else { path = fileModel.Extension; } } string key = String.Format(KeyFormat, path, iconSize); if (ImageSourceCache.TryGetValue(key, out ImageSource imageSource)) { return(imageSource); } using (ImageKeyLockProvider.Lock(key)) { if (ImageSourceCache.TryGetValue(key, out imageSource)) { return(imageSource); } imageSource = SafeNativeMethods.GetIcon(path, iconSize.ToSHIL()); if (imageSource != null) { ImageSourceCache.TryAdd(key, imageSource); } } return(imageSource); }