Home

Example Application: The Color Selector

     

Introduction

Microsoft Windows provides a function used to get a color stored in a pixel. The .NET Framework supports this operation in the Bitmap class, which is equipped with a method named GetPixel. By calling this method, you can retrieve the color of a pixel.

   

ApplicationApplication: Creating the Application

  1. Start Microsoft Visual Studio
  2. To create a new program, on the main menu, click File -> New Project...
  3. In the middle list, click Windows Application
  4. Set the Name to ColorSelector1 and click OK
  5. Copy the following picture and paste it somewhere in your computer

    Color Palette

  6. Design the form as follows:
     
    Color Selector
    Control Text Name Other Properties
    PictureBox Picture Box   pbxColor Image: colorpal1.jpg
    Label Label Red:    
    TextBox TextBox   txtRed  
    Label Label Green:    
    TextBox TextBox   txtGreen  
    Label Label Blue:    
    TextBox TextBox   txtBlue  
    Panel Panel   pnlPreview BorderStyle: FixedSingle
    Button Button Close btnClose  
  7. Right-click the form and click View Code
  8. Above the public Form1() line, declare a Boolean variable named IsSelecting:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace ColorSelector1
    {
        public partial class Form1 : Form
        {
            bool isSelecting;
    
            public Form1()
            {
                InitializeComponent();
            }
        }
    }
  9. Return to the form and double-click an unoccupied area of its body
  10. Implement the event as follows:
    private void Form1_Load(object sender, EventArgs e)
    {
                isSelecting = false;
    }
  11. Return to the form and click the picture box control on it
  12. In the Properties window, click the Events button and double-click MouseDown
  13. Implement the event as follows:
    private void pbxColor_MouseDown(object sender, MouseEventArgs e)
    {
                isSelecting = true;
    }
  14. Return to the form and click the picture box control on it
  15. In the Events section of the Properties window, double-click MouseUp and implement the event as follows:
    private void pbxColor_MouseUp(object sender, MouseEventArgs e)
    {
                isSelecting = false;
    }
  16. Return to the form and click the picture box control on it
  17. In the Events section of the Properties window, double-click MouseMove and implement the event as follows:
    private void pbxColor_MouseMove(object sender, MouseEventArgs e)
    {
            if (isSelecting == true)
            {
                    Bitmap bmpImage = (Bitmap)pbxColor.Image;
                    Color clr = bmpImage.GetPixel(e.X, e.Y);
    
                    txtRed.Text = clr.R.ToString();
                    txtGreen.Text = clr.G.ToString();
                    txtBlue.Text = clr.B.ToString();
    
                    pnlPreview.BackColor = clr;
            }
    }
  18. Execute the application
  19. Click the mouse on the picture mouse and drag to produce a color
     
  20. Close the form
 

Home Copyright © 2010-2014, FunctionX