简体   繁体   English

如何从方法中调用“绘制圆”以在图片框内绘制

[英]How to call 'draw circle' from method to draw inside picturebox

I don't know what to pass to my method to call my Circle drawing properly. 我不知道该如何传递给我的方法以正确调用我的Circle绘图。 Here is my code: 这是我的代码:

    private void Circle()
    {
        Graphics g1 = e.Graphics;
        Pen p1 = new Pen(Color.Black);
        g1.DrawEllipse(p1, 12, 12, 50, 50);
    }

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        Circle();
    }

This is not working , cause there is no 'e'. 这不起作用,因为没有'e'。

If i rewrite my code from 如果我从重写我的代码

Graphics g1 = e.Graphics;

to

Graphics g1 = this.CreateGraphics();

It will draw circle but not in the picturebox. 它将绘制圆形,但不在画框中。 I need a circle to be inside picturebox. 我需要在画框内画一个圆圈。

If your method requires a reference to the PaintEventArgs , then why not supply it one? 如果您的方法需要引用PaintEventArgs ,那么为什么不提供它呢? Something like this: 像这样:

private void Circle(PaintEventArgs e)
{
    Graphics g1 = e.Graphics;
    Pen p1 = new Pen(Color.Black);
    g1.DrawEllipse(p1, 12, 12, 50, 50);
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    Circle(e);
}

This would allow other paint event handlers to make use of that method for their own controls as well. 这将允许其他绘画事件处理程序也将该方法用于其自己的控件。

To get a reference to a Graphics object you can provide that via the parameters. 要获取对Graphics对象的引用,可以通过参数提供该引用。 Like 喜欢

private void Circle(Graphics g)
{
    Pen p1 = new Pen(Color.Black);
    g.DrawEllipse(p1, 12, 12, 50, 50);
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    Graphics myg = e.Graphics;
    Circle(myg);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM