我必须这样做:在画布上的两幅图像之间绘制一个箭头,所以当我点击带有箭头的按钮时,点击一个图像将其上的箭头粘贴,然后将箭头绘制为只要我需要并粘贴到第二张图片。如何在WinForms中的两个图像之间绘制箭头?
1
A
回答
1
要用两点绘制弧线,您需要一个预定义的角度。我假设你已经找到了这个部分。
要做到这一点,你需要绘制两个弧,每个图像一个。弧线将比每个图像都大,但在第一张图像中,您可以将其剪切到弧线离开图像前往第二个点的位置。
在第二张图片中,您需要通过两张图像的原点之间的x和y距离来抵消圆弧。然后从第一个点到第二个点在第二个图像中绘制弧,并剪切该图像外部的部分。
如果你需要一个橡皮筋弧,每当鼠标移动时你都必须擦除并重绘它。如果要在图像之间的窗体上绘制空间,可以使用适当的偏移量来实现。
1
你有两点,所以你可以画一条线。试试这个:
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);
}
}
0
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)));
}
}
相关问题
- 1. 如何在JavaFX中的两点之间绘制箭头?
- 2. 在两个列表视图之间绘制箭头
- 3. 在两个对象之间的HTML5画布上绘制箭头
- 4. 在两个弹性项目之间绘制箭头
- 5. 在列表之间绘制箭头
- 6. 绘制ASP.NET中两点之间的箭头
- 7. 两个元素之间的箭头
- 8. 如何在R中绘制3D箭头?
- 9. Android - 如何正确绘制一个带箭头的箭头?
- 10. 使用画布,SVG和js绘制两个div之间的箭头或线条
- 11. 在opengl中绘制一个箭头ES
- 12. 在Pygame中绘制一个箭头
- 13. 直接在matplotlib图上绘制箭头
- 14. 如何绘制带边框的箭头?
- 15. 如何使用CSS在彼此之上堆叠两个箭头图像(upvote/downvote)?
- 16. 用d3问题绘制带有图像和箭头的图形
- 17. 如何在多个图像之间绘制线条
- 18. 如何在iPhone中的两个位置之间绘制路线?
- 19. 如何使用画布在Android绘图视图中绘制2D箭头(看起来像Skitch应用程序绘制箭头)
- 20. 如何在我的视图上绘制箭头?
- 21. 在Java中绘制两个图标之间的延迟
- 22. 如何在c3 unity3d中从fps控制器中绘制箭头#
- 23. 如何绘制箭头JavaFX? (窗格)
- 24. 如何用java2d绘制箭头?
- 25. 使用opencv在图像上绘制双面箭头python
- 26. 时间飞得像WinForms中的箭头演示
- 27. 如何在listiview中给两个图像之间的空间?
- 28. ToolStripComboBoxItem箭头没有绘制所需的颜色(VS.Net 2010,WinForms)
- 29. 在vC++中绘制箭头mfc
- 30. 如何使用Highcharts在线图上绘制箭头?
Mmh的,它不是那么清楚......你可以给一个小例子视觉你需要什么? – digEmAll 2010-12-05 21:36:29
您需要2点以上才能绘制弧线,您还需要确定尺寸,开始和扫掠角度? – Doggett 2010-12-05 21:52:44
我很抱歉,我不明白你在做什么。你能清楚地解释a)屏幕上的东西,包括盒子,按钮,圆弧等等,以及b)你想要做什么(用户)在鼠标点击方面和可能的点击 - 拖动操作。谢谢。 – ja72 2010-12-05 21:57:22