从一个类到表单的图形

时间:2013-06-20 19:54:56

标签: c# windows c#-4.0 graphics picturebox

好的,所以我需要在c#中制作一个简单的动画作为加载图标。这一切都很好,所以让我们把这个方块作为一个例子

   PictureBox square = new PictureBox();
   Bitmap bm = new Bitmap(square.Width, square.Height);
   Graphics baseImage = Graphics.FromImage(bm);
   baseImage.DrawRectangle(Pens.Black, 0, 0, 100, 100);
   square.Image = bm;

因此,我制作了动画,一切都在这里工作,但后来我意识到我需要我的动画才能上课,所以我可以从我的同事程序中调用它来使用动画。这就是问题出现的地方,我创建了我的课,我以相同的方式做了所有事情,但是在课堂而不是表格中,我从表单中调用了我的课程,但屏幕是空白的,没有动画。为了做到这一点,是否需要传递一些东西?

namespace SpinningLogo
{//Here is the sample of my class
    class test
    {
        public void square()
        {
            PictureBox square = new PictureBox();
            Bitmap bm = new Bitmap(square.Width, square.Height);
            Graphics baseImage = Graphics.FromImage(bm);
            baseImage.DrawRectangle(Pens.Black, 0, 0, 100, 100);
            square.Image = bm;
        }

    }
}
private void button1_Click(object sender, EventArgs e)
{//Here is how I call my class
    Debug.WriteLine("11");
    test square = new test();
    square.square();
 }

2 个答案:

答案 0 :(得分:1)

将您的test课程引用到表单上的PictureBox

namespace SpinningLogo
{
    class test
    {
        public void square(PictureBox thePB)
        {
            Bitmap bm = new Bitmap(thePB.Width, thePB.Height);
            Graphics baseImage = Graphics.FromImage(bm);
            baseImage.DrawRectangle(Pens.Black, 0, 0, 100, 100);
            thePB.Image = bm;
        }

    }
}

private void button1_Click(object sender, EventArgs e)
{
    test square = new test();
    square.square(myPictureBox);  //whatever the PictureBox is really named
}

您也可以传递Form本身(使用this),但是您仍需要识别PictureBox控件(我假设)。

答案 1 :(得分:0)

您应该传递给测试类Form实例,而不是在测试类中定义PictureBox。 PictureBox应该是Form的字段,通过Form实例,你可以访问你的PictureBox。