2011-06-05 145 views
1

首先,我需要在加載到PictureBox控件的單色輸入圖像上製作一些顏色塗鴉(下面的圖片來自M. Yang關於靜止圖像着色的文章)。在PictureBox中畫線

Enter image description here

我想用這個來得到的效果:

private void PictureBoxOnMouseDown(Object sender, MouseEventArgs e) 
{ 
    if((e.Button & MouseButtons.Left) == MouseButtons.Left) 
    { 
     this.MouseInitialPosition = e.Location; 
    } 
} 

private void PictureBoxOnMouseMove(Object sender, MouseEventArgs e) 
{ 
    if((e.Button & MouseButtons.Left) == MouseButtons.Left) 
    { 
     this.MouseLastPosition = e.Location; 
    } 
    this._PictureBox.Invalidate(); 
} 

private void PictureBoxOnPaint(Object sender, PaintEventArgs e) 
{ 
    using(var pen = new Pen(Color.Red, 3.0F)) 
    { 
     e.Graphics.DrawLine(pen, this.MouseInitialPosition, this.MouseLastPosition); 
    } 
} 

但是,這讓我不太我一直在等待:

  • 我可以不要放幾條線。線路不存儲;

  • 我用行覆蓋行;

二。我需要從我已經繪製的圖像中獲取所有像素,並以某種方式對其進行過濾(即,提取特定的像素)。如何將線條/塗鴉存儲到圖像上然後有效地讀取圖像?

回答

2

簡單的解決辦法是,當一個mouseMove事件上PictureBox控件出現存儲線,然後無效重繪:

public class Lines 
{ 
    public Point startPoint = new Point(); 
    public Point endPoint = new Point(); 
} 

Lines l = new Lines(); 
List<Lines> allLines = new List<Lines>(); 

private void pictureBox1_MouseMove(object sender, MouseEventArgs e) 
{ 
    // Collect endPoint when mouse moved 
    if ((e.Button & MouseButtons.Left) == MouseButtons.Left) 
    { 
     l.endPoint = e.Location; 
     //Line completed 
     allLines.Add(l); 
     this.pictureBox1.Invalidate(); 
    } 
} 

private void pictureBox1_MouseDown(object sender, MouseEventArgs e) 
{ 
    // Collect startPoint when left mouse clicked 
    if ((e.Button & MouseButtons.Left) == MouseButtons.Left) 
    { 
     l = new Lines(); 
     l.startPoint = e.Location; 
    } 
} 

private void pictureBox1_Paint(object sender, PaintEventArgs e) 
{ 
    foreach (var aLine in allLines) 
    { 
     e.Graphics.DrawLine(Pens.Blue, aLine.startPoint, aLine.endPoint); 
    } 
} 
1

只需保留一行行。然後,當調用OnPaint時,繪製所有行。

Line類會是這樣的:

public class Line 
{ 
    public List<Point> Points = new List<Point>(); 

    public DrawLine(Pen pen, Grphics g) 
    { 
     for (int i = 0; i < Points.Count - 1; ++i) 
      g.DrawLine(pen, Points[i], Points[i+1]; 
    } 
} 

private void PictureBoxOnMouseDown(Object sender, MouseEventArgs e) 
{ 
    if((e.Button & MouseButtons.Left) == MouseButtons.Left) 
    { 
     var newLine = new Line(); 
     newLine.Points.Add(e.Location); 
     lines.Add(newLine); 
    } 
} 

PictureBoxOnMouseMove(Object sender, MouseEventArgs e) 
{ 
    if((e.Button & MouseButtons.Left) == MouseButtons.Left) 
    { 
     lines[lines.Count-1].Points.Add(e.Location); 
    } 
} 

private void PictureBoxOnPaint(Object sender, PaintEventArgs e) 
{ 
    using(var pen = new Pen(Color.Red, 3.0F)) 
    { 
     foreach (var line in lines) 
      DrawLine(pen, e.Graphics) 
    } 
} 
+0

在拖動,你會產生很多鼠標移動事件。在此代碼中,所有這些事件都會導致添加一行。經過一次拖拽後,您將在線條集合中獲得很多線條,而每條線條需要一條線條。執行此操作的正確方法是在每次移動鼠標後添加該行,但在drag = mouse up事件結束時添加該行。 – 2015-12-30 13:04:16

+1

@HaraldDutch,OP詢問有關塗鴉而不是線條。 – 2015-12-30 20:53:58