I’m working on a project where I need to take a JPG image and then generate some code that is based on the image. So for this project I would like to:
- Create a console application
- Parse in an image file as input
- Export a formatted text file based on the image using a code template
In this post, I will walk you through how I decided to do this with .NET Core and T4 Templates and I’ll give you a sample project to play around with.
To get started, you’ll need the latest version of .NET Core.
From here, you can either work with the command line directly to create a console application by using the “dotnet new” command or you can use Visual Studio Community or Visual Studio Code to create the project. I’ll be using VS community for this post.
In order to read the command line arguments into the app, I decided to go with this CommandLineParser Nuget package from here.
In order to read the image file I found a great post here on the .NET Blog that offers several good options.
I chose to go with the ImageSharp project from here … mainly because I liked how it uses all managed code so it should then run anywhere.
In order to format and export the file you can create a .tt template in Visual Studio by using Add Item > C# > General > RuntimeTextTemplate > RuntimeTextTemplate.tt. To read more about T4 templates you can go here. (I had trouble running this in .NET Core until I realized that the Custom Tool property is not set to TextTemplatingFilePreprocessor automatically, so you’ll need to set that yourself first too)
A working sample that you can play around with is here on github.
dotnet DotnetT4.dll -i t.jpg -o t.txt
Let me know in the comments if you have any questions on how this works and I’ll be glad to help you out.
Thanks for reading!