我目前正在使用pictureBox.我很难在图片框上画一个大约2 x 2正方形的网格.现在,下面的代码只给我一条画线.我怎样才能在pictureBox上绘制一个完整的网格?
码:
private Graphics g1;
public Form1()
{
InitializeComponent();
pictureBox1.Image = new Bitmap(500, 500);
g1 = Graphics.FromImage(this.pictureBox1.Image);
Pen gridPen = new Pen(Color.Black, 2);
g1.DrawLine(gridPen, 0, 0, 100, 100);
}
Run Code Online (Sandbox Code Playgroud)
这就是我想要完成的事情:

我发现了这个问题: 在Windows窗体中有效地绘制网格
以下是它的要点:
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
int numOfCells = 200;
int cellSize = 5;
Pen p = new Pen(Color.Black);
for (int y = 0; y < numOfCells; ++y)
{
g.DrawLine(p, 0, y * cellSize, numOfCells * cellSize, y * cellSize);
}
for (int x = 0; x < numOfCells; ++x)
{
g.DrawLine(p, x * cellSize, 0, x * cellSize, numOfCells * cellSize);
}
}
Run Code Online (Sandbox Code Playgroud)
相应地自定义