954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Scaling an object at runtime

In my Forms application i tried to resize a pictureBox control during runtime
and it seems to give some trouble. There is scale factor scalex , scaley which i applied
to the width and height property of the control.

tempw = 980.0f;   
temph = 130.0f;   
tempw = float.Parse(pictureBox1.Width.ToString());    
temph = float.Parse(pictureBox1.Height.ToString());   
tempw = pictureBox1.Width;
temph = pictureBox1.Height;
pictureBox1.Width = (int)(tempw * scaleX);            
pictureBox1.Height =(int)(temph * scaleY);


when i use lines 1&2 there is no problem but when i use lines 3&4 or 5&6 the box expands
to a large value and i get a "out of memory exception".All my variables are float values.But during debug the value of final Width, Height values seem to be what i expected.

deepak_1706
Newbie Poster
7 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 
CloneXpert
Junior Poster in Training
64 posts since Aug 2010
Reputation Points: 30
Solved Threads: 19
 

When are you calling that code?

Ryshad
Nearly a Posting Virtuoso
1,307 posts since Aug 2009
Reputation Points: 512
Solved Threads: 246
 
When are you calling that code?

The code is written in the OnPaint event of the Form. But i have overridden the OnPaint
As follows...

protected override void OnPaint(PaintEventArgs e)
{
      base.OnPaint(e);
      //some more code for drawing
}
deepak_1706
Newbie Poster
7 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

Hi,

Have a look at this please: http://social.msdn.microsoft.com/Forums/en-ZA/vbgeneral/thread/89474c0d-bb68-4bdf-b23d-8d593fc75073 good luck!:)

I looked at ur suggestion, thanks..
but what i am trying to do here is different. Transformation scales the existing bitmap image. But i need to draw my bitmap at runtime using the GDI+.

The picturebox is scaled so as to maintain the relative location wrt other controls on the screen when the app is run on diff resolutions.

deepak_1706
Newbie Poster
7 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

The Form_Paint event is called every time part of the UI is repainted, every time it fires your picturebox grows, eventually getting so large it is too large for the programs allocated memory.

If you want to resize your controls according to the size of the form then you need to adjust it relative to the form; you could use a similar approach to the one used in webdesign and calculate the height and width of controls as a percentage of their container:

Panel1.Width = Form1.Width * 0.5 //set width to 50% of form
Button1.Width = Panel1.Width * 0.2 //button is 20% of the panel it is placed in
//etc


If you want to alter them according to a fixed scale then you need to apply the scale ONCE when the form first loads, rather than every time it is refreshed.

Ryshad
Nearly a Posting Virtuoso
1,307 posts since Aug 2009
Reputation Points: 512
Solved Threads: 246
 

The Form_Paint event is called every time part of the UI is repainted, every time it fires your picturebox grows, eventually getting so large it is too large for the programs allocated memory.

If you want to resize your controls according to the size of the form then you need to adjust it relative to the form; you could use a similar approach to the one used in webdesign and calculate the height and width of controls as a percentage of their container:

Panel1.Width = Form1.Width * 0.5 //set width to 50% of form
Button1.Width = Panel1.Width * 0.2 //button is 20% of the panel it is placed in
//etc

If you want to alter them according to a fixed scale then you need to apply the scale ONCE when the form first loads, rather than every time it is refreshed.

Now i got what happened. When i used the value from the size property , each time the size has already increased due to the modification i had made. Hence leading to the
ultimate memory overflow. Thanks now i intend to use the actual number instead of the
size property.

Thanks a lot RYSHAD for bringing this to my notice.

deepak_1706
Newbie Poster
7 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: