如何在pictureBox中制作半透明图像,仅在C#中使用可见像素捕获鼠标事件? [重复]

时间:2022-08-27 12:11:48

This question already has an answer here:

这个问题在这里已有答案:

I am drawing a square image with transparent background inside a pictureBox inside a Form with C#. I only want it to capture the mouse events with its visible part, but it happens that it captures events with its whole surface.

我正在使用C#在Form中的一个pictureBox中绘制一个带有透明背景的方形图像。我只希望它捕获具有可见部分的鼠标事件,但它碰巧捕获整个表面的事件。

1 个解决方案

#1


1  

This will work for SizeMode of Normal and AutoSize:

这适用于NormalMode of Normal和AutoSize:

if (new Bitmap(((PictureBox)sender).Image).GetPixel(e.X, e.Y).A >= 8)
    // do stuff

otherwise you would probably have to do some calculations to get the pixel location.

否则你可能不得不做一些计算来获得像素位置。

If this works for you, you should also consider storing the bitmap in a variable instead of newing it up on every mouse event.

如果这适合您,您还应该考虑将位图存储在变量中,而不是在每次鼠标事件时将其新增。

EDIT: I used 8 just as a delta value instead of zero to leave some space for almost completely transparent pixels, but of course you don't have to.

编辑:我使用8作为delta值而不是零,为几乎完全透明的像素留出一些空间,但当然你不必。

#1


1  

This will work for SizeMode of Normal and AutoSize:

这适用于NormalMode of Normal和AutoSize:

if (new Bitmap(((PictureBox)sender).Image).GetPixel(e.X, e.Y).A >= 8)
    // do stuff

otherwise you would probably have to do some calculations to get the pixel location.

否则你可能不得不做一些计算来获得像素位置。

If this works for you, you should also consider storing the bitmap in a variable instead of newing it up on every mouse event.

如果这适合您,您还应该考虑将位图存储在变量中,而不是在每次鼠标事件时将其新增。

EDIT: I used 8 just as a delta value instead of zero to leave some space for almost completely transparent pixels, but of course you don't have to.

编辑:我使用8作为delta值而不是零,为几乎完全透明的像素留出一些空间,但当然你不必。