C# 在列表中绘制对象的所有线条,然后添加、删除新线?
我将一些线条对象保存在列表中,并尝试在图片框上绘制它们一次,但每次它只在图片框的中间绘制一条线条,是否有解决此问题的方法。 先谢谢你C# 在列表中绘制对象的所有线条,然后添加、删除新线?,c#,image-processing,C#,Image Processing,我将一些线条对象保存在列表中,并尝试在图片框上绘制它们一次,但每次它只在图片框的中间绘制一条线条,是否有解决此问题的方法。 先谢谢你 public class Lines { public System.Drawing.Point startPoint = new System.Drawing.Point(); public System.Drawing.Point endPoint = new System.Drawing.Point();
public class Lines
{
public System.Drawing.Point startPoint = new System.Drawing.Point();
public System.Drawing.Point endPoint = new System.Drawing.Point();
}
Lines b = new Lines();
List<Lines> alllines = new List<Lines>();
//------------inside button click i wrote the following code----------
b.startPoint.X = rectlist[i].X;
b.startPoint.Y = (rectlist[i].Y + rectlist[i].Bottom) / 2;
b.endPoint.X = rectlist[i].Right;
b.endPoint.Y = (rectlist[i].Top + rectlist[i].Bottom) / 2;
alllines.Add(b);
this.OrignalimgPIcBX.Invalidate();
有什么问题
中的行列表现在已更正 但是要知道线对象没有绘制在正确的位置 我把图片框的大小模式改为拉伸图像,是不是有什么变化 这是绘画活动
private void OrignalimgPIcBX_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
using (var pen = new Pen(Color.Black, 2))
{
foreach (var lines in alllines)
{
g.DrawLine(pen, lines.startPoint, lines.endPoint);
}
}
}
答案只有三种可能的解释
- 你的台词都一样
- 除了一条线之外,所有的线都在可查看区域之外
- 或者你正在吃掉一个例外
for (int i = 0; i < rectlist.Count; i++)
{
var b = new Lines(); // <-- you need to do this
b.startPoint.X = rectlist[i].X;
b.startPoint.Y = (rectlist[i].Y + rectlist[i].Bottom) / 2;
b.endPoint.X = rectlist[i].Right;
b.endPoint.Y = (rectlist[i].Top + rectlist[i].Bottom) / 2;
alllines.Add(b);
}
for(int i=0;i var b=新行()//所有行
是否包含除b
以外的内容?我检查每行的起始点,它们不相同,但在按钮外单击,我再次检查它们,它们是相同的!!这意味着什么?这意味着当添加它们时,您没有进行正确的计算,或者如果您在循环中添加它们你使用的是相同的索引。换句话说,在局部区域内,线的起点是可接受的,不相同的,但在绘制事件和其他区域中,线是相同的!!我怀疑线总是相同的,请在将它们添加到列表时检查谢谢兄弟,我知道我被更新到了同一个线对象中^^
for (int i = 0; i < rectlist.Count; i++)
{
var b = new Lines(); // <-- you need to do this
b.startPoint.X = rectlist[i].X;
b.startPoint.Y = (rectlist[i].Y + rectlist[i].Bottom) / 2;
b.endPoint.X = rectlist[i].Right;
b.endPoint.Y = (rectlist[i].Top + rectlist[i].Bottom) / 2;
alllines.Add(b);
}