In computer graphics, a color gradient (sometimes called a color ramp or color progression) specifies a range of position-dependent colors, usually used to fill a region. For example, many window managers allow the screen background to be specified as a gradient. The colors produced by a gradient vary continuously with position, producing smooth color transitions.
There are few steps to generate gradient color for form in c#
Step 1- Take a form and drag – drop textbox and button

Step 2 – chose first color via button ‘color 1’

Step 3 – chose second color via button ‘color 2’

Step 4- selected color fill into textbox

Step 5 – Then click on Apply button for generate gradient color for Form1

Step 6 – you can select custom color also


Code:
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System;
using System.Collections.Generic;
using System.Linq;
namespace GradientColor
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
ColorDialog cd = new ColorDialog();
System.Drawing.Color strcolor1;
System.Drawing.Color strcolor2;
public void draw(object sender, PaintEventArgs e)
{
try
{
using (LinearGradientBrush brush = new LinearGradientBrush(ClientRectangle, strcolor1, strcolor2, LinearGradientMode.Vertical))
{
e.Graphics.FillRectangle(brush, ClientRectangle);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void Form1_Resize(object sender, System.EventArgs e)
{
Invalidate();
}
private void button1_Click(object sender, EventArgs e)
{
cd.ShowDialog();
txtColor1.BackColor = cd.Color;
strcolor1 = cd.Color;
}
private void button2_Click(object sender, EventArgs e)
{
cd.ShowDialog();
txtcolor2.BackColor = cd.Color;
strcolor2 = cd.Color;
}
private void btnApply_Click(object sender, EventArgs e)
{
Paint += draw;
Invalidate();
}
}
}
Leave Comment
1 Comments