I'm trying to draw a 8x8 chess board using C#. Here's my first attempt to draw it. It won't draw the board and I haven't found what I'm missing.
public void Form1_Load(object sender, EventArgs e)
{
Bitmap bm = new Bitmap(8 * 100, 8 * 100);
Graphics g = Graphics.FromImage(bm);
Color color1, color2;
for (int i = 0; i < 8; i++)
{
if (i % 2 == 0)
{
color1 = Color.Black;
color2 = Color.White;
}
else
{
color1 = Color.White;
color2 = Color.Black;
}
SolidBrush blackBrush = new SolidBrush(color1);
SolidBrush whiteBrush = new SolidBrush(color2);
for (int j = 0; j < 8; j++)
{
if (j % 2 == 0)
g.FillRectangle(blackBrush, i * 100, j * 100, 100, 100);
else
g.FillRectangle(whiteBrush, i * 100, j * 100, 100, 100);
}
}
g.DrawImage(bm, 150, 200);
}
add BackgroundImage = bm; to the bottom of your code.
You are drawing the board fine, just not displaying the bitmap...
Edit: im not sure if you're interested, but i rewrote this code.
int sqaureWidth = 100;
int sqaureHeight = 100;
int rowCount = 8;
int columnCount = 8;
Bitmap bm = new Bitmap(sqaureWidth * columnCount, sqaureHeight * rowCount);
using (Graphics g = Graphics.FromImage(bm))
{
for (int i = 0; i < columnCount; i++)
{
for (int j = 0; j < rowCount; j++)
{
if ((j % 2 == 0 && i % 2 == 0) || (j % 2 != 0 && i % 2 != 0))
g.FillRectangle(Brushes.White, i * sqaureWidth, j * sqaureHeight, sqaureWidth, sqaureHeight);
else if ((j % 2 == 0 && i % 2 != 0) || (j % 2 != 0 && i % 2 == 0))
g.FillRectangle(Brushes.Black, i * sqaureWidth, j * sqaureHeight, sqaureWidth, sqaureHeight);
}
}
}
BackgroundImage = bm;
also this project could help if you want to make a chess game: http://www.codeproject.com/Articles/20736/C-C-CLI-Micro-Chess-Huo-Chess
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With