StringBuilder asciiBuilder = new(outputWidth * outputHeight) įor ( var h = 0 h < image.Height h += heightStep)įor ( var w = 0 w < image.Width w += widthStep) Var heightStep = image.Height / outputHeight Var outputHeight = ( int)(outputWidth / aspect) Var widthStep = image.Width / outputWidth Var aspect = image.Width / ( double)image.Height Public AsciiArt GenerateAsciiArtFromImage( We’ll examine the code in more detail in the following sections: The final class of importance is the actual generator. The code _image allows us to get the pixel information from the image! This was done because you can actually implement this same functionality with the System.Drawing namespace and the Bitmap class, but it will only work on Windows. In the above code, we can see that we are implementing the IImageSource interface. Public void Dispose() => _image.Dispose() Public float AspectRatio => _image.Width / ( float)_image.Height Public ImageSharpImageSource(Image image) Internal sealed class ImageSharpImageSource : IImageSource Internal interface IImageSource : IDisposable Let’s look at the implementation for the image source next: When we look at the code for this class, we’ll be able to see the indexer method that allows us to get the pixel data for an X and Y coordinate. The ImageSharpImageSource class acts as a bridge between the ImageSharp library and our ASCII generation logic. The ImageSharp library is used to load the image and then clone it into a format ( Rgba32) that allows us to work with individual pixel colors. While implicit isn't always as obvious, an added benefit for readability here is that we don't have overly nested code. With this new language feature, when the variable goes out of scope, it will be the equivalent to the using scope ending as well. Previously, explicit using blocks used curly braces to denote the scope. In C# 8, we got this language feature which allows us to simplify our using statements. I just wanted to make a quick note about the using declaration syntax that we have here in case you're not familiar with it. The magic happens inside of the GenerateAsciiArtFromImage method, which we will look at shortly. We also instantiate our main Generator class, which will handle the ASCII conversion, along with the ImageSharpImageSource that will hold the image data. Here, we’re setting up the path to our image and creating a stream to read it. This is the entry point to our C# program. Var asciiArt = generator.GenerateAsciiArtFromImage(image)
Using var image = new ImageSharpImageSource(imageRgba32) Using var imageRgba32 = sourceImage.CloneAs() Using var sourceImage = Image.Load(inputStream) String imagePath = " your file path here" Thanks! Let’s look at some code (which, by the way, is available in full on GitHub):
Watch the video as you follow along! Example Code to Generate ASCII ArtĪlright, you toughed it out through my introduction. I’ve included this list not just because I think it’s pretty cool, but to get your creative juices flowing! Think about the different things you want to focus on as a developer and see if you can incorporate them into your ASCII art generator!īeing able to leverage simple programs like this takes the stress away from “what’s the right thing to build” and allows you to focus on learning and exploring. I regularly remind my audience that I think building things and actually writing code is one of the absolute best ways to learn.Īs we navigate this code together, I want you to keep this in mind! At the end of the article, I propose some variations and enhancements that you may want to consider. They are trying to read books, articles, and blog posts (just like this one!) to learn theory, or watching videos and trying to find the best BootCamp so they have the best chance of success. Many times, beginner programmers are stuck in some of the early phases of learning because they are not sure how to allocate their time. I get it! But that’s why I want to put an important message beforehand, especially for the more junior developers. I realize many of you coming here are just looking to jump directly into the code. Before I Provide Code to Generate ASCII Art Not only will you have the full source code to have a functioning C# app that can generate ASCII art, but I’ll also explain why simple programs like this can be critical for helping you learn. In this guide, we’ll walk through a C# approach to transform standard images and generate ASCII art from them. For those new to programming, building a program to generate ASCII art can serve as an insightful introduction. It’s a fascinating way to represent images without the need for traditional graphics. ASCII art, a technique of creating visuals using characters from the ASCII standard, has been a part of the computing world for decades.