From iLab Cookbook

Toolkits: ImageAlphaBlending

<< Back to the EasyImage Toolkit page

Tutorial - Alpha Blending Two Images

What you will learn

This tutorial shows:

In the above picture,

Download

While we recommend you create this program from scratch, you can also download the source and executables.

Source and Executables of this and other examples: EasyImagesExamples.zip
  • Unzip all files into a single folder.
  • Try the executable; a shortcut is in the top-level directory, or look in the debug directory
  • You will need a web camera attached to your computer.

Step 1. Preconditions

Including EasyImages in your new Visual Studio 2005 C# project. This was described in Minimal Camera Example. Make sure to include the using EasyImages; line in your project.

Step 2. Creating GUI controls

Add the following GUI controls to your form window. Thei locations and purpose should be obvious if you check the image at the top of this page.

Step 3. Include 2 images as Resources

Two images should be included as resources, named image1 and image2. It is beyond the scope of this tutorial to teach you how to use resources (this is standard in VS 2005), but if you are unsure, just read two images of the same size from files.

Step 4. Writing the program

The complete program is listed below.

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Resources;
using EasyImages;
namespace ImageAlphaBlending
{
  public partial class Form1 : Form
  {
    //Get these two images (they are saved as resources as part of this project)
    Image image1 = Images.Picture1;
    Image image2 = Images.Picture2;

    public Form1()
    {
      InitializeComponent();
    }

    // Show the images when the form is loaded
    private void Form1_Load(object sender, EventArgs e)
    {
      ShowImage();
    }

    // Show the images when the alpha blending level is reset
    private void tbAlphaBlend_Scroll(object sender, EventArgs e)
    {
      ShowImage();
    }

    // Alpha-blend the two images (use the alpha level in the trackbar)
    // Then display it as well as the alpha value
    private void ShowImage()
    {
      pbImage.Image = EasyImages.ImageGenerator.AlphaBlend(image2, image1, tbAlphaBlend.Value / 100.0f);
      lblAlphaLevel.Text = "Alpha Blending Level: " + tbAlphaBlend.Value.ToString ();
    }
  }
}

Explanation

Retrieved from http://grouplab.cpsc.ucalgary.ca/cookbook/index.php/Toolkits/ImageAlphaBlending
Page last modified on June 23, 2007, at 01:43 PM