Home

Example Application: The Color Selector

 

Introduction

To paint objects and produce colors on the screen, Microsoft Windows uses a process of painting each pixel. There are two things you can be interested in with this information. You can paint a pixel by specifying its color. On the other hand, you can retrieve the color of a pixel and use it for any necessary reason.

To assist you with identifying the color of a pixel, the Bitmap class of the .NET Framework is equipped with a method named GetPixel, which produces the color assigned to a specific pixel. To identify the pixel itself, the Bitmap.GetPixel() method takes two arguments that are the Cartesian coordinates of the pixel.

 

Practical LearningPractical Learning: Accessing the Color of a Pixel

  1. To create a new program, on the main menu, click File -> New -> Project...
  2. In the Templates list, click Windows Application
  3. Set the Name to ColorSelector1 and click OK
  4. Copy the following picture and paste it somewhere in your computer
     
  5. Design the form as follows:
     
    Color Selector
    Control Text Name Other Properties
    PictureBox   pbxColor Image: colorpal1.jpg
    Label Red:    
    TextBox   txtRed  
    Label Green:    
    TextBox   txtGreen  
    Label Blue:    
    TextBox   txtBlue  
    Panel   pnlPreview BorderStyle: FixedSingle
    Button Close btnClose  
  6. Right-click the form and click View Code
  7. 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();
            }
        }
    }
  8. Return to the form and double-click an unoccupied area of its body
  9. Implement the event as follows:
     
    private void Form1_Load(object sender, EventArgs e)
    {
                isSelecting = false;
    }
  10. Return to the form and click the picture box control on it
  11. In the Properties window, click the Events button Events and double-click MouseDown
  12. Implement the event as follows:
     
    private void pbxColor_MouseDown(object sender, MouseEventArgs e)
    {
                isSelecting = true;
    }
  13. Return to the form and click the picture box control on it
  14. 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;
    }
  15. Return to the form and click the picture box control on it
  16. 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;
            }
    }
  17. Execute the application
  18. Click the mouse on the picture mouse and drag to produce a color
     
    Color Selector
  19. Close the form

 

 
 

Home Copyright 2008-2009, yevol.com