Quantcast
Viewing all articles
Browse latest Browse all 12583

What is the correct way to draw Graphics from another control into a picturebox in the main form?

I'm drawing graphics on a picturebox by passing the Graphics parameter to the method in a custom control.

Basically, I draw a line on my picturebox. Then I pass the Graphics reference to the method of a custom control which in turn draws some text string on my picturebox.

Here is the code snippet (c++/cli for the mainform and c# for the custom control):

//Main Form.h picturebox pain event
MainFunction()
{
	Pen^ aPen = gcnew Pen(Color::Cyan);
	Pen^ aRedPen = gcnew Pen(Color::Red);
	Graphics^ myGraph = e->Graphics;
	myGraph->Clear(Color.Black);
	myGraph->DrawLine(aRedPen, Point(50,100), Point(30,49));
	if (addSomeText)
	{
		Pen^ aPen = gcnew Pen(Color::Red);
		aPen->Width = 1.2;
		customcontorl->anotherControl(myGraph, aPen);
	}
}

In a custom control I have such a method...

/method in custom control.cs class
public void anotherControl(ref Graphics g, ref Pen aPen)
{
		Font drawFont = new Font("Arial", 16);
		SolidBrush drawBrush = new SolidBrush(aPen.Color);
		StringFormat drawFormat = new StringFormat();
		g.DrawString("Some text...", drawFont, drawBrush, new Point(3, 30));
		drawFont.Dispose();
		drawBrush.Dispose();
		drawFormat.Dispose();
}
The strange thing is that I cannot clear the Graphics.
What is the correct way to draw Graphics from another control into a picturebox in the main form?

Viewing all articles
Browse latest Browse all 12583

Trending Articles