你使用winforms吗?如果是,您实际上不需要工作区的图片框。我认为更合适的是表单或面板上的Graphics 类。由于表单重绘圆圈,您已经丢失了线条,将您的绘图代码放入表单绘制处理程序中,并且在需要时会重新绘制图片。在某些情况下,您可能需要手动触发重绘圆圈,为此您应该使用表单的Invalidate 方法。
例如,将此代码添加到绘图处理程序:
private void Form1_Paint(object sender, PaintEventArgs e)
{
// Drawing vertical lines
for (int x = 5; x < this.ClientRectangle.Width; x+=5)
{
e.Graphics.DrawLine(Pens.Gray, new Point(x, 0), new Point(x, this.ClientRectangle.Height));
}
// Drawing horisontal lines
for (int y = 5; y < this.ClientRectangle.Width; y += 5)
{
e.Graphics.DrawLine(Pens.Gray, new Point(0, y), new Point(this.ClientRectangle.Width,y));
}
}
您也可以通过这种方式在按钮单击处理程序中使用图形:
Graphics g = Graphics.FromHwnd(this.Handle);
g.FillEllipse(Brushes.Beige, new Rectangle(10, 10, 10, 10));
但是在这种情况下,您绘制的所有内容都将在表单的重绘循环期间被删除,您将不得不在表单绘制处理程序中重复它
[编辑]
好的,例如您的表单上有pictureBox1,您可以通过Bitmap 类以这种方式轻松绘制:
// Draw into bitmap
Bitmap bmp = new Bitmap(150, 150);
Graphics g = Graphics.FromImage(bmp);
g.FillRectangle(Brushes.Green, new Rectangle(25, 75, 10, 30));
// Set bitmap into picture box
pictureBox1.Image = bmp;
在这种情况下,您无需重新绘制绘画,图片框会为您完成。如果您喜欢从图片框下方显示绘画,请不要忘记将BackColor ot图片框设置为Transparent。