Custom Control Problem

  • Thread starter Thread starter anand
  • Start date Start date
A

anand

Hi all,

I have created a custom control which displays Image using Graphics..

I am using this control in a different windows application.
I am dynamically creating the control and putting that in the form.

In the Control's InitializeComponent() function following statement is
present

class MyControl
{
InitializeComponent()
{
this.Size = new System.Drawing.Size(150, 150);
}

DisplayImage(ImageFileName)
{
Graphics.DrawImage(...);
}
}

While creating the control dynamically I am using following statements

class Client
{
AddControl()
{
MyControl control = new MyControl();
control.Left = 10;
control.Top = 10;
control.Width = 100;
control.Height = 100;
}
}

and when I call control.DisplayImage(ImageFileName);
the function MyControl.DisplayImage() shows image correctly.
But If in AddControl() I change the control.Width > 150 or control.Height >
150.
The control does not display the image after 150.
But the trace statements in MyControl.DisplayImage() shows all the values
correctly.

Can anybody help me how to add controls dynamically, without this problem.?

Thank you in Advance.
-Anand
 
Anand,

The problem has nothing to do with the fact that you are adding the
controls dynamically. Basically, the problem arises from the fact that you
are painting the image once on the screen, and when the screen is
invalidated, you no longer know what to paint.

What you want to do is store the image passed to you in the DisplayImage
method (store the Image instance). Once you have that, call Invalidate on
yourself to cause a repaint.

Finally, override the OnPaint method of your control so that you draw
the image to the Graphics object passed to you there.

Hope this helps.
 
anand,

The destRect in the drawImage function is the tricky part.When the custom
control is resized, the destRect value must be updated, for the image to
automatically streched.

If your problem still exists, can you paste the code of DrawImage?

Shak.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top