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.

Recommended Answers

All 6 Replies

When are you calling that code?

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
}

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.

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.