In this blog I will describe you that how to create a gradient background color in c#. For creating gradient background color we used System.Drawing.Drawing2D; namespace. Now we are going to write down some steps which is helpful for you while implementing gradient background color for your application.
Steps to implement gradient background
1) Add a namespace in your project which is System.Drawing.Drawing2D. This namespace contains all necessary classes and interfaces and enumeration which is required to implement gradient background.
2) Now identify that control for which you want to create background color. It might be label control, might be panel control or itself main container control which is form. One thing important while selecting control for background gradient is that, that control must have Paint event because we needs to override it or right down code on this event.
3) After identifying control go to the property window of that control followed by event then click on Paint event. This will generate a Paint handler for your control where you write code for gradient background. That handler looks like something this.
private void ImageTool_Paint(object sender, PaintEventArgs e)
4) Then create an object of Rectangle class which is as it.
Rectangle rc = new Rectangle(0, 0, this.ClientSize.Width,
Where first two parameters of rectangle constructor represents starting co-ordinate of control which is 0, 0 that is beginning and last two parameters represent last co-ordinate of control.
5) Now create an object of LinearGradientBrush class which is as follows.
LinearGradientBrush brush = new LinearGradientBrush(rc, Color.WhiteSmoke, Color.Tan, LinearGradientMode.Vertical)
Where first parameter represents region of control which needs to be make gradient second and third parameter represents color combination and last parameter represent gradient mode that should be Verticle, Horizontal, BackwordDiagonal and ForwordDiagonal.
6) Finally fill control using FillRectangle() method of graphics class which is as it is.
An example which represents gradient background for panel
private void pnl_Paint(object sender, PaintEventArgs e)
Rectangle rc = new Rectangle(0, 0, this.ClientSize.Width, this.ClientSize.Height);
using (LinearGradientBrush brush = new LinearGradientBrush(rc, Color.WhiteSmoke, Color.Tan, LinearGradientMode.Vertical))