Share via


HOW TO:建立線形漸層

更新:2007 年 11 月

GDI+ 提供了水平、垂直和對角線形漸層。依照預設,線形漸層中的色彩會一致變更,但是,您也可以自訂線形漸層,使色彩以不一致的方式變更。

下列範例會使用水平線形漸層筆刷來填滿直線、橢圓形和矩形。

LinearGradientBrush 建構函式會接收四個引數:兩個點和兩個色彩。第一個點 (0, 10) 和第一個色彩 (紅色) 關聯,第二個點 (200, 10) 和第二個色彩 (藍色) 關聯。如同預期,從 (0, 10) 到 (200, 10) 繪製的直線會從紅色逐漸變成藍色。

在 (50, 10) 和 (200, 10) 這兩點中的 10 並不重要。重點在於兩個點都有相同的第二座標,連接它們的直線是水平的。當水平座標從 0 移到 200 時,橢圓形和矩形也會從紅色逐漸變成藍色。

下圖顯示的是直線、橢圓形和矩形。請注意,當水平座標增加到 200 以上時,色彩漸層又會重複。

線性漸層

若要使用水平線形漸層

  • 請傳入不透明紅色和不透明藍色,分別做為第三和第四個引數。

    Dim linGrBrush As New LinearGradientBrush( _
       New Point(0, 10), _
       New Point(200, 10), _
       Color.FromArgb(255, 255, 0, 0), _
       Color.FromArgb(255, 0, 0, 255))
    Dim pen As New Pen(linGrBrush)
    
    e.Graphics.DrawLine(pen, 0, 10, 200, 10)
    e.Graphics.FillEllipse(linGrBrush, 0, 30, 200, 100)
    e.Graphics.FillRectangle(linGrBrush, 0, 155, 500, 30)
    
    
    LinearGradientBrush linGrBrush = new LinearGradientBrush(
       new Point(0, 10),
       new Point(200, 10),
       Color.FromArgb(255, 255, 0, 0),   // Opaque red
       Color.FromArgb(255, 0, 0, 255));  // Opaque blue
    
    Pen pen = new Pen(linGrBrush);
    
    e.Graphics.DrawLine(pen, 0, 10, 200, 10);
    e.Graphics.FillEllipse(linGrBrush, 0, 30, 200, 100);
    e.Graphics.FillRectangle(linGrBrush, 0, 155, 500, 30);
    

在上述範例中,當您從水平座標 0 移動到水平座標 200 時,色彩元素會呈線形變更。例如,第一個座標介於 0 和 200 之間的點,它的藍色元素將會介於 0 和 255 之間。

GDI+ 可讓您調整色彩從漸層一端到另一端的改變方式。假設您要根據下表,建立從黑色變成紅色的漸層筆刷。

水平座標

RGB 元素

0

(0, 0, 0)

40

(128, 0, 0)

200

(255, 0, 0)

請注意,當水平座標為 0 到 200 之間的 20% 時,紅色元素的濃度只有一半。

下列範例會設定 LinearGradientBrush 物件的 Blend 屬性,建立三個相關濃度與三個相關位置之間的關聯。如同上述表格,相對濃度 0.5 和相關位置 0.2 關聯。程式碼會使用漸層筆刷來填滿橢圓形和矩形。

下圖顯示的是產生的橢圓形和矩形。

線性漸層

若要自訂線形漸層

  • 請傳入不透明黑色和不透明紅色,分別做為第三和第四個引數。

    Dim linGrBrush As New LinearGradientBrush( _
       New Point(0, 10), _
       New Point(200, 10), _
       Color.FromArgb(255, 0, 0, 0), _
       Color.FromArgb(255, 255, 0, 0))
    
    Dim relativeIntensities As Single() = {0.0F, 0.5F, 1.0F}
    Dim relativePositions As Single() = {0.0F, 0.2F, 1.0F}
    
    'Create a Blend object and assign it to linGrBrush.
    Dim blend As New Blend()
    blend.Factors = relativeIntensities
    blend.Positions = relativePositions
    linGrBrush.Blend = blend
    
    e.Graphics.FillEllipse(linGrBrush, 0, 30, 200, 100)
    e.Graphics.FillRectangle(linGrBrush, 0, 155, 500, 30)
    
    
    LinearGradientBrush linGrBrush = new LinearGradientBrush(
       new Point(0, 10),
       new Point(200, 10),
       Color.FromArgb(255, 0, 0, 0),     // Opaque black 
       Color.FromArgb(255, 255, 0, 0));  // Opaque red
    
    float[] relativeIntensities = { 0.0f, 0.5f, 1.0f };
    float[] relativePositions = { 0.0f, 0.2f, 1.0f };
    
    //Create a Blend object and assign it to linGrBrush.
    Blend blend = new Blend();
    blend.Factors = relativeIntensities;
    blend.Positions = relativePositions;
    linGrBrush.Blend = blend;
    
    e.Graphics.FillEllipse(linGrBrush, 0, 30, 200, 100);
    e.Graphics.FillRectangle(linGrBrush, 0, 155, 500, 30);
    

上述範例中的漸層都是水平漸層;也就是說,當您延著任何水平直線移動時,色彩便會逐漸變更。您也可以定義垂直漸層和對角漸層。

下列範例會將 (0, 0) 和 (200, 100) 兩點傳遞至 LinearGradientBrush 建構函式。藍色和 (0, 0) 關聯,綠色和 (200, 100) 關聯。直線 (畫筆寬度為 10) 和矩形會使用線形漸層筆刷填滿。

下圖顯示的是直線和橢圓形。請注意,當您延著與通過 (0, 0) 和 (200, 100) 兩點的直線成平形方向的任何直線移動時,橢圓形中的色彩便會逐漸變更。

線性漸層

若要建立對角線形漸層

  • 請傳入不透明藍色和不透明綠色,分別做為第三和第四個引數。

    Dim linGrBrush As New LinearGradientBrush( _
       New Point(0, 0), _
       New Point(200, 100), _
       Color.FromArgb(255, 0, 0, 255), _
       Color.FromArgb(255, 0, 255, 0))
    ' opaque blue
    ' opaque green
    Dim pen As New Pen(linGrBrush, 10)
    
    e.Graphics.DrawLine(pen, 0, 0, 600, 300)
    e.Graphics.FillEllipse(linGrBrush, 10, 100, 200, 100)
    
    
    LinearGradientBrush linGrBrush = new LinearGradientBrush(
       new Point(0, 0),
       new Point(200, 100),
       Color.FromArgb(255, 0, 0, 255),   // opaque blue
       Color.FromArgb(255, 0, 255, 0));  // opaque green
    
    Pen pen = new Pen(linGrBrush, 10);
    
    e.Graphics.DrawLine(pen, 0, 0, 600, 300);
    e.Graphics.FillEllipse(linGrBrush, 10, 100, 200, 100);
    

請參閱

其他資源

使用漸層筆刷填滿形狀

Windows Form 中的圖形和繪圖