c#畫直線
本文實例講述了C#使用GDI繪制矩形的方法。分享給大家供大家參考。具體實現方法如下:
| 1 2 3 |
|
代碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp11
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//創建Graphics對象
Graphics GPS = this.CreateGraphics();
//創建黑色pen對象
Pen MyPen = new Pen(Color.Black, 2f);
//確定起點和終點
Point pt1 = new Point(70, 20);
Point pt2 = new Point(200, 320);
//使用DrawLine方法繪制直線
GPS.DrawLine(MyPen, pt1, pt2);
}
private void button2_Click(object sender, EventArgs e)
{
Graphics GPS = this.CreateGraphics();
Pen MyPen = new Pen(Color.Red, 2f);
GPS.DrawLine(MyPen, 50,20,300,200);
}
}
}
運行結果

