Donate

WIndows Forms Linear Gradients In C#.NET 2D

Hi,
Here's a simple demo using Linear gradients (Color Manipulation) in C#.NET Windows Forms.
private int _rColor = 100;
     
public int GenerateRGB(int cc)
{
 int rt = 0;
 if (cc <= 255)
 {
  rt = Information.RGB(255, cc, 0);
 }
 else if (cc < 511)
 {
  rt = Information.RGB(255 - (cc - 255), 255, 0);
 }
 else if (cc < 766)
 {
  rt = Information.RGB(0, 255, cc - 511);
 }
 else if (cc < 1022)
 {
  rt = Information.RGB(0, 255 - (cc - 767), 255);
 }
 else if (cc < 1277)
 {
  rt = Information.RGB(0, 0, 255 - (cc - 1022));
 }
 else
 {
  rt = Information.RGB(0, 0, 0);
 }
 return rt;
}

private void timer1_Tick(object sender, EventArgs e)
{
 this.Refresh();
 _rColor += 1;
 if (_rColor > 1500)
 {
  _rColor = 0;
 }
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
 Color color = new Color();
 Random m_Rnd = new Random(); 
 color = Color.FromArgb(255, m_Rnd.Next(0, 255), m_Rnd.Next(0, 255), m_Rnd.Next(0, 255));

 using (LinearGradientBrush linGrBrush = new LinearGradientBrush(new Point(0, 10), new Point(200, 10), Color.FromArgb(GenerateRGB(_rColor)), color))
 {
  using (Pen mypen = new Pen(linGrBrush, 2))
  {
   e.Graphics.DrawLine(mypen, 0, 0, this.Width, this.Height);
  }
 }
}
Form Load:
Linear Gradients In C#.NET 2D
After One Second, the color changes:
Linear Gradients In C#.NET 2D

Comments

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

How To Insert Or Add Emojis In Microsoft Teams Status Message

Pass GUID As Parameter To Action Using ASP.NET MVC ContribGrid