public static void Save(Image image, Stream stream, IconFactorySizeOption option = IconFactorySizeOption.Application, bool dispose = false) { if (image == null) { throw new ArgumentNullException(nameof(image)); } if (stream == null) { throw new ArgumentNullException(nameof(stream)); } var img = ImageCorrection(image); try { var size = Math.Max(img.Width, img.Height); var sizes = GetSizes(option); var images = sizes.Where(x => x <= size).Select(x => img.Redraw(x, x)); Save(ImageCorrection(images), stream, dispose); } finally { if (dispose) { img?.Dispose(); } } }
/// <summary> /// Retrieves all size dimensions for the specified /// <see cref="IconFactorySizeOption"/> value. /// </summary> /// <param name="option"> /// The <see cref="IconFactorySizeOption"/> value. /// </param> public static IEnumerable <int> GetSizes(IconFactorySizeOption option) { if (option == IconFactorySizeOption.Application) { return new[] { 256, 128, 64, 48, 32, 24, 16 } } ; return(new[] { 256, 128, 96, 64, 48, 40, 32, 24, 22, 20, 16, 14, 10, 8 }); }
/// <summary> /// Saves multiple sizes of the specified <see cref="Image"/> to a single /// <see cref="Icon"/> file. /// </summary> /// <param name="image"> /// The images to be converted into a single icon. /// </param> /// <param name="path"> /// The file path to the icon. /// </param> /// <param name="option"> /// The option for determining automatic resizing. /// </param> /// <exception cref="ArgumentNullException"> /// image or path is null. /// </exception> /// <exception cref="ArgumentInvalidException"> /// path is invalid. /// </exception> public static void Save(Image image, string path, IconFactorySizeOption option = IconFactorySizeOption.Application) { if (image == null) { throw new ArgumentNullException(nameof(image)); } if (string.IsNullOrEmpty(path)) { throw new ArgumentNullException(nameof(path)); } var file = PathEx.Combine(path); if (!PathEx.IsValidPath(file)) { throw new ArgumentInvalidException(nameof(path)); } if (!FileEx.TryDelete(file)) { return; } using var fs = new FileStream(file, FileMode.Create); Save(image, fs, option, true); }