public static WriteLine ( string message = "" ) : void | ||
message | string | |
리턴 | void |
/// <summary> /// Extension method to help save out images. If codec is null, it'll save as original format /// </summary> /// <param name="image">Image to save</param> /// <param name="path">Path to save it to</param> /// <param name="codec">Codec to save as (can be null).</param> /// <param name="dryRun"></param> public static void Save(this Image image, string path, ImageCodecInfo codec, bool dryRun) { if (dryRun) { LoggingService.WriteLine("Would have created {0}", path); } else { if (codec == null) { // Saves as original image.Save(path); } else { // Saves with customized codec image.Save(path, codec, null); } LoggingService.WriteLine("Created {0}", path); } }
public void Process(FileInfo image, string outPath, string prefix, string suffix, ImageFormat format) { LoggingService.WriteLine("Processing {0}", image.Name); var outFileName = string.Format("{0}{1}{2}", prefix, Path.GetFileNameWithoutExtension(image.FullName), suffix); var rootDir = string.IsNullOrWhiteSpace(outPath) ? Path.GetDirectoryName(_here) : new DirectoryInfo(outPath).FullName; var extension = format == null ? image.Extension : string.Format(".{0}", format.ToString().ToLower()); LoggingService.WriteVerbose("New File Name: {0}{1}", outFileName, extension); int defaultHeight; int defaultWidth; using (var img = Image.FromFile(image.FullName)) { defaultHeight = img.Height; defaultWidth = img.Width; } LoggingService.WriteVerbose("Original Dimensions: {0}W by {1}H", defaultWidth, defaultHeight); var expectedImages = ImageHelper.BuildImageInfo(rootDir, outFileName, extension); DirectoryHelper.SetupPaths(expectedImages, _dry); var codec = ImageHelper.GetImageCodecInfo(format); foreach (var expectedImage in expectedImages) { var path = expectedImage.Value.Item1; var scale = expectedImage.Value.Item2; var scaledWidth = (int)Math.Ceiling(defaultWidth / scale); var scaledHeight = (int)Math.Ceiling(defaultHeight / scale); var scaledImage = _scalerService.Scale(scaledWidth, scaledHeight, image.FullName); scaledImage.Save(path, codec, _dry); LoggingService.WriteVerbose("New Dimensions for {0}: {1}W by {2}H", expectedImage.Key, scaledWidth, scaledHeight); } LoggingService.WriteLine(); }
private static void Main(string[] args) { var helpRequested = false; var log = false; var verbose = false; var quiet = false; var images = new List <FileInfo>(); var outPath = AppDomain.CurrentDomain.BaseDirectory; var prefix = string.Empty; var suffix = string.Empty; ImageFormat format = null; var dry = false; var options = new OptionSet { { "i|img|image=", "Image to resize \nnote: this MUST be your largest version (xxhdpi/@3x)", v => images.Add(new FileInfo(v)) }, { "f|format=", "Output format (png, jpg)", v => format = ImageHelper.ParseImageFormat(v) }, { "o|out|outpath=", "Path to save out the image\ndefaults to current directory of downsize.exe", v => outPath = v }, { "pre|prefix=", "Prefix to prepend to the image name", v => prefix = v }, { "suf|suffix=", "Suffix to append to the image name", v => suffix = v }, { "l|log", "write a log file to the output directory", v => log = v != null }, { "s|q|silent|quiet", "Don't write out to console", v => quiet = v != null }, { "d|dry|dryrun", "Summary of what would happen", v => dry = v != null }, { "v|verbose", "Write verbose information", v => verbose = v != null }, { "?|h|help", "show help message and exit", v => helpRequested = v != null } }; try { var p = options.Parse(args); images.AddRange(p.Select(unParsed => new FileInfo(unParsed))); } catch (OptionException e) { Console.Write("{0}: ", AppName); Console.WriteLine(e.Message); Console.WriteLine("Try `{0} --help' for more information.", AppName); Console.WriteLine(); return; } if (helpRequested) { ShowHelp(options); return; } LoggingService.Init(outPath, log, verbose, quiet); LoggingService.WriteLine("Begin Processing Images!"); LoggingService.WriteVerbose("Writing files to {0}", outPath); var processor = new ImageProcessor(dry); foreach (var image in images) { try { processor.Process(image, outPath, prefix, suffix, format); } catch (Exception ex) { LoggingService.WriteError("An error occurred while processing {0}", image); LoggingService.WriteError(ex.Message); } } LoggingService.WriteLine("Finished Processing Images!"); }