简体   繁体   English

在C#控制台上使用用户输入图像

[英]Working with user input images on C# console

I'm working on a program which overlays an image on top of another, so far it works but the only problem is that I need to set a path for the image file I want to use as the base for the program to work. 我正在开发一个程序,该程序可以将图像叠加在另一个程序上,到目前为止,它可以工作,但是唯一的问题是我需要为要用作程序工作基础的图像文件设置路径。

Is there anyway to make it so the program can accept any png file without the use of seeting a specific path? 无论如何,它使得程序可以接受任何png文件而无需使用特定路径? Said program is going to be used to quickly watermark pictures (ie. Say the user has a photo he/she wishes to watermark, they would drag the png file to the built executable and it would give the edited picture with the watermark) 所述程序将用于快速为照片加水印(即,假设用户拥有他/她希望对其加水印的照片,他们会将png文件拖到内置可执行文件中,并为编辑后的图片加水印)

(It has to be on console, I can't use wpf or forms). (它必须在控制台上,我不能使用wpf或表格)。

You can use the first command line parameter of your console application to recieve a path. 您可以使用控制台应用程序的第一个命令行参数来接收路径。 Then, to use it, the user would drag and drop the png file into the exe (drag into the icon in explorer). 然后,要使用它,用户将png文件拖放到exe中(拖放到资源管理器中的图标中)。 The exe would then run taking as first argument the path of the file the user dragged, and then execute as normal. 然后,exe将以用户拖动的文件路径作为第一个参数运行,然后按常规执行。

In fact, if the user would drag and drop multiple files, they would each be taken as a separate command line argument. 实际上,如果用户拖放多个文件,则每个文件都将作为一个单独的命令行参数。

When no file is dragged, you want to tell the user about how to use your exe. 当没有文件被拖动时,您想告诉用户如何使用exe。 Plus, you can keep a Console.ReadLine as alternative input method. 另外,您可以保留Console.ReadLine作为替代输入法。

Addendum : You could drag and drop files into the console window, and it will behave as if the user typed the path. 附录 :您可以将文件拖放到控制台窗口中,其行为就像用户键入路径一样。 That should also work... in fact, if you put that Console.ReadLine I mentioned above, both ways would work. 这也应该起作用...实际上,如果您放置了我上面提到的Console.ReadLine ,那么两种方法都可以起作用。 Only one file, tested in the default console and powershell. 在默认控制台和Powershell中仅测试了一个文件。

I let checking the file type up to you. 我让您检查文件类型。 In fact, it would also work with folders, handle them as you must (for example, you can use Directoy.GetFiles or Directory.EnumerateFiles ). 实际上,它也可以使用文件夹,并Directoy.GetFiles处理它们(例如,您可以使用Directoy.GetFilesDirectory.EnumerateFiles )。

The basic idea is something like this (without error checking): 基本思想是这样的(不进行错误检查):

static void Main(string[] args)
{
    if (args.Length == 0 )
    {
        Console.WriteLine("Command line Usage: ConsoleAppName.exe filePath");
        Console.WriteLine("You can drag and drop files into the executable to use it");
        // Optional: take a path from Console.ReadLine
    }
    else
    {
        foreach (var filePath in args)
        {
            Console.WriteLine($"Processing: {filePath}");
            // Optional: check if folder and handle correctly
            ProcessFile(filePath);
        }
        Console.WriteLine("All done");
    }
    Console.WriteLine("[Press any key to exit]");
    Console.ReadKey();
}

I am assuming the file check happens inside ProcessFile 我假设文件检查发生在ProcessFile内部


You can, of course, reference System.Drawing from your console application. 当然,您可以从控制台应用程序引用System.Drawing

Instead of taking the extension or trying to get the header of the file. 而不是采用扩展名或尝试获取文件头。 I would suggest to just try to load the image ( Bitmap(string) ). 我建议只是尝试加载图像( Bitmap(string) )。 That way, it will support all the formats that can be loaded by GDI+ (BMP, GIF, EXIF, JPG, PNG and TIFF), and will do it even if the extension is wrong. 这样,它将支持GDI +可以加载的所有格式(BMP,GIF,EXIF,JPG,PNG和TIFF),即使扩展名错误也可以做到。 It also means you get an ArgumentException if it isn't an image in one of the supported formats. 这也意味着如果不是支持格式之一的图像,则会得到ArgumentException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM