.
In Depth Banner
Skip Navigation Links

Select your preferred language

Listing 1 code.

 

using System;

using System.Drawing;

using System.Drawing.Imaging;

using System.Drawing.Drawing2D;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

 

namespace simpleblendcs

{

  /// <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();

 

      //

      // TODO: Add any constructor code after InitializeComponent call

      //

    }

 

    /// <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()

    {

      //

      // Form1

      //

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

      this.ClientSize = new System.Drawing.Size(292, 266);

      this.Name = "Form1";

      this.Text = "Form1";

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

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

 

    }

    #endregion

 

    /// <summary>

    /// The main entry point for the application.

    /// </summary>

    [STAThread]

    static void Main()

    {

      Application.Run(new Form1());

    }

 

    Bitmap bm;

 

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

    {

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

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

    }

 

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

    {

      if(bm==null)

        return;

 

      //Draw an image with some alphablended objects on.

 

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

      for(int y=0;y<8;y++)

      {

        for(int x=0;x<8;x++)

        {

          Color c=Color.FromArgb(32*y+(4*x),Color.Black);

          SolidBrush sb=new SolidBrush(c);

          e.Graphics.FillEllipse(sb,bm.Width/8*x,bm.Height/8*y,bm.Width/8,bm.Height/8);

          sb.Dispose();

        }

      }

 

      //now draw some solid objects with transparent images on them

 

      GraphicsPath gp=new GraphicsPath();

      gp.AddRectangle(new Rectangle(0,0,bm.Width/8,bm.Height/8));

      gp.AddEllipse(bm.Width/80,bm.Height/80,(bm.Width/8)-(bm.Width/40),(bm.Height/8)-(bm.Width/40));

 

      for(int y=0;y<8;y++)

      {

        for(int x=0;x<8;x++)

        {

          ImageAttributes ia=new ImageAttributes();

          ColorMatrix cm=new ColorMatrix();

          cm.Matrix33=1.0f/255*(32f*y+(4f*x));

          ia.SetColorMatrix(cm);

          Matrix mx=new Matrix(1,0,0,1,x*bm.Width/8+bm.Width,y*bm.Height/8);

          e.Graphics.Transform=mx;

          SolidBrush sb=new SolidBrush(Color.Black);

          e.Graphics.FillPath(sb,gp);

          sb.Dispose();

          e.Graphics.Transform=new Matrix();

          e.Graphics.DrawImage(bm,

            new Rectangle(bm.Width+x*(bm.Width/8),

              y*(bm.Height/8),

              bm.Width/8,

              bm.Height/8),

            x*(bm.Width/8),

            y*(bm.Height/8),

            bm.Width/8,

            bm.Height/8,GraphicsUnit.Pixel,

            ia);

          ia.Dispose();

        }

      }

    }

 

  }

}

 

Press your browser Back button to return to the article.

Copyright © Bob Powell 2000-.  All rights reserved.