Skip to content

Instantly share code, notes, and snippets.

@einarwh
Created November 2, 2011 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save einarwh/1334908 to your computer and use it in GitHub Desktop.
Save einarwh/1334908 to your computer and use it in GitHub Desktop.
Create a pix-it image.
private static Image CreateImage(PixItData data)
{
int width = data.PixelSize * data.PixelsWide;
int height = data.PixelSize * data.PixelsHigh;
var image = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(image))
{
if (data.Background.HasValue)
{
Color bgColor = data.Background.Value;
using (var brush = new SolidBrush(bgColor))
{
g.FillRectangle(brush, 0, 0,
data.PixelSize * data.PixelsWide,
data.PixelSize * data.PixelsHigh);
}
}
foreach (Color color in data.Data.Keys)
{
using (var brush = new SolidBrush(color))
{
foreach (Pixel p in data.Data[color])
{
g.FillRectangle(brush,
p.X*data.PixelSize,
p.Y*data.PixelSize,
data.PixelSize,
data.PixelSize);
}
}
}
}
return image;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment