功能:实现图片的读取,显示,获取当前鼠标点的RGB数值; 控件布局:1个pictureBox,1个button,5个textBox,5个Label; 原理:增加mousemove事件,当图片在pictureBox中显示时(pick=true),滑动鼠标,则可以将RGB值以及当前坐标显示在textBox中
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DisplayPixelValue
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//表示是否拾取图片上的颜色
private bool pick;
public Bitmap myBitmap;

private void Form1_Load(object sender, EventArgs e)
{
pick = false;
}

//事件主要是完成读入文件和对pictureBox控件的初始化
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog of = new OpenFileDialog();
of.Filter = "JPG图像(*.jpg)|*.jpg|BMP图像(*.bmp)|*.bmp|所有文件(*.*)|*.*";
of.FilterIndex = 0;
of.ShowDialog();
String filename = of.FileName.ToString();

//在此处加载一个新的图片。
this.myBitmap = new Bitmap(filename);
// Stretches the image to fit the pictureBox.
pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
Bitmap myImage = this.myBitmap;//new Bitmap(fileToDisplay);
//pictureBox1.ClientSize = new Size(xSize, ySize);
pictureBox1.Image = (Image)myImage;
//设置当前窗体与图片大小相当
//this.ClientSize = new Size(pictureBox1.Width + 20, pictureBox1.Height + 60);
pick = true;//设为TRUE可以开始读取像素点。

}
//增加mousemove事件
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (pick)
{
Color pixelColor = this.myBitmap.GetPixel(e.X, e.Y);
//pixelColor = Color.FromArgb(255,0,0);//可以以此方法对COLOR变量进行RGB值设置。
textBox1.Text = pixelColor.R.ToString();
textBox2.Text = pixelColor.G.ToString();
textBox3.Text = pixelColor.B.ToString();
textBox4.Text = e.X.ToString();
textBox5.Text = e.Y.ToString();

}
}
}
}