Alec Tang

Professional Web Developer/ Web Designer

I build websites based on the latest web standards providing the best possible solution to your company

  • Home
  • About
  • Portfolio
  • Contact
  • Blog
Subscribe Feed

How to create Gradient effect background using C#

Posted by Alec on Mon, 10 Aug 2009, in ASP.NET C#   

Ever wanted to create a gradient image dynamically? This can easily be done using C# with the following snipplet of code. We can also specify the width, height, gradient direction (horizontal or vertical), and the two colors.

int intWidth = Convert.ToInt16(txbWidth.Text);
int intHeight = Convert.ToInt16(txbHeight.Text);
banner.GradientDirection = Convert.ToInt16(rcbDirection.SelectedValue);
LinearGradientMode gradientMode;
if (banner.GradientDirection == 1)
       gradientMode = LinearGradientMode.Vertical;
else
       gradientMode = LinearGradientMode.Horizontal;

using (Bitmap bitmap = new Bitmap(intWidth, intHeight))
    using (Graphics graphics = Graphics.FromImage(bitmap))
        using (LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, intWidth, intHeight),
               FirstColor, SecondColor, gradientMode))
                {
                   //brush.SetSigmaBellShape(intWidth / 100, intHeight / 100);
                    graphics.FillRectangle(brush, new Rectangle(0, 0, intWidth, intHeight));
                    MemoryStream stream = new MemoryStream();
                    bitmap.Save(stream, ImageFormat.Jpeg);
                    Byte[] bytes = stream.ToArray();
                 }

More info at: http://weblogs.asp.net/whaggard/archive/2004/10/27/248878.aspx
 

Comments Be the first to write a comment. Comment gets approved before publishing.

Post Your Comment

 
 
   

Search

 

Latest Posts

  • List Attachments Open as Read Only
  • Website finally back up from Google's block
  • Sys.WebForms.PageRequestManagerParserErrorException
  • How Google treats Content Duplication
  • How to create HTML column in Sharepoint List View
  • How Sharepoint stores User Data
  • How to send email via Sharepoint
  • Malaysia Airlines launched iPhone Application: MHMobile
  • Intranet, the next big market
  • How to retrieve and update from a multi choice Checkboxlist ...

Categories

  • Browsers (1)
  • Projects (1)
  • Web Design (7)
  • Sharepoint (21)
  • Telerik (5)
  • Wordpress (1)
  • Internet (2)
  • SQL (5)
  • LINQ (3)
  • ASP.NET C# (34)
  • JavaScripts (3)
  • IIS (0)
  • Industry (1)
  • Tools (8)
  • SEO (5)

Archives

  • September 2010 (1)
  • August 2010 (1)
  • May 2010 (1)
  • April 2010 (3)
  • March 2010 (5)
  • February 2010 (4)
  • January 2010 (11)
  • November 2009 (3)
  • October 2009 (1)
  • September 2009 (9)
  • August 2009 (3)
  • July 2009 (4)
  • June 2009 (1)
  • May 2009 (2)
  • April 2009 (8)
  • March 2009 (6)
  • February 2009 (2)
© Copyright 2009 Alec Tang. All Rights Reserved.
This site is conform to W3C Standard XHTML & CSS