0

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? 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)

(It has to be on console, I can't use wpf or forms).

  • "any .png" file as in, it just finds one? The user selects any random one via file path? – Austin T French Jul 24 '19 at 01:18
  • Sorry for not specifying, by any png I meant as in the user selects a photo and opens it using the executable (the program is for adding watermarks quickly to photos) – MrsClefairy Jul 24 '19 at 01:24
  • Currently how the user is passing the image path to the exe? How exactly the user is opening the photo using the exe? – Chetan Jul 24 '19 at 01:31
  • 1
    Please [edit] post instead of adding clarifications as comments (also "user selects a photo and opens it" is not exactly clear - where they select photo?) – Alexei Levenkov Jul 24 '19 at 01:31
  • (I suspect https://stackoverflow.com/questions/3788429/c-sharp-windows-open-with-context-menu-behaviour is what you are asking... but that is not yet clear) – Alexei Levenkov Jul 24 '19 at 01:37
  • Yes it is something like that, I want for the user to simply open the image with the executable and get the edited picture as the result, unfortunately right now the only way the user could input their image would be to modify the path in the code each time he / she wishes to put a new picture which is what im struggling with – MrsClefairy Jul 24 '19 at 01:43

1 Answers1

1

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). The exe would then run taking as first argument the path of the file the user dragged, and then execute as normal.

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. Plus, you can keep a Console.ReadLine as alternative input method.

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. Only one file, tested in the default console and 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).

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


You can, of course, reference System.Drawing from your console application.

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)). 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. It also means you get an ArgumentException if it isn't an image in one of the supported formats.

Theraot
  • 31,890
  • 5
  • 57
  • 86