I made myself a small (and yet another) app for resizing images

For a long time I used a third-party app called ImageResizer, but it could only shrink JPEG files. And since I was getting tired of converting images to JPG and then possibly back again,
I made myself a small app of my own that converts file types, primarily JPG, but it can also handle any other type – I tried it with png and gif (not so much now, but rather in my SunamoBlogConverter), where I use the same algorithm. It is yet another app, because I already have a similar one from earlier, which is designed specifically for shrinking photos and is an app with a GUI:

ImageShrinker

The new app is an app without a GUI; the only thing it prints is whether the file was converted successfully or whether it already existed (here I am converting a single file, but there is no limit on how many you can convert at once):

SunamoImageResizer
So back to ImageResizer: it works by simply letting me drag files onto it, and it shrinks them based on the name of the exe. If you give it only the width, it can calculate the height automatically. My program can do all of this too. And so that this post is not too bare, I can at least share the Main() method with you:

C#
static void Main(string[] args)
{
    string coords = null;
    int width = 0;
    int height = 0;
    bool calculateHeight = false;
    string size = Process.GetCurrentProcess().ProcessName.Replace(".exe", "");
    if (!size.Contains("-"))
    {
        string error = "You must specify at least the width - for a width of 16px, name the program ImageResizer16. The program will then shrink both dimensions in the same ratio. If you want to specify a different width and height (for example height: 768px, width 1366px), name the program ImageResizer-1366x768";
        Error(error);
        return;
    }
    else
    {
        string[] first = Split(size, "-");
        coords = first[first.Length - 1];
        if (coords.Contains("x"))
        {
            string[] second = Split(coords, "x");

            if (second.Length != 2)
            {
                Error("You cannot specify more than 2 dimensions");
                return;
            }

            if (int.TryParse(second[0], out width))
            {
                height = width;
            }
            else
            {
                Error("No valid number was specified for the width");
                return;
            }
            if (int.TryParse(second[1], out height))
            {

            }
            else
            {
                Error("No valid number was specified for the height");
                return;
            }
        }
        else
        {
            if (int.TryParse(coords, out width))
            {
                calculateHeight = true;
                coords =  width + "x" + height;

            }
            else
            {
                Error("No valid number was specified for the size");
                return;
            }
        }

    }
    coords = "-" + coords;
    for (int i = 0; i < args.Length; i++)
    {
        if (calculateHeight)
        {
            Image image = Image.FromFile(args[i]);
            //-1366x768 = 800x600
            float f = (float)image.Width / (float)image.Height;
            height = (int)(width / f);
            coords = width + "x" + height;
        }

        string to = InsertBetweenFileNameAndExtension(args[i], "-" + coords);
        string fileName = Path.GetFileName(to);
        if (ImageResize(args[i], to, width, height) == "")
        {
            Console.WriteLine("File {0} already existed", fileName);
        }
        else
        {
            Console.WriteLine("Successfully converted to {0}", fileName);
        }
    }
    Console.ReadLine();
}
Categories

Leave a Reply