Share via


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.

Displaying an 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.

Try It!

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 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)
    

    Note

    Replace picture with the name of the resource that you added in an earlier step.

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

Next Steps

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

See Also

Tasks

Drawing Text on a Form

Other Resources

Drawing Pictures: Using Graphics

Visual Basic Guided Tour