Click here to Skip to main content
15,894,405 members
Articles / Web Development / HTML

Progess Bar Control for ASP.NET

Rate me:
Please Sign up or sign in to vote.
3.77/5 (7 votes)
15 Feb 20057 min read 117K   3.6K   73  
A reusable progress bar server control for ASP.NET server pages.
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.UI.Design;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Design;
using System.ComponentModel.Design;
using System.ComponentModel.Design.Serialization;


[assembly: TagPrefix("ProgressBarControl", "PBC")]
namespace ProgressBarControl
{
	/// <summary>
	/// This is the common Storm graphical progress bar.
	/// </summary>
	/// <remarks>
	/// This custom control produces a graphical progress bar.
	/// </remarks>
	[DefaultProperty("Type")] 
	[ToolboxData("<{0}:ProgressBar runat=server></{0}:ProgressBar>")]
	[ToolboxBitmapAttribute( typeof(ProgressBar) ) ]
	public class ProgressBar : System.Web.UI.WebControls.WebControl
	{
		#region Enumerations
		public enum BarType
		{
			Horizontal,
			Vertical
		}

		// Corresponds to System.Drawing.Imaging.ImageFormat
		public enum BarFormat
		{
			Gif,
			Jpeg,
			Png,
			Bmp
		}
		#endregion

		#region Private member variables
		BarFormat format;
		BarType type;
		bool border;
		System.Drawing.Color fillColor;
		double fillPercent;
		Unit barSize;
		#endregion

		#region Public properties
		[DefaultValue(BarFormat.Png), Category("ProgressBar Common"), Description("Compression format for image.")]
		public BarFormat Format
		{
			get { return format; }
			set { format = value; }
		}

		[DefaultValue(false), Category("ProgressBar Common"), Description("Flag to show border or not.")]
		public bool Border
		{
			get { return border; }
			set { border = value; }
		}

		[DefaultValue(BarType.Horizontal), Category("ProgressBar Common"), Description("Type of progress bar to render.")]
		public BarType Type
		{
			get { return type; }
			set { type = value; }
		}

		[Category("ProgressBar Common"), Description("Background color of progress bar.")]
		public System.Drawing.Color FillColor
		{
			get { return fillColor; }
			set { fillColor = value; }
		}

		[DefaultValue(0.0), Category("ProgressBar Common"), Description("Fill percentage of progress bar.")]
		public double FillPercent
		{
			get { return fillPercent; }
			set { fillPercent = value; }
		}

		[DefaultValue(0.0), Category("ProgressBar Common"), Description("Size of progress bar inside frame")]
		public Unit BarSize
		{
			get { return barSize; }
			set { barSize = value; }
		}

		[Browsable(false)]
		public string ContentType
		{
			get { return "image/" + Format.ToString().ToLower(); }
		}

		[Browsable(false)]
		public override System.Web.UI.WebControls.BorderStyle BorderStyle
		{
			get
			{
				return base.BorderStyle;
			}
			set
			{
				base.BorderStyle = value;
			}
		}

		[Browsable(false)]
		public override Unit BorderWidth
		{
			get
			{
				return base.BorderWidth;
			}
			set
			{
				base.BorderWidth = value;
			}
		}

		[Browsable(false)]
		public override Color BorderColor
		{
			get
			{
				return base.BorderColor;
			}
			set
			{
				base.BorderColor = value;
			}
		}

		[Browsable(false)]
		public override Color ForeColor
		{
			get
			{
				return base.ForeColor;
			}
			set
			{
				base.ForeColor = value;
			}
		}

		[Browsable(false)]
		public override bool EnableViewState
		{
			get
			{
				return base.EnableViewState;
			}
			set
			{
				base.EnableViewState = value;
			}
		}


		[Browsable(false)]
		public override string AccessKey
		{
			get
			{
				return base.AccessKey;
			}
			set
			{
				base.AccessKey = value;
			}
		}

