更容易使用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