在C#中自由选择图片框

时间:2012-03-05 07:38:54

标签: c# winforms picturebox

我正在使用C#中的一个Windows应用程序,我想选择一个图片框控件的任何区域,就像我们可以使用工具“Free-Form Select”在ms画中一样,后者我也可以编辑这个区域。 我正在使用以下代码绘制区域:

private void picturemap_MouseMove(object sender, MouseEventArgs e)

        {
              if (StartDrawing)
              {
                    if (e.Button == MouseButtons.Left)
                    {
                          paintCurrentPosition(3, Color.Red, e.X, e.Y);
                    }
              }
        }
        private void paintCurrentPosition(int thickness, Color colorPen, int x, int y)
        {
              SolidBrush brush = new SolidBrush(colorPen);
              bmp = new Bitmap(picturemap.Image);
              gr = Graphics.FromImage(bmp);
              gr.FillRectangle(brush, x, y, thickness, thickness);

              picturemap.Image = bmp;
        }

现在我想选择我在这里绘制的区域。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

您需要在用户绘图时将自由形状的每个坐标存储到GrphicsPath对象中。在鼠标上移动时,请务必关闭路径。

稍后当用户将鼠标悬停在没有按下鼠标按钮的图片框上时,在鼠标移动时执行HitTest(e.X,e.Y)以确定当前坐标是否位于图形路径区域内。

有许多简单的算法可以非常快速地为你执行HitTest,而不会造成计算滞后。

这类似于做一个矩形。包含(x,y)测试。