		[Browsable(false)]
		public override string CssClass
		{
			get
			{
				return base.CssClass;
			}
			set
			{
				base.CssClass = value;
			}
		}
		#endregion

		#region Rendering Methods

		//Initial request for WebChart object
		protected override void Render(HtmlTextWriter output)
		{

			if (this.Site != null && this.Site.DesignMode )
			{
				output.Write(string.Format("<font size='1'  color='SeaGreen' face='arial'>&nbsp;[ProgressBar::{0}]</font><br>", ID) );
			}
			else
			{
				string uniqueName = GenerateUniqueName();

				Page.Application[ProgressBarRenderStream.ImageNamePrefix + uniqueName] = this;

				//Write relative URL for image stream request
				output.Write( string.Format("<img src='{0}?id={1}' border='{2}' height='{3}' width='{4}' alt='{5}'>", ProgressBarRenderStream.ImageHandlerRequestFilename, uniqueName, (this.Border ? "1" : "0"), (int)this.Height.Value, (int)this.Width.Value, this.Enabled ? ((this.ToolTip == null || this.ToolTip == string.Empty) ? System.Math.Round(this.FillPercent, 2).ToString()+"%" : this.ToolTip) : "") );
			}
		}

		public MemoryStream RenderProgressBar()
		{
			try
			{
				if( Site != null && Site.DesignMode )
				{
					string sType = this.Type.ToString();
					return null;
				}
				else
				{
					return makeProgressBar();
				}
			}
			catch
			{
				return null;
			}
		}

		private MemoryStream makeProgressBar()
		{
			Bitmap bmp = new Bitmap((int)Width.Value, (int)Height.Value, PixelFormat.Format32bppArgb);
			MemoryStream memStream = new MemoryStream();
	
			Brush fillBrush = new SolidBrush( FillColor );
			Brush backgroundBrush = new SolidBrush( this.BackColor );

			// draw background
			System.Drawing.Graphics graphics = Graphics.FromImage( bmp );
			graphics.FillRectangle(backgroundBrush, 0, 0, (int)Width.Value, (int)Height.Value);

			double fillAmount;
			if( this.Type == BarType.Horizontal )
			{
				// draw a horizontal bar
				// draw only BarSize height, centered vertically
				// inside the frame
				fillAmount = Width.Value * (FillPercent/100.0);
				graphics.FillRectangle(fillBrush, 0, 
					((int)Height.Value - (int)BarSize.Value)/2, 
					(int)fillAmount, (int)BarSize.Value);
			}
			else
			{
				// draw a vertical bar
				// draw only BarSize width, centered horizontally
				// inside the frame
				fillAmount = Height.Value * (FillPercent/100.0);
				graphics.FillRectangle(fillBrush, ((int)Width.Value - (int)BarSize.Value)/2, 
					(int)Height.Value-(int)fillAmount, 						
					(int)BarSize.Value, (int)Height.Value);
			}

			graphics.Save();

			System.Drawing.Imaging.ImageFormat imgformat = System.Drawing.Imaging.ImageFormat.Png;
			switch( Format )
			{
				case BarFormat.Bmp:
					imgformat = ImageFormat.Bmp;
					break;

				case BarFormat.Gif:
					imgformat = ImageFormat.Gif;
					break;

				case BarFormat.Jpeg:
					imgformat = ImageFormat.Jpeg;
					break;

				case BarFormat.Png:
					imgformat = ImageFormat.Png;
					break;
			}

			// Render BitMap Stream Back To Client
			bmp.Save(memStream, imgformat);
			return memStream;
		}

		//Generates a new name for this control & registers 
		string GenerateUniqueName()
		{
			string sControlName = System.Guid.NewGuid().ToString();

			//Identifies requested ProgressBar image
			return sControlName;
		}

		#endregion
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I am a software engineer with over 20 years experience. My interests include web application development, graphics programming, network connectivity, and game programming.

Comments and Discussions