当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C# Graphics.DrawLine()方法用法及代码示例


Graphics.DrawLine()方法用于绘制连接由坐标对指定的两个点的线。此方法的重载列表中有4种方法,如下所示:

  • DrawLine(Pen, PointF, PointF) Method
  • DrawLine(Pen, Int32, Int32, Int32, Int32) Method
  • DrawLine(Pen, Single, Single, Single, Single) Method
  • DrawLine(Pen, Point, Point) Method

在这里,我们将讨论前两种方法。



DrawLine(Pen, PointF, PointF) Method

此方法用于绘制从一组指定的点到一组指定的点的线形。它需要一个PointF变量,该变量由(x,y)点组成。

用法:

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

参数:

  • pen:笔确定线条的颜色,宽度和样式。
  • pt1:将(x,y)坐标定义为初始点的PointF变量。
  • pt2:将(x,y)坐标定义为最终点的PointF变量。

异常:如果pen为null,则此方法将提供ArgumentNullexception。

例:

// 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); 
    } 
} 
}

输出:

DrawLine(Pen, Int32, Int32, Int32, Int32) Method

此方法用于绘制线形式的一组指定坐标,这些坐标以离散形式x1,y1,x2,y2给出。

用法:



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

参数:

  • pen:Pen确定线条的颜色,宽度和样式。
  • x1:第一点的横坐标。
  • y1:第一点的纵坐标。
  • x2:第二点的横坐标。
  • y2:第二点的纵坐标。

异常:如果pen为null,则此方法将提供ArgumentNullexception。

范例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); 
    } 
} 
}

输出:

范例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); 
        } 
    } 
} 
}

输出:




相关用法


注:本文由纯净天空筛选整理自ShivamChauhan5大神的英文原创作品 C# | Graphics.DrawLine() Method | Set – 1。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。