.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Windows Form Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Different LinearGradiantBrushes in C#

Posted By:Abhisek Panda       Posted Date: November 25, 2009    Points: 10    Category: Windows Form Controls    URL: http://www.dotnetspark.com  
 

Different LinearGradiantBrushes in C#:-

         We can use LinearGradiantBrushe type whenever we want to blend two colors together in a gradiant pattern. When we build a LinearGradiantBrush we need to specify a pair of Color types and the direction of the blend of colors via the LinearGradiantMode enumeration defined as follows:-

public enum LinearGradiantMode
{
Horizontal, Vertical, ForwardDiagonal, BackwardDiagonal
}
To test these values just create a widow form application in C# and add the following code in the Paint event handler of the form.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace GradientBrushes
{
public partial class MainForm : Form
{
public MainForm()
{
CenterToScreen();
InitializeComponent();
}
private void MainForm_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Rectangle r = new Rectangle(10, 10, 100, 100);
// A gradient brush.
LinearGradientBrush lgBrush = null;
int yOffSet = 10;
// Get all members of the LinearGradientMode enum.
Array obj = Enum.GetValues(typeof(LinearGradientMode));
// Draw an oval with a LinearGradientMode member.
for (int x = 0; x < obj.Length; x++)
{
// Configure Brush.
LinearGradientMode lgMode = (LinearGradientMode)obj.GetValue(x);
lgBrush = new LinearGradientBrush(r, Color.GreenYellow, Color.Blue,lgMode );
// Print name of LinearGradientMode enum.
g.DrawString(lgMode.ToString(), new Font("Times New Roman", 10),
new SolidBrush(Color.Black), 0, yOffSet);
// Fill a rectangle with the correct brush.
g.FillRectangle(lgBrush, 150, yOffSet, 200, 50);
yOffSet += 80;
}
}
}
}
When you run the application it will show you the different brushes with there names.

     

Further Readings:

    Responses

    No response found. Be the first to respond this post

    Post Comment

    You must Sign In To post reply
    Find More code samples in C#, ASP.Net, Vb.Net and more Here

    Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend