簡體   English   中英

.NET Brush with hard color transition(類似於漸變)

[英].NET Brush with hard color transition (similar to gradient)

我需要一個刷,這將使硬盤的過渡,從一個顏色到下一個類似如下所示。 我提供了另一個控件的 Fill Brush,所以我需要一個 Brush 對象。

我想要的是:

http://mechgt.com/dump/sample.png

我已經嘗試了幾種LinearGradientBrush方法(見下面的一個例子),但它不會產生硬線; 過渡仍然是漸變,線條模糊。

LinearGradientBrush brush = new LinearGradientBrush(pictureBox1.ClientRectangle, Color.Green, Color.Silver, 0f);
Blend blend = new Blend();
blend.Factors = new float[] { 0, 0, 1, 1 };
blend.Positions = new float[] { 0, .7f, .7f, 1 };
brush.Blend = blend;

我得到的:

http://mechgt.com/dump/sample2.png

我如何定義一個可以做到這一點的畫筆?

為此,您可能需要使用TexureBrush

TextureBrush將圖像作為參數,這意味着您可以從光盤加載現有圖像,或在內存中創建一個圖像,您可以在其中繪制矩形等。

在以下示例中,GetTextureBrush 將創建實際畫筆。 您可以在此處定義顏色和形狀。 我剛剛在您需要的車道上創建了一些東西,根據需要進行調整。

例如(VB.net):

Private Sub DrawBrush()

    Dim g As Graphics = Me.CreateGraphics
    Dim bmp As Image = GetTexureBrush
    Dim b As New TextureBrush(bmp)

    g.FillRectangle(b, New Rectangle(0, 0, 200, 500))

    b.Dispose()
    bmp.Dispose()
    g.Dispose()

End Sub
Private Function GetTexureBrush() As Image

    Dim bmp As New Bitmap(100, 20)
    Dim g As Graphics = Graphics.FromImage(bmp)

    g.FillRectangle(Brushes.DarkGreen, New Rectangle(0, 0, 75, 20))
    g.FillRectangle(Brushes.Gray, New Rectangle(75, 0, 25, 20))

    g.Dispose()

    Return bmp

End Function

C#

private void DrawBrush()
{
    Graphics g = this.CreateGraphics;
    Image bmp = GetTexureBrush();
    TextureBrush b = new TextureBrush(bmp);

    g.FillRectangle(b, new Rectangle(0, 0, 200, 500));

    b.Dispose();
    bmp.Dispose();
    g.Dispose();

}
private Image GetTexureBrush()
{

    Bitmap bmp = new Bitmap(100, 20);
    Graphics g = Graphics.FromImage(bmp);

    g.FillRectangle(Brushes.DarkGreen, new Rectangle(0, 0, 75, 20));
    g.FillRectangle(Brushes.Gray, new Rectangle(75, 0, 25, 20));

    g.Dispose();

    return bmp;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM