如何在WinForms中的两个图像之间绘制箭头?

发布于 2024-10-05 19:16:33 字数 88 浏览 3 评论 0原文

我必须这样做:在画布上的两个图像之间绘制一个箭头,因此当我单击带有箭头的按钮时,我单击一个图像以将箭头粘贴到其上,然后绘制箭头只要我需要并将其粘贴到第二张图片中。

I have to do this: to draw an arrow between two images from my canvas, so when I click the button with the arrow on it and I put click on one image to paste the arrow on it and then to draw the arrow as long as i need and to paste it to the second image.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

疯了 2024-10-12 19:16:33

要用两点绘制圆弧,您需要一个预定义的角度。我想你已经弄清楚了那部分。

为此,您需要绘制两条弧线,每个图像中各一条。弧线将比每个图像都大,但在第一个图像中,您可以将其剪裁在弧线退出图像并朝向第二个点的位置。

在第二个图像中,您需要将圆弧偏移两个图像原点之间的 x 和 y 距离。然后在第二张图像中从第一个点到第二个点绘制圆弧,并剪掉图像外部的部分。

如果您需要橡皮筋弧,则必须在鼠标移动时擦除并重新绘制它。如果您想在图像之间的表单上的空间上绘图,可以使用适当的偏移量来实现。

To draw an arc with two points, you'll need a predefined angle. I assume you have that part figured out.

To do this, you need to draw two arcs, one in each image. The arc will be bigger than each image, but in the first image you can just clip it where the arc exits the image going toward the second point.

In the second image, you need to offset the arc by the x and y distance between the origins of the two images. Then draw the arc in the second image from the first point to the second point and clip that part that is outside the image.

If you need a rubber band arc, you'll have to erase and redraw it whenever the mouse moves. If you want to draw on the space on the form between the images, you can do that using the proper offset.

骄傲 2024-10-12 19:16:33

你有两个点,所以你可以画一条线。试试这个:

public class Shape
{
    public float X { get; set; }
    public float Y { get; set; }
    public Image Image { get; set; }
}

public class Line
{
    public Shape A { get; set; }
    public Shape B { get; set; }
}

和代码:

private string _currentTool;
private readonly List<Shape> _shapes;
private readonly List<Line> _lines;
private Line _currentLine;

private void Button1Click(object sender, EventArgs e)
{
    _currentTool = "img";
}

private void Button2Click(object sender, EventArgs e)
{
    _currentTool = "line";
}

private void PictureBox1MouseDown(object sender, MouseEventArgs e)
{
    switch (_currentTool)
    {
        case "img":
            _shapes.Add(new Shape { Image = button1.Image, X = e.X, Y = e.Y });
            pictureBox1.Invalidate();
            break;
        case "line":
                var selectedShapes = _shapes.Where(shape => (shape.X - 10 < e.X) && (e.X < shape.X + 10) &&
                                                           (shape.Y - 10 < e.Y) && (e.Y < shape.Y + 10));
                if (selectedShapes.Count() > 0)
                {
                    var selectedShape = selectedShapes.First();
                    _currentLine = new Line {A = selectedShape};
                    pictureBox1.Invalidate();
                }
            break;
    }
}

private void PictureBox1MouseUp(object sender, MouseEventArgs e)
{
    switch (_currentTool)
    {
        case "line":
                var selectedShapes = _shapes.Where(shape => (shape.X - 10 < e.X) && (e.X < shape.X + 10) &&
                                                           (shape.Y - 10 < e.Y) && (e.Y < shape.Y + 10));
                if (selectedShapes.Count() > 0)
                {
                    var selectedShape = selectedShapes.First();
                    _currentLine.B = selectedShape;
                    _lines.Add(_currentTool);
                    pictureBox1.Invalidate();
                }
            break;
    }
}

private void PictureBox1Paint(object sender, PaintEventArgs e)
{
    foreach (var shape in _shapes)
    {
        e.Graphics.DrawImage(shape.Image, shape.X, shape.Y);
    }
    foreach (var line in _lines)
    {
        e.Graphics.DrawLine(new Pen(Color.Black), line.A.X, line.A.Y, line.B.X, line.B.Y);
    }
}

You have two point, so you can draw a line. Try this:

public class Shape
{
    public float X { get; set; }
    public float Y { get; set; }
    public Image Image { get; set; }
}

public class Line
{
    public Shape A { get; set; }
    public Shape B { get; set; }
}

and the code:

private string _currentTool;
private readonly List<Shape> _shapes;
private readonly List<Line> _lines;
private Line _currentLine;

private void Button1Click(object sender, EventArgs e)
{
    _currentTool = "img";
}

private void Button2Click(object sender, EventArgs e)
{
    _currentTool = "line";
}

private void PictureBox1MouseDown(object sender, MouseEventArgs e)
{
    switch (_currentTool)
    {
        case "img":
            _shapes.Add(new Shape { Image = button1.Image, X = e.X, Y = e.Y });
            pictureBox1.Invalidate();
            break;
        case "line":
                var selectedShapes = _shapes.Where(shape => (shape.X - 10 < e.X) && (e.X < shape.X + 10) &&
                                                           (shape.Y - 10 < e.Y) && (e.Y < shape.Y + 10));
                if (selectedShapes.Count() > 0)
                {
                    var selectedShape = selectedShapes.First();
                    _currentLine = new Line {A = selectedShape};
                    pictureBox1.Invalidate();
                }
            break;
    }
}

private void PictureBox1MouseUp(object sender, MouseEventArgs e)
{
    switch (_currentTool)
    {
        case "line":
                var selectedShapes = _shapes.Where(shape => (shape.X - 10 < e.X) && (e.X < shape.X + 10) &&
                                                           (shape.Y - 10 < e.Y) && (e.Y < shape.Y + 10));
                if (selectedShapes.Count() > 0)
                {
                    var selectedShape = selectedShapes.First();
                    _currentLine.B = selectedShape;
                    _lines.Add(_currentTool);
                    pictureBox1.Invalidate();
                }
            break;
    }
}

private void PictureBox1Paint(object sender, PaintEventArgs e)
{
    foreach (var shape in _shapes)
    {
        e.Graphics.DrawImage(shape.Image, shape.X, shape.Y);
    }
    foreach (var line in _lines)
    {
        e.Graphics.DrawLine(new Pen(Color.Black), line.A.X, line.A.Y, line.B.X, line.B.Y);
    }
}
等往事风中吹 2024-10-12 19:16:33
public class Shape
{
public float X { get; set; }
public float Y { get; set; }
public Image Image { get; set; }

public bool Test_int(int x, int y)
    {
        if (((x <= this.x + (float)image.Width) && (x >= this.x)) && ((y <= this.y + (float)image.Height) && (y >= this.y)))
            return true;
        else
            return false;
    }
}



public class Line
{
    public Shape A { get; set; }
    public Shape B { get; set; }
}

和代码

private string currentTool;
private readonly List<Shape> shapes;
private readonly List<Line> lines;
private Line currentLine;

private void Button1Click(object sender, EventArgs e)
{
    currentTool = "img";
}

private void Button2Click(object sender, EventArgs e)
{
    currentTool = "line";
}


private void PictureBox1MouseDown(object sender, MouseEventArgs e)
{

    switch (currentTool)
    {
        case "img":
            shapes.Add(new Shape { Image = button1.Image, X = e.X, Y = e.Y });
            pictureBox1.Invalidate();
            break;
        case "line":
            foreach (Shape shape1 in shapes)
                {

                    if (shape1.Test_int(e.X, e.Y))
                    {

                        currentLine = new Line { A = shape1 };

                    }

                }
                drawArea1.Invalidate();
        break;
    }
}

