Export (0) Print
Expand All

Drawing an Image on a Form

In this lesson, you will learn how to display an image using graphics calls.

In an earlier lesson, you learned how to display an image using a PictureBox control. It is also possible to display an image from a file using the graphics methods in Visual Basic. As with the previous lesson, using the graphics methods instead of a PictureBox control is a necessity if you want to do something special, such as rotate the image.

To display an image on a form or control, use the DrawImage graphics method. The DrawImage method takes a bitmap image as an argument, along with the X and Y coordinates defining the upper-left corner of the image.

To display a rotated image

  1. On the File menu, choose New Project.

  2. On the Templates pane, in the New Project dialog box, click Windows Forms Application.

  3. In the Name box, type DrawImage and then click OK.

    A new Windows Forms project opens.

  4. In Solution Explorer, double-click the My Project node to open the Project Designer.

  5. In the Project Designer, click the Resources tab, select Add Resource, and then select Add Existing File.

  6. In the Add existing file to resources dialog box, browse to any image file and select it, and then click Open.

  7. In Solution Explorer, select the Form1 node, and then on the View menu, select Code to open the Code Editor.

  8. In the Code Editor, select Paint from the Events drop-down list.

  9. In the Form1_Paint event handler, add the following code.

    
    e.Graphics.RotateTransform(45)
    e.Graphics.DrawImage(My.Resources.picture, 50, 0)
    
    
    
    NoteNote

    In My.Resources.picture, replace picture with the name of the image file that you added to the project resources in an earlier step.

  10. Press F5 to run the program. You should see the rotated image on the form.

In this lesson, you learned how to display a rotated image on a form. That completes the lessons on graphics. In the next set of lessons, you will learn how to share the programs you create.

Next Lesson: Distributing a Program

Show:
© 2014 Microsoft