Results 1 to 12 of 12

Thread: get Pixel color

  1. #1

    Thread Starter
    Registered User
    Join Date
    Jul 2001
    Posts
    283

    get Pixel color

    how do i get the color of a specific point in a picture box?
    Last edited by marvinklein; Oct 21st, 2003 at 07:32 PM.

  2. #2
    Member
    Join Date
    May 2003
    Posts
    58
    perhaps, get the image object and convert it bitmap. Then u can use GetPixel and SetPixel function

  3. #3

    Thread Starter
    Registered User
    Join Date
    Jul 2001
    Posts
    283
    that works if it is an actual image, but what if i have been drawing lines on a form using the pen object and then i want to find if a point on the for has a line going through it?

  4. #4
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    IMO in that way you could do some maths instad of using gdi
    \m/\m/

  5. #5

    Thread Starter
    Registered User
    Join Date
    Jul 2001
    Posts
    283
    no, maths won't cut it. do you know of a way toa ctually get the pixel color?

  6. #6
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    if you want to get the pixel color then you better draw on the actual image in the picturebox. the you can easily do what Volvere said.

    want help with that?
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  7. #7
    Lively Member
    Join Date
    Aug 2003
    Location
    When?!?!
    Posts
    108

    Your problem is solved

    VB Code:
    1. 'First we get the picturebox's picture
    2.         Dim img As New Bitmap(PictureBox1.Image)
    3.  
    4.         'To get the color of a pixel, use this.
    5.         'Replace X and Y with the point of the pixel you want to get.
    6.         img.GetPixel(x, y)
    7.  
    8.         'To set the color of a pixel, do this.
    9.         'Again, replace X and Y with the pixel you want to set, and the color to set it to.
    10.         img.SetPixel(x, y, Color)
    DannyJoumaa
    Advanced VB6 Programmer
    Intermediate-Advanced VB .NET Programmer
    Intermediate C# Programmer
    Intermediate Win32 Developer
    Beginner Mac OS X Developer
    Contact: [email protected]

    Favorite Sayings:
    "Every time you open your mouth, you prove your an idiot."
    "God is a programmer. Satan is a bug. Life is debugging."

  8. #8

    Thread Starter
    Registered User
    Join Date
    Jul 2001
    Posts
    283
    ok, this is the code i am using to draw the line:

    myPictureBox.DrawLine(New Pen(Color.Black), x1 , y1, x2, y2)

    now this is what i want to do:
    Dim my Color as Color = myPictureBox.GetPixel(x, y)
    'EXCEPT GetPixel DOSN'T EXIST!

    the problem with drawing onto a bitmap contained in the picturebox is that functions like DrawLine don't exist. All you have then is GetPixel and SetPixel!

    So, can somebody please try and come up with a function that returns the color of a pixel in an object that is not a bitmap? (Like you can in 6.0 using the Point function)

    thanks...

  9. #9
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    VB Code:
    1. Dim x As Bitmap = PictureBox1.Image
    2.         MessageBox.Show(x.GetPixel(10, 10).ToString)

    You can still draw on the picturebox itself. And reset your bitmap to the image when you need to get a pixel. IF you need to do this 1000 times a second, this isn't a good solution obviously.

    In which case, this article may help you (in C#):
    http://msdn.microsoft.com/library/de...rp11152001.asp
    Last edited by nemaroller; Oct 21st, 2003 at 08:45 PM.

  10. #10

    Thread Starter
    Registered User
    Join Date
    Jul 2001
    Posts
    283
    that article is perfect for c#, but i'm using vb.net and there are a lot of useful functions that i simply don't have. maybe i should somehow use the image gdi more directly. i've never used gdi, so i don't really know what i'm talking about, so if you know where i can read something about the image gdi that would be really helpful i think.

  11. #11
    Addicted Member Sheppe's Avatar
    Join Date
    Sep 2002
    Location
    Kelowna, BC
    Posts
    245
    To draw lines, etc., you need to use a Graphics object. The following example shows you how to draw a line using the Graphics object, and how to use a Bitmap object to call GetPixel. The message boxes are only used to indicate that GetPixel does indeed work. To use this code, place it in the Load event of a form. The form must have a picturebox on it named PictureBox1. This is a crude example, but it's good as a demo:

    VB Code:
    1. Dim bmp As New Bitmap(PictureBox1.Size.Width, PictureBox1.Size.Height)
    2.         Dim grph As Graphics
    3.         grph = Graphics.FromImage(bmp)
    4.  
    5.         grph.DrawLine(New Pen(Color.Red), 0, 0, PictureBox1.Size.Width, PictureBox1.Size.Height)
    6.  
    7.         PictureBox1.Image = bmp
    8.  
    9.         MessageBox.Show("Color at point 0,0 is " & bmp.GetPixel(0, 0).ToString)
    10.  
    11.         MessageBox.Show("Color at point 10,10 is " & bmp.GetPixel(10, 10).ToString)
    [vbcode]
    On Error Goto Hell
    [/vbcode]
    Sheppe Pharis, MCSD
    Check out http://www.vb-faq.com
    Click here for access to the free Code-Express source code and component sharing network for VB6
    Want a better way to skin your .NET applications? Click here!

  12. #12

    Thread Starter
    Registered User
    Join Date
    Jul 2001
    Posts
    283
    thanks a lot eveybody. that last post made everything work.
    this issue is now resolved.

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