30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

716 Graphics and Multimedia Chapter 16<br />

Fig. 16.23 Image resizing (part 3 of 3).<br />

Line 16 declares Image reference mImage. The Shared Image method From-<br />

File then retrieves an image s<strong>to</strong>red on disk and assigns it <strong>to</strong> mImage (line 27). Line 24<br />

uses Form method CreateGraphics <strong>to</strong> create a Graphics object associated with the<br />

Form; we use this object <strong>to</strong> draw on the Form. Method CreateGraphics is inherited<br />

from class Control; all Windows controls, such as But<strong>to</strong>ns and Panels, also provide<br />

this method. When users click Set, the width and height parameters are validated <strong>to</strong> ensure<br />

that they are not <strong>to</strong>o large. If the parameters are valid, line 46 calls Graphics method<br />

Clear <strong>to</strong> paint the entire Form in the current background color. Line 49 calls Graphics<br />

method DrawImage with the following parameters: the image <strong>to</strong> draw, the x-coordinate<br />

of the upper-left corner, the y-coordinate of the upper-left corner, the width of the image<br />

and the height of the image. If the width and height do not correspond <strong>to</strong> the image’s original<br />

dimensions, the image is scaled <strong>to</strong> fit the new specifications.<br />

16.11 Animating a Series of Images<br />

The next example animates a series of images s<strong>to</strong>red in an array. The application uses the<br />

same techniques <strong>to</strong> load and display Images as those illustrated in Fig. 16.23. The images<br />

were created with Adobe Pho<strong>to</strong>shop.<br />

The animation in Fig. 16.24 uses a PictureBox, which contains the images that we<br />

animate. We use a Timer <strong>to</strong> cycle through the images, causing a new image <strong>to</strong> display<br />

every 50 milliseconds. Variable count keeps track of the current image number and<br />

increases by one every time we display a new image. The array includes 30 images (numbered<br />

0–29); when the application reaches image 29, it returns <strong>to</strong> image 0. The 30 images<br />

were prepared in advance with a graphics software package and placed in the images<br />

folder inside the bin/Debug direc<strong>to</strong>ry of the project.<br />

Lines 19–22 load each of 30 images and place them in an ArrayList. ArrayList<br />

method Add allows us <strong>to</strong> add objects <strong>to</strong> the ArrayList; we use this method in lines 20–<br />

21 <strong>to</strong> add each Image. Line 25 places the first image in the PictureBox, using the

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!