Skip to content

Instantly share code, notes, and snippets.

@chrisforbes
Created November 12, 2009 21:18
Show Gist options
  • Save chrisforbes/233283 to your computer and use it in GitHub Desktop.
Save chrisforbes/233283 to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
namespace codeshape
{
static class Program
{
static void Main(string[] args)
{
var exts = args.First().Split(',');
var sourceFiles = args.Skip(1).SelectMany(a => exts.SelectMany(e => Directory.GetFiles(a, "*." + e, SearchOption.AllDirectories)));
var codeLines = sourceFiles.SelectMany(a => File.ReadAllLines(a))
.Where(a => a.Trim() != "")
.Select(a => a.Replace("\t", " "));
var codeImages = codeLines.Select(a => a.ToBitmap());
var finalWidth = codeImages.Max(a => a.Width);
var finalHeight = codeImages.Count();
var factor = 10;
var colHeight = finalHeight / factor;
var b = new Bitmap(finalWidth * factor, colHeight);
var g = Graphics.FromImage(b);
g.FillRectangle(Brushes.White, 0, 0, finalWidth * factor, finalHeight / factor);
var i = 0;
foreach (var line in codeImages)
g.DrawImageUnscaled(line, (i / colHeight) * finalWidth, (i++ % colHeight));
g.Flush();
b.Save("c:\\users\\chrisf\\desktop\\codeimage.png", ImageFormat.Png);
}
static Bitmap ToBitmap(this string text)
{
var b = new Bitmap(text.Length, 1);
for (var i = 0; i < text.Length; i++)
b.SetPixel(i, 0, (text[i] == ' ') ? Color.White : Color.Black);
return b;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment