在我的代码中,我有PictureBox
一个背景图片。我曾经用它在它上面画一个矩形
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Pen p = new Pen(Color.Turquoise, 2);
Rectangle r = new Rectangle(600, 300, 5, 5);
e.Graphics.DrawRectangle(p, r);
p.Dispose();
}
Run Code Online (Sandbox Code Playgroud)
现在,我知道我需要对这些矩形做很多事情并动态创建它们,我为它们创建了一个类,构造函数如下所示:
public MyRectangles(int x, int y)
{
Pen p = new Pen(Color.Turquoise, 2);
Rectangle r = new Rectangle(x, y, 5, 5);
e.Graphics.DrawRectangle(p, r);
p.Dispose();
}
Run Code Online (Sandbox Code Playgroud)
问题是,e
在e.Graphics.DrawRectangle(p, r);
这里不存在。这是有道理的,我理解为什么,但是,我不知道用什么替换它,再次在同一个图片框上绘制。
尝试传递 Graphics 对象:
public MyRectangles(Graphics g, int x, int y)
{
Pen p = new Pen(Color.Turquoise, 2);
Rectangle r = new Rectangle(x, y, 5, 5);
g.DrawRectangle(p, r);
p.Dispose();
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2175 次 |
最近记录: |