From iLab Cookbook

Toolkits: ImageToJPG

<< Back to the EasyImage Toolkit page

Tutorial - Image To JPG

What you will learn

This tutorial shows:

In the above picture,

Still to be done:

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. Writing the program

The complete program is listed below.

using System;
using System.Drawing;
using System.Windows.Forms;
using EasyImages;
// Author: Saul Greenberg, University of Calgary, http://www.cpsc.ucalgary.ca/~saul 
// Copyright: (c) June 7, 2007.
// Documentation: see http://grouplab.cpsc.ucalgary.ca/cookbook/ and select EasyImages.
// The API Documentation and a tutorial explaining this and other programs are available there
// License: See License details included in the distribution package. Essentialy, permission to
// use and/or alter this program for non-commerical and/or educational purposes is granted,
// as long as attribution to the above author is maintained.
namespace ImageToJPG
{
  public partial class Form1 : Form
  {
    EasyImages.CameraClient camera;
    private delegate void SetPictureBoxImage(PictureBox pbox, Bitmap image);
    byte[] b;

    public Form1()
    {
      InitializeComponent();
    }

    //As the GUI is loaded, create a new camera object and set its initial properties
    private void Form1_Load(object sender, EventArgs e)
    {
      //Create a new camera object and set its initial properties
      camera = new EasyImages.CameraClient ("DefaultCamera");
      camera.FramesPerSecond = 10;
      camera.ReceivedFrame += new EasyImages.CameraClient.ReceivedFrameEventHandler (camera_ReceivedFrame);
      camera.Start();
      lblJPEGQuality.Text = "JPEG Quality: " + tbJPEG.Value.ToString() + "%";
    }

    void camera_ReceivedFrame(object sender, EasyImages.CameraEventArgs e)
    {
      // Display the image
      DisplayImageInPictureBox (this.pbOriginal, e.Bitmap);

      // Get a JPEG version of this image using the quality level specified in the trackbar
      EasyImages.JPEG jpeg = new EasyImages.JPEG (e.Bitmapthis.tbJPEG.Value);
      DisplayImageInPictureBox (this.pbJPG, jpeg.GetImage () );

      // Calculate the size of the original and the JPEG image
      this.lblOriginal.Text = "Size of Original: Unknown";
      this.b = jpeg.GetByteArray ();
      this.lblJPEG.Text = "JPEG image (size = " + b.Length.ToString () + ")"
    }

    private void tbJPEG_Scroll(object sender, EventArgs e)
    {
      lblJPEGQuality.Text = "Quality: " + tbJPEG.Value.ToString() + "%";
    }

    //Display the image in the picturebox in the correct thread
    private void DisplayImageInPictureBox(PictureBox pbox, Image image)
    {
      if (pbox.InvokeRequired) // We are in the wrong thread. Call ourselves in the correct thread
      {
        SetPictureBoxImage theDelegate = new SetPictureBoxImage(DisplayImageInPictureBox);
        BeginInvoke(theDelegate, new object[] { pbox, image });
      }
      else // we are in the correct thread, so assign the image
      {
        pbox.Image = image;
      }
    }
  }
}

Explanation

Much of this program is similar to what was seen in the Minimal Camera, so we only describe what is different.

Retrieved from http://grouplab.cpsc.ucalgary.ca/cookbook/index.php/Toolkits/ImageToJPG
Page last modified on July 17, 2007, at 07:42 PM