'C# getting pixels in picturebox with cursor? [closed]

How can I get pixel x and y in a picturebox using the cursor?



Solution 1:[1]

If you want to get the color of the clicked pixel:

Color pixelColor;

// add the mouse click event handler in designer mode or:
// myPicturebox.MouseClick += new MouseEventHandler(myPicturebox_MouseClick);
private void myPicturebox_MouseClick(object sender, MouseEventArgs e) {
   if (e.Button == MouseButtons.Left) 
      pixelColor = GetColorAt(e.Location);
}

private Color GetColorAt(Point point) {
   return ((Bitmap)myPicturebox.Image).GetPixel(point.X, point.Y);
}

Solution 2:[2]

The picture box has no way of getting the pixel. But the image it contains can be used to create a bitmap object that has a getpixel function. I would mention however that this is not the fastest of operations. If you need it to be quick I would look to the GDI win32 functions.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 MickyD