Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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