动态看图像像素值的一个小程序
时间:2011-05-15 来源:xiaying
因为有时候需要动态的分析某个像素的RGB值,所以用了在pictureBox上的鼠标移动响应函数
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
bool pick;
pick = true; //设为TRUE可以开始读取像素点。
if (flag == 1)//打开图片了
{
if (pick && e.X < myBitmap.Width && e.Y < myBitmap.Height)
{
textBox8.Text = e.X.ToString();
textBox9.Text = e.Y.ToString();
Color pixelColor = this.myBitmap.GetPixel(e.X, e.Y);
textBox1.Text = pixelColor.R.ToString();
textBox2.Text = pixelColor.G.ToString();
textBox3.Text = pixelColor.B.ToString();
int r, g, b;
r = pixelColor.R;
g = pixelColor.G;
b = pixelColor.B;
textBox6.Text = pixelColor.GetHue().ToString();
double rgb;
rgb = r * 0.11 + g * 0.5 + b * 0.3;
textBox4.Text = rgb.ToString();
float hue;
float brightness;
float saturation;
int max_int = getmaxrgb(pixelColor);
int min_int = getminrgb(pixelColor);
brightness = (float)max_int / (float)255;
if (max_int == min_int)
hue = saturation = 0;
else
{
float maxv = (float)max_int / (float)255;
float minv = (float)min_int / (float)255;
float dm = maxv - minv;
saturation = dm / maxv;
if (max_int == pixelColor.R)
hue = (float)(pixelColor.G - pixelColor.B) / (float)(max_int - min_int);
else if (max_int == pixelColor.G)
hue = (float)2 + (float)(pixelColor.B - pixelColor.R) / (float)(max_int - min_int);
else
hue = (float)4 + (float)(pixelColor.R - pixelColor.G) / (float)(max_int - min_int);
hue = hue * (float)60;
if (hue < 0.0)
hue = hue + (float)360;
}
int hsiH=(int)hue;
textBox5.Text = hsiH.ToString();
//后来才发现计算H值是可以直接调用函数
}
}
}