static void LoadImageFromEmbeddedResource(string imageName, DefaultImage defaultImage) { var fileStream = Assembly.GetExecutingAssembly().GetManifestResourceStream($"GameLibrary.Content.{ imageName }"); var image = (Bitmap)Image.FromStream(fileStream); ImageCache.AddOrUpdate(defaultImage.ToString(), image, (key, oldImage) => image); }
private void DownloadAllPreviewImagesAsync() { Task t = Task.Factory.StartNew(() => { CreateRequiredFolders(); foreach (AssetViewModel asset in AllAssets.ToList()) { string pathToThumbnail = Path.Combine(AbsolutePathToThumbnails, asset.Asset.IDWithoutExtension); if (ImageCache.IsOutOfDate(pathToThumbnail) || ImageCache.IsSourceUrlDifferent(pathToThumbnail, asset.Asset.PreviewImage)) { ImageCache.AddOrUpdate(pathToThumbnail, asset.Asset.PreviewImage); Guid downloadId = Guid.NewGuid(); Action onCancel = () => { RemoveFromDownloads(downloadId); }; Action onError = () => { RemoveFromDownloads(downloadId); }; Action onComplete = () => { RemoveFromDownloads(downloadId); }; string formattedUrl = "rsmm://Url/" + asset.Asset.PreviewImage.Replace("://", "$"); DownloadItemViewModel downloadItem = new DownloadItemViewModel() { UniqueId = downloadId, DownloadType = DownloadType.Image, ItemName = "Downloading preview image", DownloadUrl = formattedUrl, SaveFilePath = pathToThumbnail, OnCancel = onCancel, OnComplete = onComplete, OnError = onError }; AddToDownloads(downloadItem); } } }); }
private void GetSelectedPreviewImageAsync() { if (SelectedAsset == null) { return; } IsLoadingImage = true; UserMessage = "Fetching preview image ..."; bool isDownloadingImage = false; Task t = Task.Factory.StartNew(() => { CreateRequiredFolders(); string pathToThumbnail = Path.Combine(AbsolutePathToThumbnails, SelectedAsset.Asset.IDWithoutExtension); if (ImageCache.IsOutOfDate(pathToThumbnail) || ImageCache.IsSourceUrlDifferent(pathToThumbnail, SelectedAsset.Asset.PreviewImage)) { ImageCache.AddOrUpdate(pathToThumbnail, SelectedAsset.Asset.PreviewImage); Guid downloadGuid = Guid.NewGuid(); Action onCancel = () => { RemoveFromDownloads(downloadGuid); }; Action onError = () => { RemoveFromDownloads(downloadGuid); }; Action onComplete = () => { RemoveFromDownloads(downloadGuid); GetSelectedPreviewImageAsync(); }; isDownloadingImage = true; string formattedUrl = "rsmm://Url/" + SelectedAsset.Asset.PreviewImage.Replace("://", "$"); DownloadItemViewModel imageDownload = new DownloadItemViewModel() { UniqueId = downloadGuid, DownloadType = DownloadType.Image, ItemName = "Downloading preview image", OnCancel = onCancel, OnComplete = onComplete, OnError = onError, DownloadUrl = formattedUrl, SaveFilePath = pathToThumbnail }; AddToDownloads(imageDownload); return; } else { UserMessage = ""; } if (PreviewImageSource != null) { PreviewImageSource.Close(); PreviewImageSource = null; } using (FileStream stream = File.Open(pathToThumbnail, FileMode.Open, FileAccess.Read, FileShare.Read)) { PreviewImageSource = new MemoryStream(); stream.CopyTo(PreviewImageSource); } PreviewImageSource = new MemoryStream(File.ReadAllBytes(pathToThumbnail)); }); t.ContinueWith((taskResult) => { if (taskResult.IsFaulted) { UserMessage = "Failed to get preview image."; PreviewImageSource = null; Logger.Error(taskResult.Exception); } IsLoadingImage = isDownloadingImage; }); }