使用图片框。您可以在设计时放置它们,并且它们具有鼠标悬停、单击等事件。在特定的鼠标操作上更改它的图像。在此示例中,我只是将其设置为 null,但您明白了:
Image picImage = null; // to store original image
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
pictureBox1.Image = picImage; // rest original image
}
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
picImage = pictureBox1.Image; // remember the original image
pictureBox1.Image = null; // change the current image
}
更新:
好的,要进行像素完美选择,只需在同一位置使用两个 PictureBox,一个显示未选择的图像(pictureBox1),一个显示选定的图像(pictureBox2)。使pictureBox1 不可见。然后,当您的鼠标悬停在图像的透明/非透明区域上时,使用此代码显示/隐藏它们:
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
Bitmap b = new Bitmap(pictureBox1.Image);
Color color = b.GetPixel(e.X, e.Y);
if (color.A != 0)
{
pictureBox1.Visible = false;
pictureBox2.Visible = true;
}
}
private void pictureBox2_MouseMove(object sender, MouseEventArgs e)
{
Bitmap b = new Bitmap(pictureBox1.Image);
Color color = b.GetPixel(e.X, e.Y);
if (color.A == 0)
{
pictureBox2.Visible = false;
pictureBox1.Visible = true;
}
}