Alpha Blending

I l @ ve RuBoard

All colors in GDI+ have a fourth component in addition to the normal red, green, and blue values. This value is the "alpha" and it controls the amount that the background shows through the object just placed onscreen.

Alpha blending can be applied to all graphical shapes , such as lines and text. It can also be applied to images, either as a global value that affects the appearance of the whole image, or as a value for each individual pixel, which makes parts of the image more transparent than others.

To create a color with an alpha component, you can use the Color.FromARGB( ) method and supply the alpha as a value from 0, totally transparent, to 255, which is opaque .

Listing 3.5.9 demonstrates this effect by creating an array of ellipses that let the background show through according to their alpha value.

Listing 3.5.9 AlphaBlend.cs: Using a Solid Brush with Alpha
 1: using System;  2: using System.Drawing;  3: using System.Drawing.Drawing2D;  4: using System.Collections;  5: using System.ComponentModel;  6: using System.Windows.Forms;  7: using System.Data;  8:  9: namespace Alphablending 10: { 11:    public class Alphablending : System.Windows.Forms.Form 12:    { 13: 14:       void OnPaint(object sender, PaintEventArgs e) 15:       { 16:          SolidBrush b=new SolidBrush(Color.Red); 17:          Rectangle r=this.ClientRectangle; 18:          GraphicsPath pth=new GraphicsPath(); 19:          for(int c=1;c<10;c++) 20:          { 21:             r.Inflate(-(this.ClientRectangle.Width/20), 22:                     -(this.ClientRectangle.Height/20)); 23:             pth.AddRectangle(r); 24:          } 25:          e.Graphics.FillPath(b,pth); 26:          Random rnd=new Random(); 27:          for(int y=0;y<5;y++) 28:          { 29:             for(int x=0;x<5;x++) 30:             { 31:                b.Color=Color.FromArgb((int)((((5*x)+y)*10.63)), 32:                                  (byte)rnd.Next(255), 33:                                  (byte)rnd.Next(255), 34:                                  (byte)rnd.Next(255)); 35:                e.Graphics.FillEllipse(b,this.ClientRectangle.Width/5*x, 36:                                   this.ClientRectangle.Height/5*y, 37:                                   this.ClientRectangle.Width/5, 38:                                   this.ClientRectangle.Height/5); 39:             } 40:          } 41:       } 42: 43:       void OnSize(object sender, EventArgs e) 44:       { 45:          Invalidate(); 46:       } 47: 48:       public Alphablending() 49:       { 50:          this.Paint+=new PaintEventHandler(OnPaint); 51:          this.SizeChanged+=new EventHandler(OnSize); 52:          this.BackColor=Color.White; 53:       } 54: 55: 56:       static void Main() 57:       { 58:          Application.Run(new Alphablending()); 59:       } 60:    } 61: } 

Compile Listing 3.5.9 with the following command line:

 csc /t:winexe alphablend.cs 

The output from Listing 3.5.9 is shown in Figure 3.5.12 and clearly shows the background scene, the dark concentric rectangles, being obscured by the graduated alpha blends of the ellipses.

Figure 3.5.12. Alpha blending at work.

graphics/0305fig12.gif

I l @ ve RuBoard


C# and the .NET Framework. The C++ Perspective
C# and the .NET Framework
ISBN: 067232153X
EAN: 2147483647
Year: 2001
Pages: 204

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net