![]() |
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.
Practical Learning: Creating the Application
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 |
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();
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
isSelecting = false;
}
private void pbxColor_MouseDown(object sender, MouseEventArgs e)
{
isSelecting = true;
}
private void pbxColor_MouseUp(object sender, MouseEventArgs e)
{
isSelecting = false;
}
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; } }
|
||
Home | Copyright © 2010-2020, FunctionX | |
|