/// <summary> /// Asynchronously gets the character image from cache. /// </summary> /// <param name="filename">The filename.</param> /// <param name="directory">The directory.</param> /// <returns></returns> internal static Image GetImageFromCache(string filename, string directory = null) { // First check whether the image exists in cache EveMonClient.EnsureCacheDirInit(); string cacheFileName = Path.Combine(directory ?? EveMonClient.EVEMonImageCacheDir, filename); if (!File.Exists(cacheFileName)) { return(null); } try { // Load the data into a MemoryStream before returning the image to avoid file // locking Image image; byte[] imageBytes = File.ReadAllBytes(cacheFileName); using (MemoryStream stream = new MemoryStream(imageBytes)) { image = Image.FromStream(stream); } return(image); } catch (ArgumentException e) { ExceptionHandler.LogException(e, false); FileHelper.DeleteFile(cacheFileName); } catch (IOException e) { ExceptionHandler.LogException(e, false); } catch (UnauthorizedAccessException e) { ExceptionHandler.LogException(e, false); } return(null); }
/// <summary> /// Adds the image to the cache. /// </summary> /// <param name="image">The image.</param> /// <param name="filename">The filename.</param> /// <param name="directory">The directory.</param> /// <returns></returns> internal static async Task AddImageToCacheAsync(Image image, string filename, string directory = null) { // Saves the image file try { // Write this image to the cache file EveMonClient.EnsureCacheDirInit(); string cacheFileName = Path.Combine(directory ?? EveMonClient.EVEMonImageCacheDir, filename); await FileHelper.OverwriteOrWarnTheUserAsync(cacheFileName, async fs => { ((Image)image.Clone()).Save(fs, ImageFormat.Png); await fs.FlushAsync(); return(true); }).ConfigureAwait(false); } catch (Exception ex) { ExceptionHandler.LogRethrowException(ex); throw; } }