PictureBox - Handle Click Event on Non-Transparent Area of Image

后端 未结 2 1726
难免孤独
难免孤独 2021-01-14 19:59

I have to make a windows form in C# where two PictureBox are overlapping. TopPictureBox is containing a transparent png picture. By default TopPictureBox can be clicked by c

2条回答
  •  萌比男神i
    2021-01-14 20:21

    One way is to check whether the colour of the pixel where the user clicked, is the same as the background colour of the form. If yes, then the user clicked on a transparent area.

    (Note : As Reza mentioned, this code can be used only when there are no overlapping PictureBoxes, i.e. only when the transparent area of the image is of the same colour as the Form's background)

    Color pixelColour;
    
    private void myPicturebox_MouseClick(object sender, MouseEventArgs e)
    {
       if (e.Button == MouseButtons.Left) 
       {
         pixelColour = ((Bitmap)myPicturebox.Image).GetPixel(point.X, point.Y);
         if (this.BackColor == pixelColour)
         {
            // User clicked on transparent area
         }
         else
         {
            // User clicked on image
         }
       }
    }
    

提交回复
热议问题