i have a class written in c# inherited from Control like below.
class MyImage:Control
{
private Bitmap bitmap;
public MyImage(int width, int height)
{
this.Width = width;
this.Height = height;
bitmap = new Bitmap(width,height);
Graphics gr = Graphics.FromImage(bitmap);
gr.FillRectangle(Brushes.BlueViolet,0,0,width,height);
this.CreateGraphics().DrawImage(bitmap,0,0);
}
}
And from my main form i create an object of this class. and add thid object to the form, like below.
private void button1_Click(object sender, EventArgs e)
{
MyImage m = new MyImage(100,100);
m.Left = 100;
m.Top = 100;
this.Controls.Add(m);
}
but it doesnt appear on the form. What is the problem.
Thanks.
You should not draw anything in a class constructor. You should override OnPaint method and draw all of your custom graphics here.
You can write someting like this:
The recepient code: