C# 如何检测面板上绘制的不规则图像的点击

C# 如何检测面板上绘制的不规则图像的点击,c#,winforms,graphics,C#,Winforms,Graphics,事实上,我在一个面板上画了一个图像。这个图像是灌溉形状的。对于鼠标点击,我想显示一条消息,无论是在图像上还是在面板上进行点击。您可以使用及其来实现您的目标,但可能存在更好的解决方案。但是,在找到它们之前,请根据面板上绘制的在椭圆中捕获鼠标点的示例尝试以下方法 声明字段: private readonly GraphicsPath _graphicsPath; private readonly Region _region; private readonly Graphics _panelGrap

事实上,我在一个面板上画了一个图像。这个图像是灌溉形状的。对于鼠标点击,我想显示一条消息,无论是在图像上还是在面板上进行点击。

您可以使用及其来实现您的目标,但可能存在更好的解决方案。但是,在找到它们之前,请根据面板上绘制的在椭圆中捕获鼠标点的示例尝试以下方法

声明字段:

private readonly GraphicsPath _graphicsPath;
private readonly Region _region;
private readonly Graphics _panelGraphics;
private readonly Bitmap _image;
private readonly Graphics _panelGraphics;
初始化以上字段:

_graphicsPath = new GraphicsPath();
_graphicsPath.AddEllipse(100, 100, 100, 100); // Path that contains simple ellipse. You can add here more shapes and combine them in similar manner as you draw them.
_region = new Region(_graphicsPath); // Create region, that contains Intersect method.
_panelGraphics = panel1.CreateGraphics(); // Graphics for the panel.
面板绘制事件句柄:

private void panel_Paint(object sender, PaintEventArgs e)
{
    _panelGraphics.FillEllipse(Brushes.Red, 100, 100, 100, 100); // Draw your structure.
}
private void panel_MouseDown(object sender, MouseEventArgs e)
{
    var cursorRectangle = new Rectangle(e.Location, new Size(1, 1)); // We need rectangle for Intersect method.
    var copyOfRegion = _region.Clone(); // Don't break original region.
    copyOfRegion.Intersect(cursorRectangle); // Check whether cursor is in complex shape.

    Debug.WriteLine(copyOfRegion.IsEmpty(_panelGraphics) ? "Miss" : "In");
}
private void panel_Paint(object sender, PaintEventArgs e)
{
    _panelGraphics.DrawImageUnscaled(_image, 0, 0);
}
private void panel_MouseDown(object sender, MouseEventArgs e)
{
    Debug.WriteLine(_image.GetPixel(e.Location.X, e.Location.Y).A != 0 ? "In" : "Miss");
}
面板鼠标按下事件句柄:

private void panel_Paint(object sender, PaintEventArgs e)
{
    _panelGraphics.FillEllipse(Brushes.Red, 100, 100, 100, 100); // Draw your structure.
}
private void panel_MouseDown(object sender, MouseEventArgs e)
{
    var cursorRectangle = new Rectangle(e.Location, new Size(1, 1)); // We need rectangle for Intersect method.
    var copyOfRegion = _region.Clone(); // Don't break original region.
    copyOfRegion.Intersect(cursorRectangle); // Check whether cursor is in complex shape.

    Debug.WriteLine(copyOfRegion.IsEmpty(_panelGraphics) ? "Miss" : "In");
}
private void panel_Paint(object sender, PaintEventArgs e)
{
    _panelGraphics.DrawImageUnscaled(_image, 0, 0);
}
private void panel_MouseDown(object sender, MouseEventArgs e)
{
    Debug.WriteLine(_image.GetPixel(e.Location.X, e.Location.Y).A != 0 ? "In" : "Miss");
}

以下是仅在面板上绘制图像的场景:

声明字段:

private readonly GraphicsPath _graphicsPath;
private readonly Region _region;
private readonly Graphics _panelGraphics;
private readonly Bitmap _image;
private readonly Graphics _panelGraphics;
初始化字段:

_image = new Bitmap(100, 100); // Image and panel have same size.
var imageGraphics = Graphics.FromImage(_image);
imageGraphics.FillEllipse(Brushes.Red, 10, 10, 50, 50); // Some irregular image.

panel2.Size = new Size(100, 100);
panel2.BackColor = Color.Transparent; // Panel's background color is transparent.
_panelGraphics = panel2.CreateGraphics();
面板绘制事件句柄:

private void panel_Paint(object sender, PaintEventArgs e)
{
    _panelGraphics.FillEllipse(Brushes.Red, 100, 100, 100, 100); // Draw your structure.
}
private void panel_MouseDown(object sender, MouseEventArgs e)
{
    var cursorRectangle = new Rectangle(e.Location, new Size(1, 1)); // We need rectangle for Intersect method.
    var copyOfRegion = _region.Clone(); // Don't break original region.
    copyOfRegion.Intersect(cursorRectangle); // Check whether cursor is in complex shape.

    Debug.WriteLine(copyOfRegion.IsEmpty(_panelGraphics) ? "Miss" : "In");
}
private void panel_Paint(object sender, PaintEventArgs e)
{
    _panelGraphics.DrawImageUnscaled(_image, 0, 0);
}
private void panel_MouseDown(object sender, MouseEventArgs e)
{
    Debug.WriteLine(_image.GetPixel(e.Location.X, e.Location.Y).A != 0 ? "In" : "Miss");
}
面板鼠标按下事件句柄:

private void panel_Paint(object sender, PaintEventArgs e)
{
    _panelGraphics.FillEllipse(Brushes.Red, 100, 100, 100, 100); // Draw your structure.
}
private void panel_MouseDown(object sender, MouseEventArgs e)
{
    var cursorRectangle = new Rectangle(e.Location, new Size(1, 1)); // We need rectangle for Intersect method.
    var copyOfRegion = _region.Clone(); // Don't break original region.
    copyOfRegion.Intersect(cursorRectangle); // Check whether cursor is in complex shape.

    Debug.WriteLine(copyOfRegion.IsEmpty(_panelGraphics) ? "Miss" : "In");
}
private void panel_Paint(object sender, PaintEventArgs e)
{
    _panelGraphics.DrawImageUnscaled(_image, 0, 0);
}
private void panel_MouseDown(object sender, MouseEventArgs e)
{
    Debug.WriteLine(_image.GetPixel(e.Location.X, e.Location.Y).A != 0 ? "In" : "Miss");
}

很抱歉,这不起作用,我正在用缩放级别在屏幕上绘制图像,图像不会覆盖整个面板,因此如何检测我的鼠标单击是否在图像上或面板中的剩余区域完成,请帮助我。@user2095270:如果这只是您正在绘制的文件中的图像(不是您自己的草稿,绘图)然后使用我第二个答案中的场景。这个答案在我的机器上进行了检查,它可以工作,但只有当你正在绘制结构时,你才能将其添加到GraphicsPath。上述场景要求,面板的背景色设置为透明,并且图像上不属于形状的区域也是透明的。