Cómo: Representar imágenes con GDI+

La interfaz GDI+ puede usarse para representar imágenes que existan como archivos en las aplicaciones. Para ello hay que crear un nuevo objeto de una clase Image (como Bitmap), crear un objeto Graphics que haga referencia a la superficie de dibujo que se desea utilizar y llamar al método DrawImage del objeto Graphics. La imagen se pintará sobre la superficie de dibujo representada por la clase Graphics. Los archivos de imagen se pueden crear y modificar mediante el Editor de imágenes en tiempo de diseño para representarlos posteriormente con la interfaz GDI+ en tiempo de ejecución. Para obtener más información, vea Editor de imágenes.

Para representar una imagen con GDI+

  1. Cree un objeto que represente a la imagen que desea mostrar. Este objeto debe ser miembro de una clase que herede de Image, como Bitmap o Metafile. A continuación se muestra un ejemplo:

    ' Uses the System.Environment.GetFolderPath to get the path to the 
    ' current user's MyPictures folder.
    Dim myBitmap as New Bitmap _
       (System.Environment.GetFolderPath _
          (System.Environment.SpecialFolder.MyPictures))
    
    // Uses the System.Environment.GetFolderPath to get the path to the 
    // current user's MyPictures folder.
    Bitmap myBitmap = new Bitmap
       (System.Environment.GetFolderPath
          (System.Environment.SpecialFolder.MyPictures));
    
    // Uses the System.Environment.GetFolderPath to get the path to the 
    // current user's MyPictures folder.
    Bitmap^ myBitmap = gcnew Bitmap
       (System::Environment::GetFolderPath
          (System::Environment::SpecialFolder::MyPictures));
    
  2. Cree un objeto Graphics que represente la superficie de dibujo que desea usar. Para obtener más información, vea Cómo: Crear objetos Graphics para dibujar.

    ' Creates a Graphics object that represents the drawing surface of 
    ' Button1.
    Dim g as Graphics = Button1.CreateGraphics
    
    // Creates a Graphics object that represents the drawing surface of 
    // Button1.
    Graphics g = Button1.CreateGraphics();
    
    // Creates a Graphics object that represents the drawing surface of 
    // Button1.
    Graphics^ g = button1->CreateGraphics();
    
  3. Llame al método DrawImage del objeto Graphics para representar la imagen. Deberá especificar la imagen que desea que se dibuje y las coordenadas en las que se debe dibujar.

    g.DrawImage(myBitmap, 1, 1)
    
    g.DrawImage(myBitmap, 1, 1);
    
    g->DrawImage(myBitmap, 1, 1);
    

Vea también

Tareas

Cómo: Crear objetos Graphics para dibujar

Cómo: Dibujar texto en un formulario Windows Forms

Dibujar líneas o figuras cerradas

Referencia

Editor de imágenes

Conceptos

Lápices, líneas y rectángulos en GDI+

Otros recursos

Introducción a la programación de gráficos

Gráficos y dibujos en Windows Forms