JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Adjusting Picture Box Size to Contents

You've displayed the image of the company's Illustrious Founder in a picture box in your new program—but the picture box was a little small, and you can only see most of the I.F.'s forehead. There's some email waiting for you from the president's office, and you think you know what it says. How can you make sure picture boxes readjust themselves to fit the picture they're displaying?

When you load a picture into a picture control, it does not readjust itself to fit the picture (although image controls do)—at least, not by default. Picture boxes will resize themselves to fit their contents if you set their SizeMode property; here are the possible values, which come from the PictureBoxSizeMode enumeration:

  • Normal— Standard picture box behavior (the upper-left corner of the image is placed at upper left in the picture box).

  • StretchImage— Allows you to stretch the image in code.

  • AutoSize— Fits the picture box to the image.

  • CenterImage— Centers the image in the picture box.

Here's an example from the PictureBoxes example on the CD-ROM; in this case, I set a picture box's SizeMode property to StretchImage and then give it a new size this way:

Private Sub Button2_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button2.Click
    PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
    PictureBox1.ClientSize = New Size(300, 100)
End Sub

You can see the stretched image in Figure 7.17.

Click To expand
Figure 7.17: Stretching an image in a picture box.
Previous Section Next Section

JavaScript Editor Free JavaScript Editor     JavaScript Editor