Blog
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    API(s)
    Sencha-Touch
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog
Report Abuse Form
Reason:    
 

Home >> C# >> Gradient background in c#.
Gradient background in c#.

In this blog we learn how to create gradient backgorund in c#.net 2.0 by using GDI programing.
Views: 4272     Comments: 0
by Awadhendra Tiwari on 5/10/2011

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, this.ClientSize.Height);

               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.

e.Graphics.FillRectangle(brush, rc);

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))

                {

                    e.Graphics.FillRectangle(brush, rc);

                }           

        }

Title :  
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
       
 
Report Abuse Form
Reason:    
 
Latest ArticleRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Latest Blogs by Awadhendra TiwariRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 2644
  
Copyright © 2009 - 2013MindStick. All Rights Reserved.