Home [C# 筆記]使用 GDI+ 繪製直線 1
Post
Cancel

[C# 筆記]使用 GDI+ 繪製直線 1

什麼是 GDI+

簡單一句話,就是畫圖的

使用 GDI+ 繪製

想想,一般我們畫圖需要什麼?
一根筆、顏色、一張紙、兩點、給繪製直線的對象
Graphics Pen Brushes.Red Point DrawLine

this當前視窗的物件

畫直線 DrawLine

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//準備工作:一根筆 顏色 一張紙 兩點 繪製直線的物件

private void button1_Click(object sender, EventArgs e)
{
    //創建GDI物件
    Graphics g = this.CreateGraphics();//new Graphics(); 不能new
    //創建畫筆物件
    Pen pen = new Pen(Brushes.Red);
    //創建兩個點
    Point p1 = new Point(30, 50);
    Point p2 = new Point(250,250);
    //畫直線
    g.DrawLine(pen, p1, p2);
}

//win10不會消失
//當視窗發生移動時,重新繪製。(因為視窗移動,線就不見了,所以要再重畫)
int i = 0;
private void Form1_Paint(object sender, PaintEventArgs e)
{
    i++;
    label1.Text = i.ToString();

    //創建GDI物件
    Graphics g = this.CreateGraphics();//new Graphics(); 不能new
    //創建畫筆物件
    Pen pen = new Pen(Brushes.Red);
    //創建兩個點
    Point p1 = new Point(30, 50);
    Point p2 = new Point(250, 250);
    //畫直線
    g.DrawLine(pen, p1, p2);
}
This post is licensed under CC BY 4.0 by the author.