C# 从类到窗体的图形

C# 从类到窗体的图形,c#,windows,c#-4.0,graphics,picturebox,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);

好的,所以我需要用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();
 }

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

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

通过您的
测试
对表单上的
PictureBox
的a类引用:

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
}

您也可以通过
表单本身(使用
this
),但是您仍然需要标识
PictureBox
控件(我假设)。

通过
测试
对表单上
PictureBox
的a类引用:

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
}

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

我看不到您的
PictureBox
显示在哪里。以前它是作为子控件“在窗体上”的吗,但现在你不这样做了?你是想在窗体上创建一个新的
PictureBox
,还是在现有的
PictureBox
上写入?我看不到你的
PictureBox
显示在哪里。以前它是作为子控件“在窗体上”的吗,但现在你不这样做了?你是想在窗体上创建一个新的
PictureBox
,还是在现有的
PictureBox
上写入?