Click here to Skip to main content
13,006,360 members (79,289 online)
Click here to Skip to main content
Add your own
alternative version


6 bookmarked
Posted 14 Apr 2013

Gradient Color C#, ASP.NET

Rate this:
Please Sign up or sign in to vote.
This a trick to generate random gradient color in your ASP.NET project, which will change randomly on every post-back.


This is a trick to generate a random gradient color in your ASP.NET project, which will change randomly on every post-back.


Required references are given below:

using System.Drawing.Drawing2D;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;

Using the code  

Following is my code for the ProcessRequest method handler: 

public void ProcessRequest (HttpContext context) {

    string[] fColor = new string[] {"Gray","Skyblue","Green","Yellow" };
    string[] sColor = new string[] {"white","Red","Pink","blue" };
    Random r = new Random();
    string firstColor = fColor[r.Next(fColor.Length -1)];
    string secondColor = sColor[r.Next(sColor.Length - 1)];
    Int32 height = 786;
    Bitmap img = new Bitmap(2, height);
    Graphics g = Graphics.FromImage(img);
    LinearGradientBrush brush = new LinearGradientBrush(
        new Point(0, height), 
    g.DrawRectangle(new System.Drawing.Pen(brush, 10), new Rectangle(0, 0, 2, height));
    context.Response.ContentType = ".jpg";
    MemoryStream str = new MemoryStream();
    img.Save(str, ImageFormat.Jpeg);
private System.Drawing.Color getColorFromString(string SColor)
    return System.Drawing.Color.FromName(SColor);

Points of Interest

Provide every postback a new background whenever user browses pages of the website.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170628.1 | Last Updated 14 Apr 2013
Article Copyright 2013 by Praveen Kumar Chauhan (PRK)
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid