Skip to content

Instantly share code, notes, and snippets.

@Sir-Photch
Last active February 28, 2022 17:06
Show Gist options
  • Save Sir-Photch/f35439035173a4a6006a0e40c76ac9d2 to your computer and use it in GitHub Desktop.
Save Sir-Photch/f35439035173a4a6006a0e40c76ac9d2 to your computer and use it in GitHub Desktop.

C#/.NET6.0 Script to convert images

Converts from any type that is readable by Image.FromFile() to any of {BMP, JPG, PNG}

usage:

ImageConverter.exe {BMP, JPG, PNG} <files>

Dependencies: System.Drawing.Common

using System;
using System.Linq;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
namespace ImageConverter
{
class Program
{
enum ImageFormat { BMP, JPG, PNG, INVALID }
static void Main(string[] args)
{
System.Drawing.Imaging.ImageFormat outputFormat;
if (!Enum.TryParse(typeof(ImageFormat), args.First(), out object outputTypeObj))
{
Console.WriteLine($"Specify output-format as first argument, supported: {string.Join(',', Enum.GetValues(typeof(ImageFormat)))}");
Environment.Exit(1);
}
IEnumerable<string> images = args[1] == "*"
? Directory.EnumerateFiles(Environment.CurrentDirectory, "*.png", SearchOption.TopDirectoryOnly)
.Concat(Directory.EnumerateFiles(Environment.CurrentDirectory, "*.jpg", SearchOption.TopDirectoryOnly))
.Concat(Directory.EnumerateFiles(Environment.CurrentDirectory, "*.bmp", SearchOption.TopDirectoryOnly))
: new List<string>(args[1..]);
outputFormat = ToGDIImageFormat((ImageFormat)outputTypeObj);
foreach (var path in images)
{
if (!File.Exists(path))
{
Console.WriteLine($"Bad input arg: {path}");
Environment.Exit(1);
}
try
{
Image image = Image.FromFile(path);
var inputFormat = image.RawFormat;
if (inputFormat != outputFormat)
image.Save(Path.ChangeExtension(path, ToExtension(ToImageFormat(outputFormat))), outputFormat);
}
catch (Exception e)
{
Console.WriteLine($"Could not convert {path} to {ToImageFormat(outputFormat)}");
Console.WriteLine(e.ToString());
Environment.Exit(1);
}
}
Environment.Exit(0);
}
private static string ToExtension(ImageFormat format) => format switch
{
ImageFormat.BMP => ".bmp",
ImageFormat.PNG => ".png",
ImageFormat.JPG => ".jpg",
_ => throw new NotImplementedException($"imageFormat {format} was not implemented because of laziness")
};
private static ImageFormat ToImageFormat(System.Drawing.Imaging.ImageFormat gdi_imageFormat)
{
if (gdi_imageFormat == System.Drawing.Imaging.ImageFormat.Bmp)
return ImageFormat.BMP;
else if (gdi_imageFormat == System.Drawing.Imaging.ImageFormat.Jpeg)
return ImageFormat.JPG;
else if (gdi_imageFormat == System.Drawing.Imaging.ImageFormat.Png)
return ImageFormat.PNG;
else
return ImageFormat.INVALID;
}
private static System.Drawing.Imaging.ImageFormat ToGDIImageFormat(ImageFormat format) => format switch
{
ImageFormat.BMP => System.Drawing.Imaging.ImageFormat.Bmp,
ImageFormat.JPG => System.Drawing.Imaging.ImageFormat.Jpeg,
ImageFormat.PNG => System.Drawing.Imaging.ImageFormat.Png,
_ => throw new NotImplementedException($"imageFormat {format} was not implemented because of laziness")
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment