static void Main(string[] args) { var options = new Options(); if (!CommandLine.Parser.Default.ParseArguments(args, options)) return; var logger = new ConsoleLogger(); new Program(logger).Start(options); Console.WriteLine("Press any key to continue..."); Console.ReadKey(); }
private void Start(Options options) { if (!Directory.Exists(options.InputDirectory)) { _logger.Error("The specified directory does not exist."); return; } // in case output directory is unspecified or invalid, use input directory // for output var inputRoot = new DirectoryInfo(options.InputDirectory); var outputRoot = options.OutputDirectory != null ? new DirectoryInfo(options.OutputDirectory) : new DirectoryInfo(options.InputDirectory); if (outputRoot != inputRoot) { if (!outputRoot.Exists) { try { outputRoot.Create(); } catch (IOException ioException) { _logger.Error("Could not create output directory.\n{0}", ioException); return; } } } // scan the files _logger.Info("Input directory: {0}", inputRoot); _logger.Info("Output directory: {0}", outputRoot); _logger.Info("Searching for files..."); var files = FileScanner.ScanForFiles(options.InputDirectory, "*.txt"); if (files.Count == 0) { _logger.Error("No files found."); return; } //remove file roots so we could write them somewhere else var strippedFiles = files.Select(info => info.FullName.Replace(inputRoot.FullName, "")).ToList(); _logger.Info("Found {0} files on the path.", files.Count); new FileProcessor(options, _logger).Process(strippedFiles, inputRoot, outputRoot); }
/// <summary> /// Initializes a new instance of the <see cref="FileProcessor"/> class. /// </summary> /// <param name="options">The options.</param> /// <param name="logger">The logger.</param> public FileProcessor(Options options, ILogger logger) { _options = options; _logger = logger; }
public BitmapSource RenderToFile(string text, Options options, string outFile) { RenderTargetBitmap bitmap = null; var thread = new Thread(() => { var outFileInfo = new FileInfo(outFile); var desiredWidth = options.ImageWidth; // set Grid and Textblock options var grid = new Grid(); var tb = new TextBlock(); tb.SetValue(FrameworkElement.VerticalAlignmentProperty, VerticalAlignment.Top); tb.SetValue(FrameworkElement.HorizontalAlignmentProperty, HorizontalAlignment.Stretch); tb.SetValue(TextBlock.TextWrappingProperty, TextWrapping.Wrap); tb.SetValue(TextBlock.BackgroundProperty, Brushes.White); tb.SetValue(TextBlock.TextProperty, text); tb.SetValue(TextBlock.PaddingProperty, new Thickness(options.Padding)); tb.SetValue(TextBlock.FontFamilyProperty, new FontFamily(options.Font)); tb.SetValue(TextBlock.FontSizeProperty, (double)options.FontSize); tb.SetValue(TextBlock.TextAlignmentProperty, options.TextAlinment); // set text rendering and hinting options //TODO: http://blogs.msdn.com/b/text/archive/2009/08/24/wpf-4-0-text-stack-improvements.aspx tb.SetValue(TextOptions.TextFormattingModeProperty, TextFormattingMode.Ideal); tb.SetValue(TextOptions.TextRenderingModeProperty, TextRenderingMode.ClearType); // add TB to the grid, we'll use this to accurately measure the // final height of the box grid.Children.Add(tb); // measure grid.Measure(new Size(desiredWidth, 200)); grid.Arrange(new Rect(new Size(desiredWidth, 200))); // get the actual size required to fit the entire text var actual = Math.Ceiling(tb.ActualHeight); // addjust our components to match the actual height grid.Measure(new Size(desiredWidth, actual)); grid.Arrange(new Rect(new Size(desiredWidth, actual))); // create rendering target and render control onto it bitmap = new RenderTargetBitmap(desiredWidth, (int)actual, 96, 96, PixelFormats.Pbgra32); bitmap.Render(tb); // encode it and save it to file. var encoder = new JpegBitmapEncoder { QualityLevel = 100 }; encoder.Frames.Add(BitmapFrame.Create(bitmap)); if (outFileInfo.DirectoryName != null) Directory.CreateDirectory(outFileInfo.DirectoryName); using (Stream s = File.Create(outFile)) encoder.Save(s); }); thread.SetApartmentState(ApartmentState.STA); thread.Priority = ThreadPriority.AboveNormal; thread.Start(); thread.Join(); return bitmap; }