2009-09-29 94 views
5

如何創建一個梯度圖像(具有給定的高度&寬度,開始的顏色和終了色)使用C#?有人有簡單的樣本片段嗎?謝謝!C#創建梯度圖像

回答

10

您可以使用LinearGradientBrush做到這一點。例如

// using System.Drawing; 
// using System.Drawing.Imaging; 
// using System.Drawing.Drawing2D; 

public static void OutputGradientImage() 
{ 
    using (Bitmap bitmap = new Bitmap(100, 100)) // 100x100 pixels 
    using (Graphics graphics = Graphics.FromImage(bitmap)) 
    using (LinearGradientBrush brush = new LinearGradientBrush(
     new Rectangle(0, 0, 100, 100), 
     Color.Blue, 
     Color.Red, 
     LinearGradientMode.Vertical)) 
    { 
     brush.SetSigmaBellShape(0.5f); 
     graphics.FillRectangle(brush, new Rectangle(0, 0, 100, 100)); 
     bitmap.Save("gradientImage.jpg", ImageFormat.Jpeg); 
    } 
} 
2

一個LinearGradientBrush是你的朋友在這裏:

 

    Bitmap bmp = new Bitmap(Width, Height); 
    Graphics g = Graphics.FromImage(bmp); 
    LinearGradientBrush lgb = new LinearGradientBrush(new Point(0, 0), new Point(Width, Height), Color.Black, Color.Red); 
    g.FillRectangle(lgb, 0, 0, Width, Height); 
    bmp.Save("FileName"); 
    lgb.Dispose(); 
    g.Dispose(); 
    bmp.Dispose(); 
 
0
protected override void OnPaintBackground(PaintEventArgs e) 
{ 
    using (Brush b = new LinearGradientBrush(ClientRectangle, Color.Red, Color.Blue, LinearGradientMode.ForwardDiagonal)) 
     e.Graphics.FillRectangle(b, ClientRectangle); 
} 

這是我們所簡單,你可以把它。