Working with the PictureBox Control

The PictureBox control is an ActiveX control you can use to display bitmaps, custom graphics, and text. The PictureBox control has properties and methods to define the drawing styles, colors, and sizes of custom graphics. In addition, the PictureBox can respond to mouse and keyboard events and changes in focus. The PictureBox also has events that enable it to respond when a change is made to the image or when the control is resized.

To draw a bitmap on the PictureBox control, you can set the Picture property to the path and file name for the .bmp file. When you draw a picture by setting the Picture property, the picture is drawn in the upper-left corner of the control. You can use the DrawPicture method to draw, clip, and rescale images. The DrawPicture method can draw the bitmap anywhere in the control.

In addition to pictures, you can add custom graphics and text with the DrawPoint, DrawLine, and DrawCircle methods. The output of the custom graphic methods depends on the DrawWidth, DrawStyle, FillColor, FillStyle and ScaleMode.

The ScaleMode property of a PictureBox control determines the units of measure the control uses. The width of the line used to draw custom graphics depends on the setting of the DrawWidth property. The way the graphic is drawn on the background depends on the setting of the DrawMode and DrawStyle properties.

Use the DrawText method to draw text anywhere on a PictureBox control. The Forecolor property and the various font properties determine the appearance of the text. Use the TextWidth method to determine the amount of horizontal space required to display the text, and use the TextHeight property to determine the amount of vertical space required to display the text.

All the text and graphic methods modify the bitmap in the control. You cannot undo changes. If you want to reset the bitmap in the control, use the Cls method to clear the control’s contents.