Windows Develop Bookmark and Share   
 index > Windows Forms General > dynamically setting the size of picture box
 

dynamically setting the size of picture box

Can anyone tell me how to dynamically set the width and height of a picture box based on the size of the image that will be displayed in it? The image will be chosen by the user at run time using an open file dialog box. Any ideas would help me a lot.

smtraber
smtraber  Wednesday, September 07, 2005 8:32 AM
The easiest way is to simply set the SizeMode property of the PictureBox to PictureBoxSizeMode.AutoSize in the designer.
David M. Kean  Wednesday, September 07, 2005 9:17 AM
Hi,

Set the PictureBox's Width and Height with it's Image's Width and Height:

pictureBox1.Width = pictureBox1.Image.Width;
pictureBox1.Height = pictureBox1.Image.Height;

// Or

pictureBox1.Size = new Size(pictureBox1.Image.Width,pictureBox1.Image.Height);


Alternatively, if your Image is stored on an Image variable, set the size of your PictureBox like this:

pictureBox1.Width = myImage.Width;
pictureBox1.Height = myImage.Height;

// Or

pictureBox1.Size = new Size(myImage.Width,myImage.Height);


Regards,

-chris
Chris Vega  Wednesday, September 07, 2005 8:46 AM
The easiest way is to simply set the SizeMode property of the PictureBox to PictureBoxSizeMode.AutoSize in the designer.
David M. Kean  Wednesday, September 07, 2005 9:17 AM

You can use google to search for other answers

Custom Search

More Threads

• How can i find the user?
• Smoother Refresh
• How Windows Forms communicate with Windows Services?
• Word.selection.find.execute() ---- search and replace only work on the first search but not on second, third etc. Plz helps.
• Deployment question (what to upload)
• Is ICloneable has something to do with the error: Unable to cast object of type 'X' to type 'Y'
• Pop up notification
• HTML generated by Webbrowser control
• Hiding Selection with a ListBox
• CodeHero's