Skip to content

Instantly share code, notes, and snippets.

@Wampa842
Created December 8, 2021 11:55
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 Wampa842/c30ae509927cdc9fc971407d5be239ad to your computer and use it in GitHub Desktop.
Save Wampa842/c30ae509927cdc9fc971407d5be239ad to your computer and use it in GitHub Desktop.
Chequered bitmap
public static class GridBitmap
{
public static Bitmap Create(int repeatX, int repeatY, Size bitmapSize, Color foreground, Color background)
{
// Check for division by zero
if (repeatX == 0 || repeatY == 0)
throw new ArgumentException("Division by zero.");
Bitmap bmp = new Bitmap(bitmapSize.Width, bitmapSize.Height);
Graphics g = Graphics.FromImage(bmp);
Brush fgBrush = new SolidBrush(foreground);
g.Clear(background);
SizeF size = new SizeF((float)bmp.Width / repeatX, (float)bmp.Height / repeatY);
for (int x = 0; x < repeatX; x += 2)
{
for (int y = 0; y < repeatY; ++y)
{
g.FillRectangle(fgBrush, (x + (y % 2)) * size.Width, y * size.Height, size.Width, size.Height);
}
}
g.Dispose();
return bmp;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment