namespace BinaryImage_Console
{
/// <summary>
/// Program Class of Console App
/// </summary>
class Program
{
/// <summary>
/// Main entry point for Program
/// </summary>
/// <param name="args">Argument of main method</param>
static void Main(string[] args)
{
Console.WriteLine("Welcome to Binary Image Maker");
Console.WriteLine("\nUse following command for help:");
Console.WriteLine("dotnet ImageBinarizerApp-帮助");
args = new String[] { Console.ReadLine() };
//Test if necessary input arguments were supplied.
if (args.Length < 8)
{
if(args.Length == 1 && args[0].Equals("-help"))
{
Console.WriteLine("\nHelp:");
Console.WriteLine("\nPass the arguments as following:");
Console.WriteLine("\nExample with automatic RGB:\ndotnet ImageBinarizerApp --input-image c:\\a.png --output-image d:\\out.txt -width 32 -height 32");
Console.WriteLine("\nExample with explicit RGB:\ndotnet ImageBinarizerApp --input-image c:\\a.png --output-image d:\\out.txt -width 32 -height 32 -red 100 -green 100 -blue 100");
}
else
{
Console.WriteLine("\nError: All necessary arguments are not passed. Please pass the arguments first.");
}
Console.WriteLine("\n按任意键退出应用程序。");
Console.ReadLine();
return;
}
else
{
String inputImagePath = "";
String outputImagePath = "";
int imageWidth = 0;
int imageHeight = 0;
int redThreshold = -1;
int greenThreshold = -1;
int blueThreshold = -1;
if(args[0].Equals("--input-image") && File.Exists(args[1]))
{
inputImagePath = args[1];
}
else
{
Console.WriteLine("\nError: Input file doesn't exist.");
Console.WriteLine("\n按任意键退出应用程序。");
Console.ReadLine();
return;
}
int separatorIndex = args[3].LastIndexOf(Path.DirectorySeparatorChar);
if (args[2].Equals("--output-image") && separatorIndex >= 0 && Directory.Exists(args[3].Substring(0, separatorIndex)))
{
outputImagePath = args[3];
}
else
{
Console.WriteLine("\nError: Output Directory doesn't exist.");
Console.WriteLine("\n按任意键退出应用程序。");
Console.ReadLine();
return;
}
if (!args[4].Equals("-width") || !int.TryParse(args[5], out imageWidth))
{
Console.WriteLine("\nError: Image Width should be integer.");
Console.WriteLine("\n按任意键退出应用程序。");
Console.ReadLine();
return;
}
if (!args[6].Equals("-height") || !int.TryParse(args[7], out imageHeight))
{
Console.WriteLine("\nError: Image Height should be integer.");
Console.WriteLine("\n按任意键退出应用程序。");
Console.ReadLine();
return;
}
if(args.Length > 8)
{
if(args.Length < 14)
{
Console.WriteLine("\nError: All three Red, Green and Blue Thresholds should be passed.");
Console.WriteLine("\n按任意键退出应用程序。");
Console.ReadLine();
return;
}
else
{
if (!args[8].Equals("-red") || !(int.TryParse(args[9], out redThreshold)) || redThreshold < 0 || redThreshold > 255)
{
Console.WriteLine("\nError: Red Threshold should be in between 0 and 255.");
Console.WriteLine("\n按任意键退出应用程序。");
Console.ReadLine();
return;
}
if (!args[10].Equals("-green") || !(int.TryParse(args[11], out greenThreshold)) || greenThreshold < 0 || greenThreshold > 255)
{
Console.WriteLine("\nError: Green Threshold should be in between 0 and 255.");
Console.WriteLine("\n按任意键退出应用程序。");
Console.ReadLine();
return;
}
if (!args[12].Equals("-blue") || !(int.TryParse(args[13], out blueThreshold)) || blueThreshold < 0 || blueThreshold > 255)
{
Console.WriteLine("\nError: Blue Threshold should be in between 0 and 255.");
Console.WriteLine("\n按任意键退出应用程序。");
Console.ReadLine();
return;
}
}
}
else
{
redThreshold = -1;
greenThreshold = -1;
blueThreshold = -1;
}
Console.WriteLine("\nImage Binarization in progress...");
try
{
ImageBinarizerApplication obj = new ImageBinarizerApplication();
obj.Binarizer(inputImagePath, outputImagePath, imageWidth, imageHeight, redThreshold, greenThreshold, blueThreshold);
}
catch (Exception e)
{
Console.WriteLine($"\nError: {e.Message}");
Console.WriteLine("\n按任意键退出应用程序。");
Console.ReadLine();
return;
}
Console.WriteLine("\nImage Binarization completed.");
Console.WriteLine("\n按任意键退出应用程序。");
Console.ReadLine();
}
}
}
}
internal static void Main(string[] args)
{
new Thread(() => Request_Response(new string[0]))
{
Name = "Executor"
}.Start();
}
internal static void Request_Response(string[] args)
{
Console.WriteLine("Welcome to Binary Image Maker");
Console.WriteLine("\nUse following command for help:");
Console.WriteLine("dotnet ImageBinarizerApp-帮助");
args = new String[] { Console.ReadLine() };
}
Console.ReadLine()
which will actually accept any key presses from the user until presses the Enter key. So the effect of all this is that after all the help text is printed out, the user enters a command thinking that they are entering something into the command prompt when it actually just the Console.ReadLine()
that is reading in a string until the user presses Enter. After that the program exits.您是否真的调试过代码,即设置断点并逐行浏览代码,检查每一步的状态?如果没有,则需要先执行此操作。您必须对每个步骤都会发生的事情有一个期望,然后才能知道它是否会发生。一旦代码的行为与预期的不同,您就会发现一个特定的问题,可以对此进行更详细的研究。您不能仅通过阅读代码来解决问题。您必须观看实际操作。
static void Main(string[] args)
{
String inputImagePath = "";
String outputImagePath = "";
int imageWidth = 0;
int imageHeight = 0;
int redThreshold = -1;
int greenThreshold = -1;
int blueThreshold = -1;
Console.WriteLine("Welcome to Binary Image Maker");
Console.WriteLine("\nUse following command for help:");
Console.WriteLine("dotnet ImageBinarizerApp-帮助");
args = new String[] { Console.ReadLine() };
//Test if necessary input arguments were supplied.
if (args.Length < 8)
{
if (args.Length == 1 && args[0].Equals("-help"))
{
Console.WriteLine("\nHelp:");
Console.WriteLine("\nPass the arguments as following:");
Console.WriteLine("\nExample with automatic RGB:\ndotnet ImageBinarizerApp --input-image c:\\a.png --output-image c:\\out.txt -width 32 -height 32");
Console.WriteLine("\nExample with explicit RGB:\ndotnet ImageBinarizerApp --input-image c:\\a.png --output-image c:\\out.txt -width 32 -height 32 -red 100 -green 100 -blue 100");
}
else
{
Console.WriteLine("\nError: All necessary arguments are not passed. Please pass the arguments first.");
Console.WriteLine("\n按任意键退出应用程序。");
Console.ReadLine();
return;
}
args = new String[] { Console.ReadLine() };
if (args[0].Equals("--input-image") && File.Exists(args[1]))
{
inputImagePath = args[1];
}
else
{
Console.WriteLine("\nError: Input file doesn't exist.");
Console.WriteLine("\n按任意键退出应用程序。");
Console.ReadLine();
return;
}
int separatorIndex = args[3].LastIndexOf(Path.DirectorySeparatorChar);
if (args[2].Equals("--output-image") && separatorIndex >= 0 && Directory.Exists(args[3].Substring(0, separatorIndex)))
args[0]
. Notice that it doesn't match the string you are expecting, so the Boolean expression on line 39 short circuits to false (and therefore does not throw an array out of bounds exception). Since there expression is false, the code goes to the else clause and you seem the error message indicating that the file does not exist.Console.ReadLine()
inside curly braces and assigning it to a string array will automatically parse the input into an array of strings, then as you can see, your assumption is wrong. You only get an array with a single string because ReadLine()
on returns a single string.太感谢了。这帮助我改善了当前的实施。问题出在代码的第34和35行上。他为用户编写了一个误导性消息:"按任意键退出应用程序。" on line 34. Then on line 35, he programmed a call toConsole.ReadLine()
which will actually accept any key presses from the user until presses the Enter key. So the effect of all this is that after all the help text is printed out, the user enters a command thinking that they are entering something into the command prompt when it actually just theConsole.ReadLine()
that is reading in a string until the user presses Enter. After that the program exits.
请参阅第6个帖子中的图片。请注意,他在"按任意键退出应用程序。"在显示有关应用程序退出的最终输出之前。
谢谢你。请看一下这个快照。您知道为什么inputImagePath变量无效吗?User your debugger. Look closely at the value ofargs[0]
. Notice that it doesn't match the string you are expecting, so the Boolean expression on line 39 short circuits to false (and therefore does not throw an array out of bounds exception). Since there expression is false, the code goes to the else clause and you seem the error message indicating that the file does not exist.
If you are assuming that putting the call toConsole.ReadLine()
inside curly braces and assigning it to a string array will automatically parse the input into an array of strings, then as you can see, your assumption is wrong. You only get an array with a single string becauseReadLine()
on returns a single string.
internal static void Main(string[] args) => new Thread(() => Request_Response()) { Name = "Executor" }.Start();
internal static void Request_Response()
{
string[] args = new string[3];
args[0] = "foo";
args[1] = "bar";
foreach (string str in args)
{
if (!string.IsNullOrEmpty(str))
Debug.WriteLine($"Before declaring args new string[] {str}");
}
args = new string[] { "foobar" };
foreach (string str in args)
{
Debug.WriteLine($"After declaring args as new string[] {str}");
}
}
哈哈。谢谢。我看过你的代码。我有点理解逻辑,但是无法有效地利用调试控制台来确定流程。我将仔细查看您的扰流板,以获取有关C#编程的进一步说明。如果您想了解为什么您的代码无法正常工作。你应该;我恳请您运行此代码,并在阅读“输出”窗口后告诉我。为什么初始化foobar时,foo和bar不再位于字符串数组中?
当您查看此示例代码时,看起来似乎很明显?此代码与您刚犯的错误没有什么不同。
解决这些问题后,请查看您自己的代码,然后告诉我们问题出在哪里?
我刚喝了一瓶酒,仍然可以看到你的问题。C#:internal static void Main(string[] args) => new Thread(() => Request_Response()) { Name = "Executor" }.Start(); internal static void Request_Response() { string[] args = new string[3]; args[0] = "foo"; args[1] = "bar"; foreach (string str in args) { if (!string.IsNullOrEmpty(str)) Debug.WriteLine($"Before declaring args new string[] {str}"); } args = new string[] { "foobar" }; foreach (string str in args) { Debug.WriteLine($"After declaring args as new string[] {str}"); } }
![]()
将此using指令添加到代码文件的顶部。我有点理解逻辑,但是无法有效地利用调试控制台来确定流程。
using System.Diagnostics;