Compartir a través de


Cómo: Dibujar una forma con contorno

En este ejemplo se dibujan contornos de elipses y rectángulos en un formulario.

Ejemplo

    Private Sub DrawEllipse()
        Dim myPen As New System.Drawing.Pen(System.Drawing.Color.Red)
        Dim formGraphics As System.Drawing.Graphics
        formGraphics = Me.CreateGraphics()
        formGraphics.DrawEllipse(myPen, New Rectangle(0, 0, 200, 300))
        myPen.Dispose()
        formGraphics.Dispose()
    End Sub

    Private Sub DrawRectangle()
        Dim myPen As New System.Drawing.Pen(System.Drawing.Color.Red)
        Dim formGraphics As System.Drawing.Graphics
        formGraphics = Me.CreateGraphics()
        formGraphics.DrawRectangle(myPen, New Rectangle(0, 0, 200, 300))
        myPen.Dispose()
        formGraphics.Dispose()
    End Sub

    private void DrawEllipse()
    {
        System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Red);
        System.Drawing.Graphics formGraphics;
        formGraphics = this.CreateGraphics();
        formGraphics.DrawEllipse(myPen, new Rectangle(0, 0, 200, 300));
        myPen.Dispose();
        formGraphics.Dispose();
    }

    private void DrawRectangle()
    {
        System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Red);
        System.Drawing.Graphics formGraphics;
        formGraphics = this.CreateGraphics();
        formGraphics.DrawRectangle(myPen, new Rectangle(0, 0, 200, 300));
        myPen.Dispose();
        formGraphics.Dispose();
    }

private:
    void DrawEllipse()
    {
        System::Drawing::Pen^ myPen =
            gcnew System::Drawing::Pen(System::Drawing::Color::Red);
        System::Drawing::Graphics^ formGraphics;
        formGraphics = this->CreateGraphics();
        formGraphics->DrawEllipse(myPen, Rectangle(0, 0, 200, 300));
        delete myPen;
        delete formGraphics;
    }

private:
    void DrawRectangle()
    {
        System::Drawing::Pen^ myPen =
            gcnew System::Drawing::Pen(System::Drawing::Color::Red);
        System::Drawing::Graphics^ formGraphics;
        formGraphics = this->CreateGraphics();
        formGraphics->DrawRectangle(myPen, Rectangle(0, 0, 200, 300));
        delete myPen;
        delete formGraphics;
    }

Compilar el código

No es posible llamar a este método en el controlador de eventos Load. El contenido representado no se representa de nuevo si el formulario cambia de tamaño o se ve obscurecido por otro formulario. Para que el contenido se redibuje automáticamente, es recomendable reemplazar el método OnPaint.

Programación eficaz

Siempre debe llamar a Dispose en los objetos que consuman recursos del sistema, como los objetos Pen y Graphics.

Vea también

Referencia

DrawEllipse

OnPaint

DrawRectangle

Otros recursos

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

Utilizar lápiz para dibujar líneas y formas

Gráficos y dibujos en Windows Forms