C# | Método Graphics.DrawLine() | Serie 1

El método Graphics.DrawLine() se usa para dibujar una línea que conecta los dos puntos especificados por los pares de coordenadas. Hay 4 métodos en la lista de sobrecarga de este método de la siguiente manera:

  • Método DrawLine(Pluma, PuntoF, PuntoF)
  • DibujarLínea(Pluma, Int32, Int32, Int32, Int32) Método
  • DrawLine(Lápiz, Único, Único, Único, Único) Método
  • Método DrawLine(Pluma, Punto, Punto)

Aquí, discutiremos los dos primeros métodos.

Método DrawLine(Pluma, PuntoF, PuntoF)

Este método se utiliza para dibujar líneas desde un conjunto específico de puntos hasta un conjunto específico de puntos. Necesita una variable PointF que es un conjunto de puntos (x, y).

Sintaxis:

public void DrawLine (System.Drawing.Pen pen, System.Drawing.PointF pt1, System.Drawing.PointF pt2);

Parámetros:

  • bolígrafo: el bolígrafo determina el color, el ancho y el estilo de la línea.
  • pt1: Define las coordenadas (x, y) como una variable PointF del punto inicial.
  • pt2: Define las coordenadas (x, y) como una variable PointF del punto final.

Excepción: este método dará ArgumentNullexception si la pluma es nula.

Ejemplo:

// C# program to demonstrate the 
// DrawLine(Pen, PointF, PointF) Method
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
  
namespace GFG {
  
class PrintableForm : Form {
  
    public static void Main()
    {
        Application.Run(new PrintableForm());
    }
  
    public PrintableForm()
    {
        ResizeRedraw = true;
    }
    protected override void OnPaint(PaintEventArgs pea)
    {
        // Defines pen
        Pen pen = new Pen(ForeColor);
  
        // Defines the both points to connect
        // pt1 is (30.0, 30.0) which represents (x1, y1)
        PointF pt1 = new PointF(30.0F, 30.0F);
  
        // pt2 is (200.0, 300.0) which represents (x2, y2)
        PointF pt2 = new PointF(200.0F, 300.0F);
  
        // Draws the line
        pea.Graphics.DrawLine(pen, pt1, pt2);
    }
}
}

Producción:

DibujarLínea(Pluma, Int32, Int32, Int32, Int32) Método

Este método se usa para trazar líneas a partir de un conjunto específico de coordenadas dadas en forma x1, y1, x2, y2, todas discretas.

Sintaxis:

public void DrawLine (System.Drawing.Pen pen, int x1, int y1, int x2, int y2);

Parámetros:

  • pen : Pen determina el color, el ancho y el estilo de la línea.
  • x1 : La abscisa del primer punto.
  • y1 : La ordenada del primer punto.
  • x2 : La abscisa del segundo punto.
  • y2 : La ordenada del segundo punto.

Excepción: este método dará ArgumentNullexception si la pluma es nula.

Ejemplo 1:

// C# program to draw a cross using the
// DrawLine(Pen, Int32, Int32, Int32,
// Int32) Method
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
  
namespace GFG {
  
class PrintableForm : Form {
  
    public static void Main()
    {
        Application.Run(new PrintableForm());
    }
  
    public PrintableForm()
    {
        ResizeRedraw = true;
    }
  
    protected override void OnPaint(PaintEventArgs pea)
    {
        // Defines the pen
        Pen pen = new Pen(ForeColor);
  
        // To draw a cross we need to make 2 
        // diagonals from top-left to the 
        // bottom-right and top-right to the 
        // bottom-left to calculate these 
        // coordinates we would take help of 
        // our screen size top-left = (0, 0)
        // bottom-right = (ClientSize.Width - 1,
        // ClientSize.Height - 1)
        // top-right = (ClientSize.Width - 1, 0)
        // bottom-left = (0, ClientSize.Height - 1)
        pea.Graphics.DrawLine(pen, 0, 0, ClientSize.Width - 1,
                                       ClientSize.Height - 1);
  
        pea.Graphics.DrawLine(pen, ClientSize.Width - 1, 0, 0,
                                       ClientSize.Height - 1);
    }
}
}

Producción:

Ejemplo 2:

// C# program to Create a 8x8 square board
// using // DrawLine(Pen, Int32, Int32, Int32,
// Int32) Method
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
  
namespace GFG {
  
class PrintableForm : Form {
  
    public static void Main()
    {
        Application.Run(new PrintableForm());
    }
  
    public PrintableForm()
    {
        ResizeRedraw = true;
    }
  
    protected override void OnPaint(PaintEventArgs pea)
    {
        // Define Pen
        Pen pen = new Pen(ForeColor);
  
        // loop for all 7 horizontal lines to draw
        for (int i = 0; i < 7; i++) 
        {
            // Taking Block size to be 30x30
            // So width and height will be 30*8=240
            pea.Graphics.DrawLine(pen, i * 30, 0, i * 30, 240);
        }
  
        // loop for all 7 horizontal lines to draw
        for (int i = 0; i < 7; i++) 
        {
            pea.Graphics.DrawLine(pen, 0, i * 30,
                                    240, i * 30);
        }
    }
}
}

Producción:

Publicación traducida automáticamente

Artículo escrito por ShivamChauhan5 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *