graphics从一个类到一个窗体

好的,所以我需要在c#中使用一个简单的animation作为加载图标。 这一切都很好,所以让我们以这个广场为例

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; 

所以,我做了我的animation和一切在这里工作,但后来我意识到,我需要我的animation在一个类,所以我可以从我的同事们的程序调用它来使用animation。 这是问题出现的地方,我做了我的课,我做了所有的事情,但在一个class,而不是forms,然后从我的表单叫我的class,但屏幕是空白的,没有animation。 有什么需要通过才能做到这一点?

 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(); } 

将您的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控件(我假设)。

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