I wrote a simple program that converts graphic files in a folder to a single PDF using Magic.NET
. It works fine. I'm interested in general comments about the code as well as suggestions how I can make it run faster. Answers with some code are especially welcome.
static void Main(string[] args)
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
// Any() needs using System.Linq
if ( Directory.EnumerateFileSystemEntries(args[0], "*.tif").Any() == false )
{
Console.WriteLine("No TIF files found.");
Environment.Exit(-1);
}
string[] directory = Directory.GetFiles(args[0], "*.tif");
// gets a directory name even when a relative path is given
// http://stackoverflow.com/a/674495/2657875
string filename = new FileInfo(directory[0]).Directory.FullName + ".pdf";
// returns the pathname where the currently executing assembly resides
MagickNET.Initialize(Path.GetDirectoryName
(Assembly.GetExecutingAssembly().Location) + @"\MagicSettings");
Console.WriteLine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
using (MagickImageCollection collection = new MagickImageCollection())
{
foreach (string file in directory)
{
MagickImage image = new MagickImage(file);
image.Quality = 100;
image.Sharpen(0, 3);
image.CompressionMethod = CompressionMethod.Group4;
collection.Add(image);
}
try
{
collection.Write(filename);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
// Get the elapsed time as a TimeSpan value.
TimeSpan ts = stopWatch.Elapsed;
// Format and display the TimeSpan value.
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}",
ts.Hours, ts.Minutes, ts.Seconds);
Console.WriteLine("Done in " + elapsedTime);
}
}