Results 1 to 9 of 9

Thread: Pixels of coordinates from a picturebox

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2012
    Posts
    17

    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.

  2. #2
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    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:
    1. Private Sub PB_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PB.MouseDown
    2.         Dim TempBitmap As New Bitmap(PB.Image)
    3.         Dim MyColor As Color = TempBitmap.GetPixel(e.X, e.Y)
    4.         MessageBox.Show("Pixel x=" & e.X & ", y=" & e.Y & ", color=" & MyColor.ToString, "Color", MessageBoxButtons.OK, MessageBoxIcon.Information)
    5. 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:
    1. Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
    2.         Dim img As Drawing.Image = New Bitmap(PB.Width, PB.Height)
    3.         Dim gr As Drawing.Graphics = Graphics.FromImage(img)
    4.        
    5.         'Sample drawing..
    6.         gr.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
    7.         gr.FillRegion(Brushes.GreenYellow, New Region(PB.ClientRectangle))
    8.         gr.DrawLine(Pens.Blue, New Point(0, 0), New Point(PB.Width, PB.Height))
    9.  
    10.         PB.Image = img
    11.  
    12.         gr.Dispose()
    13.     End Sub
    Last edited by jcis; May 15th, 2012 at 10:58 PM.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2012
    Posts
    17

    Re: Pixels of coordinates from a picturebox

    Quote Originally Posted by jcis View Post
    You can do it creating a Bitmap from the Image in the PictureBox, example with PictureBox named PB:
    VB.NET Code:
    1. Private Sub PB_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PB.MouseDown
    2.         Dim TempBitmap As New Bitmap(PB.Image)
    3.         Dim MyColor As Color = TempBitmap.GetPixel(e.X, e.Y)
    4.         MessageBox.Show("Pixel x=" & e.X & ", y=" & e.Y & ", color=" & MyColor.ToString, "Color", MessageBoxButtons.OK, MessageBoxIcon.Information)
    5. 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:
    1. Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
    2.         Dim img As Drawing.Image = New Bitmap(PB.Width, PB.Height)
    3.         Dim gr As Drawing.Graphics = Graphics.FromImage(img)
    4.        
    5.         'Sample drawing..
    6.         gr.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
    7.         gr.FillRegion(Brushes.GreenYellow, New Region(PB.ClientRectangle))
    8.         gr.DrawLine(Pens.Blue, New Point(0, 0), New Point(PB.Width, PB.Height))
    9.  
    10.         PB.Image = img
    11.  
    12.         gr.Dispose()
    13.     End Sub
    When I use the first code I still get an error.

  4. #4
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    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.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    May 2012
    Posts
    17

    Re: Pixels of coordinates from a picturebox

    Quote Originally Posted by jcis View Post
    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).

  6. #6

    Thread Starter
    Junior Member
    Join Date
    May 2012
    Posts
    17

    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.

  7. #7
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    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

  8. #8

    Thread Starter
    Junior Member
    Join Date
    May 2012
    Posts
    17

    Re: Pixels of coordinates from a picturebox

    Quote Originally Posted by boops boops View Post
    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"

  9. #9
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    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
  •  



Click Here to Expand Forum to Full Width