更容易使用e.Location
点在Mouseclick
事件的参数时:
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! ");
来源
2015-02-07 15:25:52
TaW
好当然不是,你没有写代码。使用MouseMove事件和bitmap.GetPixel() – 2015-02-07 15:24:25