繁体   English   中英

在WinForms C#中从图片框删除透明背景

[英]Delete transparent background from picturebox in winforms c#

我想在一个图片框上单击以获取x,y位置。我有2个图片框,一个在另一个上方。小图片框具有透明区域background(.png)。 当我单击此区域时,我想获得第二个的图片框。

这是获得x,y位置的代码:

  pictureBox2.MouseClick += (s, e) =>
        {
            if (e.Button == MouseButtons.Right)
            {
                MessageBox.Show(String.Format("Right Clicked2 at X: {0} Y: {1}", e.X, e.Y));
            }
            else if (e.Button == MouseButtons.Left)
            { MessageBox.Show(String.Format("Mouse Clicked2 at X: {0} Y: {1}", e.X, e.Y)); }
        };

        pictureBox1.MouseClick += (s, e) =>
        {
            if (e.Button == MouseButtons.Right)
            {
                MessageBox.Show(String.Format("Right Clicked at X: {0} Y: {1}", e.X, e.Y));
            }
            else if (e.Button == MouseButtons.Left)
            { MessageBox.Show(String.Format("Mouse Clicked at X: {0} Y: {1}", e.X, e.Y)); }
        };

在此处输入图片说明

这段代码显示了第二张图片(很好显示)。但是当我选择透明区域时,它给了我来自picturebox2而不是picturebox1的消息。任何想法如何将这个区域分配给picturebox1?

     var pos = this.PointToScreen(pictureBox2.Location);
        pos = pictureBox1.PointToClient(pos);
        pictureBox2.Parent = pictureBox1;
        pictureBox2.Location = pos;
        pictureBox2.BackColor = Color.Transparent;

在此处输入图片说明

只需使用一个图片框,看看您是否在该区域内。 该区域是一个以x0, 0为中心的半周期。 所以计算

squareroot(y*y + (x0-x)*(x0-x)), x,y is where you click. 

如果它比半径小,则位于内部,否则位于外部。

编辑。

创建图形路径

using System.Drawing.Drawing2D; //is needed for the graphics path

GraphicsPath gp = new GraphicsPath();

gp.AddEllipse (x, y, width, height); //x,y the upper left corner of the rect containing the ellipse

if( gp.IsVisible (e.X, e.Y) ){
    //is inside
}
else{
    //is outside
}

注意:y坐标为负

瓦尔特

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM