I'm currently working on an image-to-ASCII converter in C# and ran into a problem. I want the program to run through every single pixel, get the brightness and add the right character in. But in my code it only checks the first pixel of the image, so pixel 0-0, and prints it in the .txt file successfully without showing any error messages. I've tried using StreamWriter, but the same result came out, which means that it might have something to do with my for loop. What have I done wrong?
Bitmap bm = new Bitmap(source);
int width = bm.Width; // Image width
int height = bm.Height; // Image height
for (int i = 0; i < width; i++)
{
int w = 0;
w++;
int h = 0;
file = source + " ASCII.txt";
if(i > width)
{
h++;
}
System.Drawing.Color pixelColor = bm.GetPixel(i, h);
float brightness = pixelColor.GetBrightness();
using (StreamWriter writer = File.CreateText(file))
{
writer.Close(); // Creating file
}
// ASCII print
if (brightness < 0.14)
{
File.AppendAllText(file, "@");
if (w > width)
{
File.AppendAllText(file, "\n");
}
}
if (brightness > 0.14 && brightness < 0.28)
{
File.AppendAllText(file, "%");
if (w > width)
{
File.AppendAllText(file, "\n");
}
}
if (brightness > 0.28 && brightness < 0.42)
{
File.AppendAllText(file, "#");
if (w > width)
{
File.AppendAllText(file, "\n");
}
}
if (brightness > 0.42 && brightness < 0.56)
{
File.AppendAllText(file, "*");
if (w > width)
{
File.AppendAllText(file, "\n");
}
}
if (brightness > 0.56 && brightness < 0.7)
{
File.AppendAllText(file, "+");
if (w > width)
{
File.AppendAllText(file, "\n");
}
}
if (brightness > 0.7 && brightness < 0.84)
{
File.AppendAllText(file, "=");
if (w > width)
{
File.AppendAllText(file, "\n");
}
}
if (brightness > 0.84 && brightness < 0.98)
{
File.AppendAllText(file, "-");
if (w > width)
{
File.AppendAllText(file, "\n");
}
}
if (brightness > 0.98)
{
File.AppendAllText(file, ":");
if (w > width)
{
File.AppendAllText(file, "\n");
}
}
}