Visual Basic 2005 for Programmers (2nd Edition)
17.11. Loading, Displaying and Scaling Images
Visual Basic's multimedia capabilities include graphics, images, animations and video. Previous sections demonstrated vector-graphics capabilities; this section presents image manipulation. The application in Fig. 17.23 loads an Image (System.Drawing namespace), then allows the user to scale the Image to a specified width and height. Figure 17.23. Image resizing.
Line 4 declares Image variable image. Line 11 in the Form's Load event handler uses Shared Image method FromFile to load an image from a file on disk. Line 12 uses the Form's CreateGraphics method to create a Graphics object for drawing on the Form. Method CreateGraphics is inherited from class Control. When you click the Set Button, lines 2427 validate the width and height to ensure that they are not too large. If the parameters are valid, line 30 calls Graphics method Clear to paint the entire Form in the current background color. Line 31 calls Graphics method DrawImage, passing as arguments the image to draw, the x-coordinate of the image's upper-left corner, the y-coordinate of the image's upper-left corner, the width of the image and the height of the image. If the width and height do not correspond to the image's original dimensions, the image is scaled to fit the new width and height. |