鼠标位置并给出位置颜色
mouse position and give the position color
如何获取光标所在位置像素的颜色?我知道如何使用 MousePosition 获取鼠标位置,但我不知道如何获取该位置的像素颜色。我写的代码在 运行
时没有结果
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
s= pictureBox1.PointToClient(Cursor.Position);
bitmap.SetPixel(s.X / 40, s.Y / 40, Color.Red);
}
在Mouseclick
事件的参数中使用e.Location
点更容易:
Color c = ( (Bitmap)pictureBox1.Image).GetPixel(e.X, e.Y);
这假设位图确实在 PicturBox
的 Image
中,而不是绘制在 Control
..
的顶部
确保活动真的成功了!
要将点击的像素设置为红色,您将从 PB 的 Image
获取 Bitmap
并设置像素,然后将 Bitmap
移回::
Bitmap bmp = (Bitmap)pictureBox1.Image;
bmp.SetPixel(e.X, e.Y, Color.Red);
pictureBox1.Image = bmp;
也在MouseClick
活动中。
如果你想获得更大的标记,你应该使用 Graphics
方法,可能是这样的:
using (Graphics G = Graphics.FromImage(pictureBox1.Image))
{
G.DrawEllipse(Pens.Red, e.X - 3, e.Y - 3, 6, 6);
}
更新: 要结合获取和设置,您可以这样写:
Bitmap bmp = (Bitmap)pictureBox1.Image;
Color target = Color.FromArgb(255, 255, 255, 255);
Color c == bmp .GetPixel(e.X, e.Y);
if (c == target )
{
bmp.SetPixel(e.X, e.Y, Color.Red);
pictureBox1.Image = bmp;
}
else MessageBox.Show("Click only on white spots! You have hit " + c.ToString(),
"Wrong spot! ");
如何获取光标所在位置像素的颜色?我知道如何使用 MousePosition 获取鼠标位置,但我不知道如何获取该位置的像素颜色。我写的代码在 运行
时没有结果 private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
s= pictureBox1.PointToClient(Cursor.Position);
bitmap.SetPixel(s.X / 40, s.Y / 40, Color.Red);
}
在Mouseclick
事件的参数中使用e.Location
点更容易:
Color c = ( (Bitmap)pictureBox1.Image).GetPixel(e.X, e.Y);
这假设位图确实在 PicturBox
的 Image
中,而不是绘制在 Control
..
确保活动真的成功了!
要将点击的像素设置为红色,您将从 PB 的 Image
获取 Bitmap
并设置像素,然后将 Bitmap
移回::
Bitmap bmp = (Bitmap)pictureBox1.Image;
bmp.SetPixel(e.X, e.Y, Color.Red);
pictureBox1.Image = bmp;
也在MouseClick
活动中。
如果你想获得更大的标记,你应该使用 Graphics
方法,可能是这样的:
using (Graphics G = Graphics.FromImage(pictureBox1.Image))
{
G.DrawEllipse(Pens.Red, e.X - 3, e.Y - 3, 6, 6);
}
更新: 要结合获取和设置,您可以这样写:
Bitmap bmp = (Bitmap)pictureBox1.Image;
Color target = Color.FromArgb(255, 255, 255, 255);
Color c == bmp .GetPixel(e.X, e.Y);
if (c == target )
{
bmp.SetPixel(e.X, e.Y, Color.Red);
pictureBox1.Image = bmp;
}
else MessageBox.Show("Click only on white spots! You have hit " + c.ToString(),
"Wrong spot! ");