获取指定点的RGB值

Posted on 2019-01-15 15:25  努力成长静待花开  阅读(224)  评论(0编辑  收藏  举报

实现效果:

  

知识运用:

  Color对象的RGB属性

实现代码:

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "*.jpg,*.jpeg,*.bmp,*.png,*.tif,*.wmf|*.jpg;*.jpeg;*.bmp;*.png;*.tif;*.wmf";
            open.ShowDialog();
            Image image = System.Drawing.Image.FromFile(open.FileName);
            pictureBox1.Image = image;
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            Bitmap bitmap = (Bitmap)pictureBox1.Image;
            try
            {
                Color color = bitmap.GetPixel(e.X,e.Y);
                textBox1.Text = color.R.ToString();
                textBox2.Text = color.G.ToString();
                textBox3.Text = color.B.ToString();
            }
            catch {}
        }