Click here to Skip to main content
15,885,767 members
Articles / Programming Languages / C#

Transparency Tutorial with C# - Part 1

Rate me:
Please Sign up or sign in to vote.
4.87/5 (56 votes)
23 Mar 2004CPOL11 min read 300.6K   12.1K   220  
An article on alpha blending and transparency
// The color finder is based on WhatColor.cs by Charles Petzold, www.charlespetzold.com
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;


namespace Spectrum
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		private System.Windows.Forms.Label labelColor;
		private System.Windows.Forms.Label labelHex;
		private System.Windows.Forms.Label labelDec;

		private WhatColor whatColor;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
			whatColor = new WhatColor();

			SetStyle(ControlStyles.AllPaintingInWmPaint |
				ControlStyles.UserPaint |
				ControlStyles.DoubleBuffer,
				true);
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.labelColor = new System.Windows.Forms.Label();
			this.labelHex = new System.Windows.Forms.Label();
			this.labelDec = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// labelColor
			// 
			this.labelColor.Font = new System.Drawing.Font("Verdana", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.labelColor.ForeColor = System.Drawing.Color.Black;
			this.labelColor.Location = new System.Drawing.Point(220, 96);
			this.labelColor.Name = "labelColor";
			this.labelColor.Size = new System.Drawing.Size(132, 16);
			this.labelColor.TabIndex = 0;
			this.labelColor.Text = "Red-Green-Blue";
			this.labelColor.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// labelHex
			// 
			this.labelHex.Font = new System.Drawing.Font("Verdana", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.labelHex.ForeColor = System.Drawing.Color.Black;
			this.labelHex.Location = new System.Drawing.Point(212, 112);
			this.labelHex.Name = "labelHex";
			this.labelHex.Size = new System.Drawing.Size(136, 16);
			this.labelHex.TabIndex = 1;
			this.labelHex.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// labelDec
			// 
			this.labelDec.Font = new System.Drawing.Font("Verdana", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.labelDec.ForeColor = System.Drawing.Color.Black;
			this.labelDec.Location = new System.Drawing.Point(216, 128);
			this.labelDec.Name = "labelDec";
			this.labelDec.Size = new System.Drawing.Size(136, 16);
			this.labelDec.TabIndex = 2;
			this.labelDec.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.BackColor = System.Drawing.Color.White;
			this.ClientSize = new System.Drawing.Size(576, 150);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.labelDec,
																		  this.labelHex,
																		  this.labelColor});
			this.Name = "Form1";
			this.Text = "Light Spectrum";
			this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			labelHex.Text = whatColor.CursorColor.R.ToString("X00")+"-"+whatColor.CursorColor.G.ToString("X00")+"-"+whatColor.CursorColor.B.ToString("X00");		
			labelDec.Text = whatColor.CursorColor.R.ToString()+"-"+whatColor.CursorColor.G.ToString()+"-"+whatColor.CursorColor.B.ToString();		
			
		}

		private float brushTransparency = 1.0f;
		private Bitmap bitmap = new Bitmap(600,100);
		private Rectangle rect = new Rectangle(8,24,560,70);
		protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
		{
			// Fill in Background (for effieciency only the area that has been clipped)
			e.Graphics.FillRectangle(new SolidBrush(SystemColors.Window), e.ClipRectangle.X,e.ClipRectangle.Y, e.ClipRectangle.Width, e.ClipRectangle.Height);

			LinearGradientBrush brBrush = 
				new LinearGradientBrush(
				rect, Color.Blue, Color.Red, 
				LinearGradientMode.Horizontal);
				
			// Create color and point arrays
			Color[] clrArray =
					{
						Color.FromArgb(255,0,0,0),
						Color.FromArgb(255,128,0,128),
						Color.FromArgb(255,255,0,255),
						Color.FromArgb(255,128,0,255),
						Color.FromArgb(255,0,0,255),
						Color.FromArgb(255,0,128,255),
						Color.FromArgb(255,0,255,255),
						Color.FromArgb(255,0, 255,128),
						Color.FromArgb(255,0,255,0),
						Color.FromArgb(255,128,255,0),
						Color.FromArgb(255,255,255,0),
						Color.FromArgb(255,255,128,0),
						Color.FromArgb(255,255,0,0),
						Color.FromArgb(255,128,0,0),
						Color.FromArgb(255,0,0,0)
					};
			float[] posArray =
					{
						0.0f,
						1.0f/14.0f,
						2.0f/14.0f,
						3.0f/14.0f,
						4.0f/14.0f,
						5.0f/14.0f,
						6.0f/14.0f,
						7.0f/14.0f,
						8.0f/14.0f,
						9.0f/14.0f,
						10.0f/14.0f,
						11.0f/14.0f,
						12.0f/14.0f,
						13.0f/14.0f,
						1.0f

					};

			// Create a ColorBlend object and
			// set its Colors and Positions properties
			ColorBlend colorBlend = new ColorBlend();
			colorBlend.Colors = clrArray;
			colorBlend.Positions = posArray;
			// Set interpolationColors property
			brBrush.InterpolationColors = colorBlend;

			// Create a points array
			float[][] ptsArray = 
				{
					new float[] { 1, 0, 0, 0, 0},
					new float[] { 0, 1, 0, 0, 0},
					new float[] { 0, 0, 1, 0, 0},
					new float[] { 0, 0, 0, brushTransparency, 0},
					new float[] { 0, 0, 0, 0, 1}
				};

			// Fill Rectangle
			e.Graphics.FillRectangle(brBrush, rect);

			// Dispose of objects
			brBrush.Dispose();		
		}


	}
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Writer Smiley Micros
United States United States
www.smileymicros.com

Comments and Discussions