.
In Depth Banner
Skip Navigation Links

Select your preferred language

Listing 2. The DropShadow application.

Note that this simple application requires a copy of the Sunset.jpg image. You may substitute any image you like from your system.

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 DropShadow

{

  /// <summary>

  /// Summary description for Form1.

  /// </summary>

  public class Form1 : System.Windows.Forms.Form

  {

    /// <summary>

    /// Required designer variable.

    /// </summary>

    private System.ComponentModel.Container components = null;

 

    public Form1()

    {

      //

      // Required for Windows Form Designer support

      //

      InitializeComponent();

 

      SetStyle(ControlStyles.DoubleBuffer|

          ControlStyles.AllPaintingInWmPaint|

          ControlStyles.UserPaint|

          ControlStyles.ResizeRedraw, 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()

    {

      System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));

      this.trackBar1 = new System.Windows.Forms.TrackBar();

      ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).BeginInit();

      this.SuspendLayout();

      //

      // trackBar1

      //

      this.trackBar1.Dock = System.Windows.Forms.DockStyle.Right;

      this.trackBar1.Location = new System.Drawing.Point(531, 0);

      this.trackBar1.Maximum = 255;

      this.trackBar1.Name = "trackBar1";

      this.trackBar1.Orientation = System.Windows.Forms.Orientation.Vertical;

      this.trackBar1.Size = new System.Drawing.Size(45, 342);

      this.trackBar1.TabIndex = 0;

      this.trackBar1.TickFrequency = 8;

      this.trackBar1.Scroll += new System.EventHandler(this.trackBar1_Scroll);

      //

      // Form1

      //

      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

      this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));

      this.ClientSize = new System.Drawing.Size(576, 342);

      this.Controls.Add(this.trackBar1);

      this.Name = "Form1";

      this.Text = "Form1";

      this.Load += new System.EventHandler(this.Form1_Load);

      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);

      ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).EndInit();

      this.ResumeLayout(false);

 

    }

    #endregion

 

    /// <summary>

    /// The main entry point for the application.

    /// </summary>

    [STAThread]

    static void Main()

    {

      Application.Run(new Form1());

    }

 

    private System.Windows.Forms.TrackBar trackBar1;

 

    Bitmap bm;

 

    private void Form1_Load(object sender, System.EventArgs e)

    {

      bm=(Bitmap)Image.FromFile("..\\..\\sunset.jpg");

      this.ClientSize=new Size(bm.Width,bm.Height);

    }

 

    public void RectangleDropShadow(Graphics tg, Rectangle rc, Color shadowColor, int depth, int maxOpacity)

    {

      //calculate the opacities

      Color darkShadow=Color.FromArgb(maxOpacity,shadowColor);

      Color lightShadow=Color.FromArgb(0,shadowColor);

      //Create a brush that will create a softshadow circle

      GraphicsPath gp=new GraphicsPath();

      gp.AddEllipse(0,0,2*depth,2*depth);

      PathGradientBrush pgb=new PathGradientBrush(gp);

      pgb.CenterColor=darkShadow;

      pgb.SurroundColors=new Color[]{lightShadow};

      //generate a softshadow pattern that can be used to paint the shadow

      Bitmap patternbm=new Bitmap(2*depth,2*depth);

      Graphics g=Graphics.FromImage(patternbm);

      g.FillEllipse(pgb,0,0,2*depth,2*depth);

      g.Dispose();

      pgb.Dispose();

 

      SolidBrush sb=new SolidBrush(Color.FromArgb(maxOpacity,shadowColor));

      tg.FillRectangle(sb,rc.Left+depth,rc.Top+depth,rc.Width-(2*depth),rc.Height-(2*depth));

      sb.Dispose();

      //top left corner

      tg.DrawImage(patternbm,new Rectangle(rc.Left,rc.Top,depth,depth),0,0,depth,depth,GraphicsUnit.Pixel);

      //top side

      tg.DrawImage(patternbm,new Rectangle(rc.Left+depth,rc.Top,rc.Width-(2*depth),depth),depth,0,1,depth,GraphicsUnit.Pixel);

      //top right corner

      tg.DrawImage(patternbm,new Rectangle(rc.Right-depth,rc.Top,depth,depth),depth,0,depth,depth,GraphicsUnit.Pixel);

      //right side

      tg.DrawImage(patternbm,new Rectangle(rc.Right-depth,rc.Top+depth,depth,rc.Height-(2*depth)),depth,depth,depth,1,GraphicsUnit.Pixel);

      //bottom left corner

      tg.DrawImage(patternbm,new Rectangle(rc.Right-depth,rc.Bottom-depth,depth,depth),depth,depth,depth,depth,GraphicsUnit.Pixel);

      //bottom side

      tg.DrawImage(patternbm,new Rectangle(rc.Left+depth,rc.Bottom-depth,rc.Width-(2*depth),depth),depth,depth,1,depth,GraphicsUnit.Pixel);

      //bottom left corner

      tg.DrawImage(patternbm,new Rectangle(rc.Left,rc.Bottom-depth,depth,depth),0,depth,depth,depth,GraphicsUnit.Pixel);

      //left side

      tg.DrawImage(patternbm,new Rectangle(rc.Left,rc.Top+depth,depth,rc.Height-(2*depth)),0,depth,depth,1,GraphicsUnit.Pixel);

 

      patternbm.Dispose();

 

    }

 

    int opacity=128;

 

    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)

    {

      RectangleDropShadow(e.Graphics,new Rectangle(30,30,bm.Width,bm.Height), Color.Black, 20, opacity);

      e.Graphics.DrawImageUnscaled(bm,10,10);

    }

 

    private void trackBar1_Scroll(object sender, System.EventArgs e)

    {

      opacity=this.trackBar1.Value;

      Invalidate();

    }

  }

}

 

Press your browser Back button to return to the per-pixel alpha article.

 

Copyright © Bob Powell 2000-.  All rights reserved.