VB. NET octopus program to obtain the RGB value of any image point
Abstract: Use the GetPixel (x, y) function in the Bitmap class to obtain the specified vertex color, and then obtain the values of R, G, and B respectively.
Create an empty form application, add a Picturebox control, a Button control, three label labels, and three Textbox controls to form Form1.
The following code is very simple. First, select an image and display it on the PictureBox control.
Private Sub btn_ImportPicture_Click (ByVal sender As System. object, ByVal e As System. eventArgs) Handles btn_ImportPicture.Click Dim OpenDialog1 As New OpenFileDialog OpenDialog1.Filter = "JPG format | *. jpg | JPEG format | *. jpeg | PNG format | *. png | ICO file and Gif file | *. ICO; * gif | all files *. * | *. * "If OpenDialog1.ShowDialog = DialogResult. OK Then Dim Path As String = OpenDialog1.FileName PictureBox1.Image = Image. fromFile (Path) End If End Sub
This is the core part. Very simple. Note that to use the GetPixel () function, you must first convert the image to the BitMap format.
Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown Dim GetColor As Color = CType(PictureBox1.Image, Bitmap).GetPixel(e.X, e.Y) txt_B.Text = GetColor.B.ToString txt_G.Text = GetColor.G.ToString txt_R.Text = GetColor.R.ToString End Sub
Yes. If you click a point on the image, the RGB value is displayed below.