ایجاد طیف نوری (Gradient) برای فرم

برای ایجاد طیف نوری برای فرم ابتدا یک برنامه ویندوزی ایجاد کرده و سپس با زدن دکمه F7 به محیط کدنویسی رفته و کدهای زیر را در جایگزین کدهای آن کنید:

using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public void PaintGradient(Control control1, LinearGradientMode _direction, Color _gradientColorStart, Color _gradientColorEnd)
        {

            LinearGradientBrush gradBrush;

            gradBrush = new LinearGradientBrush(new Rectangle(0, 0, control1.Width, control1.Height), _gradientColorStart, _gradientColorEnd, _direction);

            Bitmap bmp = new Bitmap(control1.Width, control1.Height);

            Graphics g = Graphics.FromImage(bmp);

            g.FillRectangle(gradBrush, new Rectangle(0, 0, control1.Width, control1.Height));

            control1.BackgroundImage = bmp;

            control1.BackgroundImageLayout = ImageLayout.Stretch;

        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            PaintGradient(this, LinearGradientMode.Vertical, Color.RoyalBlue, Color.DarkBlue);
        }
    }
}

حال برنامه را اجرا و نتیجه را مشاهده نمایید :
create-a-gradient-for-form-csharp