private static ColorInfo[,] AverageColorInfo(Bitmap image, int lineHeight, int lineWidth) { int width = image.Width / lineWidth; int height = image.Height / lineHeight; int pixelCount = width * height; // foreach goes through the rightmost index first ColorInfo[,] output = new ColorInfo[lineWidth, lineHeight]; for (int y = 0; y < lineHeight; y++) { for (int x = 0; x < lineWidth; x++) { float totalBright = 0; int totalRed = 0; int totalGreen = 0; int totalBlue = 0; for (int a = x * width; a < (x + 1) * width; a++) { for (int b = y * height; b < (y + 1) * height; b++) { Color pixel = image.GetPixel(a, b); totalBright += pixel.GetBrightness(); totalRed += pixel.R; totalGreen += pixel.G; totalBlue += pixel.B; } } float avgBright = totalBright / pixelCount; byte avgRed = (byte)(totalRed / pixelCount); byte avgGreen = (byte)(totalGreen / pixelCount); byte avgBlue = (byte)(totalBlue / pixelCount); output[x, y] = new ColorInfo(avgRed, avgGreen, avgBlue, avgBright); } } return(output); }
private static void WriteToText(ColorInfo[,] tiles) { using (StreamWriter sw = new StreamWriter("out.txt", false, Encoding.UTF8)) { for (int y = 0; y < tiles.GetLength(1); y++) { for (int x = 0; x < tiles.GetLength(0); x++) { ColorInfo info = tiles[x, y]; if (info.Brightness > 0.875) { sw.Write(' '); } else if (info.Brightness > 0.625) { sw.Write(Strings.ChrW('\u2591')); } else if (info.Brightness > 0.375) { sw.Write(Strings.ChrW('\u2592')); } else if (info.Brightness > 0.125) { sw.Write(Strings.ChrW('\u2593')); } else { sw.Write(Strings.ChrW('\u2588')); } } sw.WriteLine(); } } }