|
-
May 15th, 2012, 06:25 PM
#1
Thread Starter
Junior Member
Pixels of coordinates from a picturebox
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.
-
May 15th, 2012, 09:56 PM
#2
Re: Pixels of coordinates from a picturebox
You can do it creating a Bitmap from the Image in the PictureBox, example with PictureBox named PB:
VB.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
Click in the pbox to see the pixel color
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
Last edited by jcis; May 15th, 2012 at 10:58 PM.
-
May 15th, 2012, 11:41 PM
#3
Thread Starter
Junior Member
Re: Pixels of coordinates from a picturebox
 Originally Posted by jcis
You can do it creating a Bitmap from the Image in the PictureBox, example with PictureBox named PB:
VB.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
Click in the pbox to see the pixel color
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.
-
May 15th, 2012, 11:48 PM
#4
Re: Pixels of coordinates from a picturebox
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.
-
May 15th, 2012, 11:52 PM
#5
Thread Starter
Junior Member
Re: Pixels of coordinates from a picturebox
 Originally Posted by jcis
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 didn't read the error carefully enough. I DID set an image this time (a bmp image), and I STILL get an error (a different one, though).
-
May 15th, 2012, 11:52 PM
#6
Thread Starter
Junior Member
Re: Pixels of coordinates from a picturebox
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.
-
May 16th, 2012, 03:53 AM
#7
Re: Pixels of coordinates from a picturebox
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:
Code:
Dim pixelColor As Color = PictureBox1.Image.GetPixel(x, y)
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 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
-
May 16th, 2012, 07:08 PM
#8
Thread Starter
Junior Member
Re: Pixels of coordinates from a picturebox
 Originally Posted by boops boops
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:
Code:
Dim pixelColor As Color = PictureBox1.Image.GetPixel(x, y)
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 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
The first code is what I was referring to.
I guess error when I use it:
"'GetPixel' is not a member of System.drawing.image"
-
May 17th, 2012, 12:31 AM
#9
Re: Pixels of coordinates from a picturebox
Oops! My mistake, it's a Bitmap method so you have to do it like this:
Code:
Dim bmp As Bitmap = CType(PictureBox1.Image, Bitmap)
Dim pixelColor As Color = bmp.GetPixel(x, y)
BB
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|