大家好,本人是C#的初學者,現在碰到個DrawLine的問題。
1.我如果直接呼叫DrawLine沒有任何問題,主要代碼如下:
foreach (var lines in marchingSquares.contourData)
{
foreach (var line in lines.Value)
{
int cur_startY = Convert.ToInt32(line.startPoint.Y * length);
int cur_startX = Convert.ToInt32(image.Height - line.startPoint.X * length);
int cur_endY = Convert.ToInt32(line.endPoint.Y * length);
int cur_endX = Convert.ToInt32(image.Height - line.endPoint.X * length);
g.DrawLine(pen, cur_startY, cur_startX, cur_endY, cur_endX);
}
}
2.但是現在為了實作線條的可編輯,自己定義了一個控制元件,在控制元件中drawLine,主要代碼如下:
List<Line_EXT> lineExtList = new List<Line_EXT>();
foreach (var lines in marchingSquares.contourData)
{
foreach (var line in lines.Value)
{
int cur_startY = Convert.ToInt32(line.startPoint.Y * length);
int cur_startX = Convert.ToInt32(image.Height - line.startPoint.X * length);
int cur_endY = Convert.ToInt32(line.endPoint.Y * length);
int cur_endX = Convert.ToInt32(image.Height - line.endPoint.X * length);
lineExtList.Add(new Line_EXT(cur_startY,cur_startX,cur_endY,cur_endX));
}
}
mouseDragCurveImpl = new MouseDragCurve(g,pen,lineExtList);
mouseDragCurveImpl.Location = new Point(0, 0);
mouseDragCurveImpl.Dock = System.Windows.Forms.DockStyle.Fill;
this.contourMap.Controls.Add(mouseDragCurveImpl);
lineExtList保存了所有線段的起點和終點。MouseDragCurve class定義如下:
class MouseDragCurve : Control
{
// client rectangle
Rectangle clientRect;
//flag
bool mouseDown = false;
private Graphics g;
private Pen pen;
List<Line_EXT> lineExtList;
#region Inits
public MouseDragCurve(Graphics g,Pen pen,List<Line_EXT> lineExtList)
{
this.g = g;
this.pen = pen;
this.lineExtList = lineExtList;
this.BackColor = Color.White;
this.DoubleBuffered = true; // 雙緩沖,避免閃爍
}
void drawCurve()
{
for(int i = 0;i < lineExtList.Count;i++)
{
g.DrawLine(pen,lineExtList[i].startY,lineExtList[i].startX,lineExtList[i].endY,lineExtList[i].endX);
}
}
protected override void OnPaint(PaintEventArgs e)
{
drawCurve();
}
經過除錯OnPaint,drawCurve都呼叫到了,為什么圖沒顯示出來,請大家指教,謝謝。不勝感激!
uj5u.com熱心網友回復:
https://github.com/wangdeshui/paint.net看看,可以解決winform繪圖問題

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/86369.html
標籤:C#
上一篇:Visual Studio 2013(VB.net)路過的各位大神,能否給我一個連接access檔案的原始碼?
下一篇:新手求C#學習視頻
