How do I find the color of a pixel at a certain coordinate of an image in a picturebox?
I found a bunch of tutorials but they are irrelevant and do not work for vb 2008 express.
Thanks,
Yoni201.
Printable View
How do I find the color of a pixel at a certain coordinate of an image in a picturebox?
I found a bunch of tutorials but they are irrelevant and do not work for vb 2008 express.
Thanks,
Yoni201.
You can do it creating a Bitmap from the Image in the PictureBox, example with PictureBox named PB:
Click in the pbox to see the pixel colorVB.NET Code:
Private Sub PB_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PB.MouseDown Dim TempBitmap As New Bitmap(PB.Image) Dim MyColor As Color = TempBitmap.GetPixel(e.X, e.Y) MessageBox.Show("Pixel x=" & e.X & ", y=" & e.Y & ", color=" & MyColor.ToString, "Color", MessageBoxButtons.OK, MessageBoxIcon.Information) End Sub
Edit: The previous code assumes the PictureBox contains an image, if what you're doing is drawing using Drawing.Graphics then first you need to do some other things:
- Create a new Drawing.Image
- Create your Graphics from that Image
- Do all your drawing
- Assign the Image to the Image in the PictureBox
You can do these 4 steps like this (example using Form's Shown event)
VB.NET Code:
Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown Dim img As Drawing.Image = New Bitmap(PB.Width, PB.Height) Dim gr As Drawing.Graphics = Graphics.FromImage(img) 'Sample drawing.. gr.SmoothingMode = Drawing2D.SmoothingMode.HighQuality gr.FillRegion(Brushes.GreenYellow, New Region(PB.ClientRectangle)) gr.DrawLine(Pens.Blue, New Point(0, 0), New Point(PB.Width, PB.Height)) PB.Image = img gr.Dispose() End Sub
When I use the first code I still get an error.
http://i48.tinypic.com/288d8n.jpg
Did you read what I've wrote after EDIT:? Looks like you didn't set an image in the PIctureBox Image property. So select an Image as the PictureBox image or create your image by code as it's explained in the 2nd example.
I'm not drawing anything, I just want to be able to load an image into the picturebox, and find the colors for specific coordinates. no drawing.
You are not being clear. Are you talking about the coordinates of the image, or the coordinates of the picturebox? They will only be the same when you use the picturebox Image and set the picturebox's SizeMode to None.
Assume your picturebox is called PictureBox1 and you want the colour of the pixel at coordinates x, y. If the coordinates are relative to the image, all you need is:
If the coordinates are relative to the picturebox and you want it to work for any SizeMode including Stretch, Zoom etc., you can get a snapshot of the picturebox like this:Code:Dim pixelColor As Color = PictureBox1.Image.GetPixel(x, y)
Code:Dim w As Integer = PictureBox1.ClientSize.Width
Dim h As Integer = PictureBox1.ClientSize.Height
Dim bmp As New Bitmap(w, h)
PictureBox1.DrawToBitmap(bmp, PictureBox1.ClientRectangle)
'bmp is now a snapshot of the picturebox. So get the pixel colour...
Dim pixelColor As Color = bmp.GetPixel(x, y)
BB
Oops! My mistake, it's a Bitmap method so you have to do it like this:
BBCode:Dim bmp As Bitmap = CType(PictureBox1.Image, Bitmap)
Dim pixelColor As Color = bmp.GetPixel(x, y)