我有一个c#控制台应用程序在调用exe文件时需要一些参数。
这是我的控制台应用程序的代码。告诉我代码中是否有任何错误
using System.Drawing;
using net.sf.dotnetcli;
namespace GenerateTiles
{
class Program
{
public struct OPTIONS
{
public const string FILEPATH = "f";
public const string TARGETFOLDER = "t";
public const string REMOVEXISTINGFILES = "d";
public const string MAXZOOM = "max";
public const string HELP = "help";
}
static void Main(string[] args)
{
Options options = new Options();
options.AddOption(new Option(OPTIONS.FILEPATH, true, "File to parse"));
options.AddOption(new Option(OPTIONS.TARGETFOLDER, true, "Folder to create tiles"));
options.AddOption(new Option(OPTIONS.REMOVEXISTINGFILES, false, "Remove existing files"));
options.AddOption(new Option(OPTIONS.MAXZOOM, true, "Max zoom dimension"));
options.AddOption(new Option(OPTIONS.HELP, true, "Print this message"));
BasicParser parser = new BasicParser();
CommandLine cmd = parser.Parse(options, args);
if (cmd.HasOption(OPTIONS.HELP))
{
HelpFormatter formatter = new HelpFormatter();
formatter.PrintHelp("GenerateTiles", options);
return;
}
//********
//********
if ((!cmd.HasOption(OPTIONS.FILEPATH)) ||
(!cmd.HasOption(OPTIONS.TARGETFOLDER)) ||
(!cmd.HasOption(OPTIONS.MAXZOOM)))
{
HelpFormatter formatter = new HelpFormatter();
formatter.PrintHelp("GenerateTiles", options);
//return;
}
if (!System.IO.File.Exists(cmd.GetOptionValue(OPTIONS.FILEPATH)))
{
Console.WriteLine("file not exist");
return;
}
double maxZoom;
bool res = double.TryParse(cmd.GetOptionValue(OPTIONS.MAXZOOM), out maxZoom);
if ((res == false) || (maxZoom >= 10))
{
Console.WriteLine("Scale multiplier should be an integer <=10");
return;
}
//Read image
Bitmap bmSource;
try
{
bmSource = (Bitmap)Bitmap.FromFile(cmd.GetOptionValue(OPTIONS.FILEPATH));
}
catch
{
Console.WriteLine("image file not valid");
return;
}
//check directory exist
if (!System.IO.Directory.Exists(cmd.GetOptionValue(OPTIONS.TARGETFOLDER)))
{
System.IO.Directory.CreateDirectory(cmd.GetOptionValue(OPTIONS.TARGETFOLDER));
}
else if (cmd.HasOption(OPTIONS.REMOVEXISTINGFILES))
{
string[] files = System.IO.Directory.GetFiles(cmd.GetOptionValue(OPTIONS.TARGETFOLDER));
foreach (string file in files)
System.IO.File.Delete(file);
string[] dirs = System.IO.Directory.GetDirectories(cmd.GetOptionValue(OPTIONS.TARGETFOLDER));
foreach (string dir in dirs)
System.IO.Directory.Delete(dir, true);
}
int actualHeight = bmSource.Height;
int actualWidth = bmSource.Width;
if (((actualHeight % 256) != 0)
||
((actualWidth % 256) != 0))
{
Console.WriteLine("image width and height pixels should be multiples of 256");
return;
}
int actualResizeSizeWidth = 1;
int level = 0;
while (level <= maxZoom)
{
string leveldirectory = System.IO.Path.Combine(cmd.GetOptionValue(OPTIONS.TARGETFOLDER), String.Format("{0}", level));
if (!System.IO.Directory.Exists(leveldirectory))
System.IO.Directory.CreateDirectory(leveldirectory);
int rowsInLevel = Convert.ToInt32(Math.Pow(2, level));
actualResizeSizeWidth = 256 * rowsInLevel;
//create image to parse
int actualResizeSizeHeight = (actualHeight * actualResizeSizeWidth) / actualWidth;
Bitmap resized = new Bitmap(bmSource, new Size(actualResizeSizeWidth, actualResizeSizeHeight));
string levelSourceImage = System.IO.Path.Combine(leveldirectory, "level.png");
resized.Save(levelSourceImage);
for (int x = 0; x < rowsInLevel; x++)
{
string levelrowdirectory = System.IO.Path.Combine(leveldirectory, String.Format("{0}", x));
if (!System.IO.Directory.Exists(levelrowdirectory))
System.IO.Directory.CreateDirectory(levelrowdirectory);
Bitmap bmLevelSource = (Bitmap)Bitmap.FromFile(levelSourceImage);
//generate tiles
int numberTilesHeight = Convert.ToInt32(Math.Ceiling(actualResizeSizeHeight / 256.0));
for (int y = 0; y < numberTilesHeight; y++)
{
Console.WriteLine("Generating Tiles " + level.ToString() + " " + x.ToString() + " " + y.ToString()); int heightToCrop = actualResizeSizeHeight >= 256 ? 256 : actualResizeSizeHeight;
Rectangle destRect = new Rectangle(x * 256, y * 256, 256, heightToCrop);
//croped
Bitmap bmTile = bmLevelSource.Clone(destRect, System.Drawing.Imaging.PixelFormat.DontCare);
//full tile
Bitmap bmFullTile = new Bitmap(256, 256);
Graphics gfx = Graphics.FromImage(bmFullTile);
gfx.DrawImageUnscaled(bmTile, 0, 0);
bmFullTile.Save(System.IO.Path.Combine(levelrowdirectory, String.Format("{0}.png", y)));
bmFullTile.Dispose();
bmTile.Dispose();
}
}
level++;
}
}
}
}
我正在使用以下参数
从命令行调用exe1)尝试过这种方式但失败了
GenerateTiles /f C::\test\img.jpg /t C::\test\Src /max 10
2)再次以这种方式尝试但失败了
GenerateTiles /f C:\test\img.jpg /t C:\test\Src /max 10
3)再次尝试这种方式但失败了
GenerateTiles C:\test\img.jpg C:\test\Src /max 10
所以我犯了错误?请纠正我。感谢