C#用光标获取图片框中的像素?

xFi*_*eTR 4 c# bitmap cursor picturebox winforms

如何使用光标获取像素x和图片y框?

Oma*_*mar 15

如果要获取所点击像素的颜色:

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);
}
Run Code Online (Sandbox Code Playgroud)

  • 请记住:e.location是pictureBox中的位置,而实际图像已调整大小以适合该框,但仍然具有其原始大小。当要求此图像中的pixelvalue时,您将从未缩放的图像中获取该值。这(也许)不是您可能期望的颜色值! (2认同)

rer*_*run 5

图片框无法获取像素.但它包含的图像可用于创建具有getpixel函数的位图对象.不过我会提到这不是最快的操作.如果你需要快速,我会看看GID win32功能.