how do i get the color of a specific point in a picture box?
Printable View
how do i get the color of a specific point in a picture box?
perhaps, get the image object and convert it bitmap. Then u can use GetPixel and SetPixel function
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?
IMO in that way you could do some maths instad of using gdi
no, maths won't cut it. do you know of a way toa ctually get the pixel color?
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?:D
VB Code:
'First we get the picturebox's picture Dim img As New Bitmap(PictureBox1.Image) 'To get the color of a pixel, use this. 'Replace X and Y with the point of the pixel you want to get. img.GetPixel(x, y) 'To set the color of a pixel, do this. 'Again, replace X and Y with the pixel you want to set, and the color to set it to. img.SetPixel(x, y, Color)
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...
VB Code:
Dim x As Bitmap = PictureBox1.Image 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
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.
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:
Dim bmp As New Bitmap(PictureBox1.Size.Width, PictureBox1.Size.Height) Dim grph As Graphics grph = Graphics.FromImage(bmp) grph.DrawLine(New Pen(Color.Red), 0, 0, PictureBox1.Size.Width, PictureBox1.Size.Height) PictureBox1.Image = bmp MessageBox.Show("Color at point 0,0 is " & bmp.GetPixel(0, 0).ToString) MessageBox.Show("Color at point 10,10 is " & bmp.GetPixel(10, 10).ToString)
thanks a lot eveybody. that last post made everything work.
this issue is now resolved.