A PictureBox displays an image. The image can be one of several formats, such as bitmap, GIF (Graphics Interchange Format) and JPEG. (Images are discussed in Chapter 17, Graphics and Multimedia.) A PictureBox's Image property specifies the image that is displayed, and the SizeMode property indicates how the image is displayed (Normal, StretchImage, Autosize or CenterImage). Figure 13.29 describes common PictureBox properties and a common event.

Figure 13.29. PictureBox properties and event.

PictureBox properties and event


Common Properties


Sets the image to display in the PictureBox.


Enumeration that controls image sizing and positioning. Values are Normal (default), StretchImage, AutoSize and CenterImage. Normal places the image in the top-left corner of the PictureBox, and CenterImage puts the image in the middle. These two options truncate the image if it is too large. StretchImage resizes the image to fit in the PictureBox. AutoSize resizes the PictureBox to hold the image.

Common Event


Occurs when the user clicks the control. When you double click this control in the designer, an event handler is generated for this event.

Figure 13.30 uses a PictureBox named imagePictureBox to display one of three bitmap imagesimage0, image1 or image2. These images are located in the images directory in the project's bin/Debug and bin/Release directories. Whenever a user clicks the Next Image Button, the image changes to the next image in sequence. When the last image is displayed and the user clicks the Next Image Button, the first image is displayed again. Inside event handler nextButton_Click (lines 2028), we use an int (imageNum) to store the number of the image we want to display. We then set the Image property of imagePictureBox to an Image (lines 2527).

Figure 13.30. Using a PictureBox to display images.

(This item is displayed on pages 627 - 628 in the print version)

 1 // Fig. 13.30: PictureBoxTestForm.cs
 2 // Using a PictureBox to display images.
 3 using System;
 4 using System.Drawing;
 5 using System.Windows.Forms;
 6 using System.IO;
 8 // Form to display different images when PictureBox is clicked
 9 public partial class PictureBoxTestForm : Form
10 {
11 private int imageNum = -1; // determines which image is displayed
13 // default constructor
14 public PictureBoxTestForm()
15 {
16 InitializeComponent();
17 } // end constructor
19 // change image whenever Next Button is clicked
20 private void nextButton_Click( object sender, EventArgs e )
21 {
22 imageNum = ( imageNum + 1 ) % 3; // imageNum cycles from 0 to 2
24 // create Image object from file, display in PicutreBox
25 imagePictureBox.Image = Image.FromFile( 
26  Directory.GetCurrentDirectory() + @"imagesimage" +
27  imageNum + ".bmp" ); 
28 } // end method nextButton_Click
29 } // end class PictureBoxTestForm






    Introduction to Computers, the Internet and Visual C#

    Introduction to the Visual C# 2005 Express Edition IDE

    Introduction to C# Applications

    Introduction to Classes and Objects

    Control Statements: Part 1

    Control Statements: Part 2

    Methods: A Deeper Look


    Classes and Objects: A Deeper Look

    Object-Oriented Programming: Inheritance

    Polymorphism, Interfaces & Operator Overloading

    Exception Handling

    Graphical User Interface Concepts: Part 1

    Graphical User Interface Concepts: Part 2


    Strings, Characters and Regular Expressions

    Graphics and Multimedia

    Files and Streams

    Extensible Markup Language (XML)

    Database, SQL and ADO.NET

    ASP.NET 2.0, Web Forms and Web Controls

    Web Services

    Networking: Streams-Based Sockets and Datagrams

    Searching and Sorting

    Data Structures



    Appendix A. Operator Precedence Chart

    Appendix B. Number Systems

    Appendix C. Using the Visual Studio 2005 Debugger

    Appendix D. ASCII Character Set

    Appendix E. Unicode®

    Appendix F. Introduction to XHTML: Part 1

    Appendix G. Introduction to XHTML: Part 2

    Appendix H. HTML/XHTML Special Characters

    Appendix I. HTML/XHTML Colors

    Appendix J. ATM Case Study Code

    Appendix K. UML 2: Additional Diagram Types

    Appendix L. Simple Types


    Visual C# How to Program
    Visual C# 2005 How to Program (2nd Edition)
    ISBN: 0131525239
    EAN: 2147483647
    Year: 2004
    Pages: 600

    Flylib.com © 2008-2020.
    If you may any questions please contact us: flylib@qtcs.net