private void PictureBox1MouseUp(object sender, MouseEventArgs e)
{

    switch (currentTool)
    {
        case "line":
            foreach (Shape shape1 in shapes)
            {

                if (shape1.Test_int(e.X, e.Y))
                {


                    currentLine.B = shape1;

                    }

                }
                lines.Add(currentLine);
                drawArea1.Invalidate();

            break;
    }
}

private void PictureBox1Paint(object sender, PaintEventArgs e)
{

    foreach (var shape in shapes)
    {
        e.Graphics.DrawImage(shape.Image, shape.X, shape.Y);
    }
    foreach (var line in lines)
    {
        Pen p = new Pen(Color.Gray, 1);
        Pen p2 = new Pen(Color.Black, 5);

        e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

        p2.StartCap = System.Drawing.Drawing2D.LineCap.Round;
        p2.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;

        float x1 = line.A.X+line.A.Image.Width ;
        float y1 = line.A.Y+line.A.Image.Height/2;
        float x2 = line.B.X;
        float y2 = line.B.Y+line.B.Image.Height/2;
        double angle = Math.Atan2(y2 - y1, x2 - x1);

        e.Graphics.DrawLine(p, x1, y1, x2, y2);
        e.Graphics.DrawLine(p2, x2, y2, x2 + (float)(Math.Cos(angle)), y2 + (float)(Math.Sin(angle)));

    }
}
public class Shape
{
public float X { get; set; }
public float Y { get; set; }
public Image Image { get; set; }

public bool Test_int(int x, int y)
    {
        if (((x <= this.x + (float)image.Width) && (x >= this.x)) && ((y <= this.y + (float)image.Height) && (y >= this.y)))
            return true;
        else
            return false;
    }
}



public class Line
{
    public Shape A { get; set; }
    public Shape B { get; set; }
}

and the code

private string currentTool;
private readonly List<Shape> shapes;
private readonly List<Line> lines;
private Line currentLine;

private void Button1Click(object sender, EventArgs e)
{
    currentTool = "img";
}

private void Button2Click(object sender, EventArgs e)
{
    currentTool = "line";
}


private void PictureBox1MouseDown(object sender, MouseEventArgs e)
{

    switch (currentTool)
    {
        case "img":
            shapes.Add(new Shape { Image = button1.Image, X = e.X, Y = e.Y });
            pictureBox1.Invalidate();
            break;
        case "line":
            foreach (Shape shape1 in shapes)
                {

                    if (shape1.Test_int(e.X, e.Y))
                    {

                        currentLine = new Line { A = shape1 };

                    }

                }
                drawArea1.Invalidate();
        break;
    }
}

private void PictureBox1MouseUp(object sender, MouseEventArgs e)
{

    switch (currentTool)
    {
        case "line":
            foreach (Shape shape1 in shapes)
            {

                if (shape1.Test_int(e.X, e.Y))
                {


                    currentLine.B = shape1;

                    }

                }
                lines.Add(currentLine);
                drawArea1.Invalidate();

            break;
    }
}

private void PictureBox1Paint(object sender, PaintEventArgs e)
{

    foreach (var shape in shapes)
    {
        e.Graphics.DrawImage(shape.Image, shape.X, shape.Y);
    }
    foreach (var line in lines)
    {
        Pen p = new Pen(Color.Gray, 1);
        Pen p2 = new Pen(Color.Black, 5);

        e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

        p2.StartCap = System.Drawing.Drawing2D.LineCap.Round;
        p2.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;

        float x1 = line.A.X+line.A.Image.Width ;
        float y1 = line.A.Y+line.A.Image.Height/2;
        float x2 = line.B.X;
        float y2 = line.B.Y+line.B.Image.Height/2;
        double angle = Math.Atan2(y2 - y1, x2 - x1);

        e.Graphics.DrawLine(p, x1, y1, x2, y2);
        e.Graphics.DrawLine(p2, x2, y2, x2 + (float)(Math.Cos(angle)), y2 + (float)(Math.Sin(angle)));

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文