Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET Interface and Graphics > Get a picturebox's clicked pixel color


Reply
 
Thread Tools Display Modes
  #1  
Old 07-31-2010, 06:22 AM
coocool321's Avatar
coocool321 coocool321 is offline
Newcomer
 
Join Date: Jul 2010
Posts: 11
Question Get a picturebox's clicked pixel color

Hello again
I am currently creating an app and i want to be able to get the colour of a pixel, but in a picturebox.
Example: I click anywhere in the picturebox and it will give me the color of the pixel I clicked.
Does anybody know how to do that? Thanks in advance
__________________
Coocool321, answering some of your questions, since 2010.
Reply With Quote
  #2  
Old 08-02-2010, 03:45 AM
shekoasinger shekoasinger is offline
Freshman
 
Join Date: May 2010
Location: The Netherlands
Posts: 44
Cool

I actually know how to do it, but it wont be the picturebox only, you would have to do it yourself. So that when you click, it knows on what pixel you have clicked? thats easy men. I will just give you some help I wont write the full code!

Make 3 labels. and 1 extra picturebox.
The labels are for the red, green and blue values, the picturebox is for the whole colour.
Code:
            Dim lColor As Integer
            Dim lDC As Integer
            lDC = GetWindowDC(0)
            lColor = GetPixel(lDC, MousePosition.X, MousePosition.Y)
            Dim r As Integer = lColor Mod 256
            Dim g As Integer = (lColor \ 256) Mod 256
            Dim b As Integer = lColor \ 256 \ 256
            PictureBox1.BackColor = Color.FromArgb(r, g, b)
            Button1.Text = r
            Button2.Text = g
            Button3.Text = b
and you would need this declarations just below public class form1.
Code:
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Integer, ByVal x As Integer, ByVal y As Integer) As Integer
    Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Integer) As Integer
Reply With Quote
  #3  
Old 08-02-2010, 06:55 AM
Qua's Avatar
Qua Qua is offline
Impetuous & volatile

* Expert *
 
Join Date: Apr 2005
Location: 127.0.0.1
Posts: 2,016
Default

If the picturebox contains a bitmap then you can use the GetPixel method to retrieve the pixel of a specific point.
Code:
DirectCast(pb1.Image, Bitmap).GetPixel(1,1)
__________________
Reading is the foundation for all knowledge - Unknown.
Reply With Quote
  #4  
Old 08-25-2010, 07:03 AM
coocool321's Avatar
coocool321 coocool321 is offline
Newcomer
 
Join Date: Jul 2010
Posts: 11
Default

Thanks for all your help! I resolved my problem.
__________________
Coocool321, answering some of your questions, since 2010.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:

Powered by liquidweb