当我以绿色绘制一条线时,如何自动绘制每10个像素的红点?

时间:2013-12-07 03:15:07

标签: c# winforms

在form1的顶部,我做了:

GraphicsPath gp = new GraphicsPath();
GraphicsPath redgp = new GraphicsPath();
Point p;

然后在pictureBox1移动事件我做了:

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            label4.Visible = true;
            label4.Text = String.Format("X: {0}; Y: {1}", e.X, e.Y);
            if (panning)
            {
                movingPoint = new Point(e.Location.X - startingPoint.X,
                                        e.Location.Y - startingPoint.Y);
                pictureBox1.Invalidate();
            }
            if (checkBox2.Checked && e.Button == MouseButtons.Left)
            {
                gp.AddLine(e.X * xFactor, e.Y * yFactor, e.X * xFactor, e.Y * yFactor);//e.Location, e.Location);
                redgp.AddEllipse((e.X) * xFactor, (e.Y) * yFactor, 3f, 3f);
                p = e.Location;
                pictureBox2.Invalidate();
            }

        }

然后在pictureBox2绘制事件:

private void pictureBox2_Paint(object sender, PaintEventArgs e)
        {
            if (!checkBox1.Checked)
            {
                Pen p;
                p = new Pen(Brushes.Green);
                foreach (PointF pt in points)
                {
                    e.Graphics.FillEllipse(Brushes.Red, pt.X, pt.Y, 3f, 3f);
                }

                for (int i = 0; i < points.Count - 1; i++)
                {
                    if (points.Count > 1)
                    {
                        e.Graphics.DrawLine(p, points[i].X, points[i].Y, points[i + 1].X, points[i + 1].Y);
                    }
                }

                if (checkBox2.Checked)
                {
                    using (Pen pp = new Pen(Color.Green, 2f))
                    {
                        pp.StartCap = pp.EndCap = LineCap.Round;
                        pp.LineJoin = LineJoin.Round;
                        e.Graphics.DrawPath(pp, gp);
                    }
                    using (Pen pp = new Pen(Color.Red, 2f))
                    {
                        pp.StartCap = pp.EndCap = LineCap.Round;
                        pp.LineJoin = LineJoin.Round;
                        e.Graphics.DrawPath(pp, redgp);
                    }
                }
            }
        }

选中复选框2后,我点击鼠标左键,然后在picturebox1上移动鼠标,它将在pictureBox2中画一条线。

pictureBox2中的行为绿色。 我希望当我在picturebox1上移动鼠标时,它会在pictureBox2上以绿色绘制线条,并且在picutreBox2中自动每10个像素空间,它将在绿线上自动绘制一个点。

所以我将此代码添加到pictureBox1移动事件:

redgp.AddEllipse((e.X) * xFactor, (e.Y) * yFactor, 3f, 3f);

在picturebox2绘制事件:

using (Pen pp = new Pen(Color.Red, 2f))
                    {
                        pp.StartCap = pp.EndCap = LineCap.Round;
                        pp.LineJoin = LineJoin.Round;
                        e.Graphics.DrawPath(pp, redgp);
                    }

但它的作用是在绿线上或附近的pictureBox2中绘制更宽的红线。不是我想要的。

我该如何解决?

2 个答案:

答案 0 :(得分:1)

通过这种方式解决了它。 在form1的顶部添加了一个变量计数器。

然后picturebox1鼠标移动事件:

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            label4.Visible = true;
            label4.Text = String.Format("X: {0}; Y: {1}", e.X, e.Y);
            if (panning)
            {
                movingPoint = new Point(e.Location.X - startingPoint.X,
                                        e.Location.Y - startingPoint.Y);
                pictureBox1.Invalidate();
            }
            if (checkBox2.Checked && e.Button == MouseButtons.Left)
            {
                gp.AddLine(e.X * xFactor, e.Y * yFactor, e.X * xFactor, e.Y * yFactor);
                pixelscounter += 1;
                if (pixelscounter == 10)
                {
                    redgp.AddEllipse((e.X) * xFactor, (e.Y) * yFactor, 3f, 3f);
                    pixelscounter = 0;
                }
                p = e.Location;
                pictureBox2.Invalidate();
            }

        }

我让计数器上升1。 如果它的10创造一个红点。并将计数器重新设置为0。

答案 1 :(得分:0)

该行是更宽的(厚),因为您已使用PEN和2f。 它只显示红色,因为您在同一点上绘制两条相同厚度的线,所以这里发生的是您只能看到您绘制的最后一条线。在你的情况下它是RED。

如果你想在这里出现两行;

用户绿色钢笔原样:                 使用(笔pp =新笔(Color.Green,2f))

但要使红笔更薄:                 使用(笔pp =新笔(Color.Red,1f))

因此,它将在绿线上绘制红线。

希望它有帮助!