/// <summary> /// Save gallery image /// </summary> public void Save() { if (AppliedFilters.Count == 0) { if (File.Exists(OriginalFilePath)) { File.Delete(OriginalFilePath); } OriginalFileName = string.Empty; } if (TempFilePath.NotEmpty() && File.Exists(TempFilePath)) { File.Copy(TempFilePath, ImageFilePath, true); File.Delete(TempFilePath); TempFilePath = string.Empty; } if (TempThumbnailFilePath.NotEmpty() && File.Exists(TempThumbnailFilePath)) { File.Copy(TempThumbnailFilePath, ThumbnailFilePath, true); File.Delete(TempThumbnailFilePath); TempThumbnailFilePath = string.Empty; } }
/// <summary> /// Applies filters /// </summary> /// <param name="filters">Filters</param> public void ApplyFilters(List <Filter> filters) { try { // Saving original image if (AppliedFilters.Count == 0) { OriginalFileName = GenerateOriginalFileName(ImageFileName); File.Copy(ImageFilePath, OriginalFilePath, true); } // Creating temp image if (TempFilePath.IsNullOrEmpty()) { TempFilePath = Path.Combine(ImageFolderPath, GenerateTempFileName(ImageFileName)); File.Copy(ImageFilePath, TempFilePath, true); } foreach (Filter filter in filters) { try { // Applying filter Bitmap original = new Bitmap(TempFilePath); Bitmap bitmap = new Bitmap(original); original.Dispose(); File.Delete(TempFilePath); ApplyFilter(bitmap, filter).Save(TempFilePath, ImageFormat.Jpeg); bitmap.Dispose(); GarbageCollector.Collect(); AppliedFilters.Add(filter); } catch (Exception e) { LogHelper.Logger.Error(e, $"Unable to apply filter to image. Filter name: {filter.FilterType}"); } } // Create thumbnail for filtered image if (TempThumbnailFilePath.IsNullOrEmpty()) { TempThumbnailFilePath = Path.Combine(ImageFolderPath, GenerateTempThumbnailFileName(ImageFileName)); } else if (File.Exists(TempThumbnailFilePath)) { File.Delete(TempThumbnailFilePath); } new ImagesConverter(TempFilePath).CreateThumbnail(TempThumbnailFilePath, CommonSettings.ThumbnailWidth, CommonSettings.ThumbnailHeight); } catch (Exception e) { LogHelper.Logger.Error(e, "Unable to apply filters to image."); } GarbageCollector.Collect(); }