2017-10-05 186 views
1

在我的表單中,我有2 picturebox控件。我在pictureBox1上加載了一個藍色背景圖像,並單獨控制了左邊的pictureBox2。通過下面的代碼,我可以在我的picturebox1圖片上繪製箭頭。將Drawline圖像繪製成Picturebox圖像

目標:在我pictureBox1_MouseUp事件我要添加的所有,我畫了pictureBox1pictureBox2箭頭。

問題:的問題是在我的pictureBox1_MouseUp事件時,我寫pictureBox2.Image = pictureBox1.Image它不添加畫箭頭,我就畫了pictureBox1。它只添加我在表單加載事件中分配的pictureBox1圖像。

private bool isMoving = false; 
    private Point mouseDownPosition = Point.Empty; 
    private Point mouseMovePosition = Point.Empty; 
    private List<Tuple<Point, Point>> lines = new List<Tuple<Point, Point>>(); 
    Pen _Pen; 

    private void pictureBox1_Paint(object sender, PaintEventArgs e) 
    { 

     if (isMoving) 
     { 
      if (pictureBox1.Image == null) e.Graphics.Clear(Color.White); 

      // Add this line for high quality drawing: 
      e.Graphics.SmoothingMode = SmoothingMode.HighQuality; 

      AdjustableArrowCap bigArrow = new AdjustableArrowCap(5, 5); 
      _Pen = new Pen(Color.IndianRed, 3); 
      _Pen.CustomEndCap = bigArrow; 
      e.Graphics.DrawLine(_Pen, mouseDownPosition, mouseMovePosition); 
      _Pen.Dispose(); 
     } 
    } 

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e) 
    { 
     isMoving = true; 
     mouseDownPosition = e.Location; 
    } 

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e) 
    { 
     if (isMoving) 
     { 
      mouseMovePosition = e.Location; 
      pictureBox1.Invalidate(); 
     } 
    } 

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e) 
    { 

     if (isMoving) 
     { 
      lines.Add(Tuple.Create(mouseDownPosition, mouseMovePosition)); 
     } 
     isMoving = false; 

     pictureBox2.Image = pictureBox1.Image; 
    } 

enter image description here

試驗1:(變更pictureBox1_Paint代碼)

有了這個代碼,它利用了pictureBox2的箭頭,但它看起來是繪製多個箭頭。

 if (isMoving) 
     { 
      if (pictureBox1.Image == null) e.Graphics.Clear(Color.White); 

      // Add this line for high quality drawing: 
      e.Graphics.SmoothingMode = SmoothingMode.HighQuality; 

      AdjustableArrowCap bigArrow = new AdjustableArrowCap(5, 5); 
      _Pen = new Pen(Color.IndianRed, 3); 
      Bitmap BitImg = (Bitmap)pictureBox1.Image; 
      _Pen.CustomEndCap = bigArrow; 
      using (var graphics = Graphics.FromImage(BitImg)) 
      { 
       graphics.DrawLine(_Pen, mouseDownPosition, mouseMovePosition); 
      } 
      pictureBox1.Image = BitImg; 
      _Pen.Dispose(); 
     } 

enter image description here

測試2:(我從油漆事件的代碼並粘貼它MouseMove事件有一些修改它使用了太多的內存,它不會對pict​​ureBox1但箭頭畫現在是在pictureBox2可見)

private void pictureBox1_MouseMove(object sender, MouseEventArgs e) 
    { 
     if (isMoving) 
     { 
      mouseMovePosition = e.Location; 

      if (isMoving) 
      { 

       AdjustableArrowCap bigArrow = new AdjustableArrowCap(5, 5); 
       _Pen = new Pen(Color.IndianRed, 3); 
       BitImg = new Bitmap(pictureBox1.Image);      
       _Pen.CustomEndCap = bigArrow; 
       using (var graphics = Graphics.FromImage(BitImg)) 
       { 
        graphics.SmoothingMode = SmoothingMode.HighQuality; 
        graphics.DrawLine(_Pen, mouseDownPosition, mouseMovePosition); 
       } 

       _Pen.Dispose(); 
      } 

      pictureBox1.Invalidate(); 

     } 
    } 

enter image description here

回答

1

繪製所有行pictureBox1: enter image description here

借鑑,只有最後一行pictureBox1: enter image description here

pictureBox2控制,Paint事件添加到pictureBox2_Paint

我建議你做筆和蓋全球varriable:

// Make pen and cap global varriable to boost the perfomane. 
// Create and delete them each draw will cost alot of CPU 
Pen pen = new Pen(Color.IndianRed, 3); 
AdjustableArrowCap bigArrow = new AdjustableArrowCap(5, 5); 

Form1()事件,添加此行:

pen.CustomEndCap = bigArrow; 

,並做如下:

public partial class Form1 : Form 
{ 
    private bool isMoving = false; 
    private Point mouseDownPosition = Point.Empty; 
    private Point mouseMovePosition = Point.Empty; 
    private List<Tuple<Point, Point>> lines = new List<Tuple<Point, Point>>(); 

    public Form1() 
    { 
     InitializeComponent(); 
     pen.CustomEndCap = bigArrow; 
    } 

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e) 
    { 
     isMoving = true; 
     mouseDownPosition = e.Location; 
    } 

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e) 
    { 
     if (isMoving) 
     { 
      mouseMovePosition = e.Location; 
      pictureBox1.Invalidate(); 
     } 
    } 

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e) 
    { 
     if (isMoving) 
     { 
      lines.Add(Tuple.Create(mouseDownPosition, mouseMovePosition)); 
      pictureBox2.Invalidate(); 
     } 
     isMoving = false; 
    } 

    private void pictureBox1_Paint(object sender, PaintEventArgs e) 
    { 
     if (isMoving) 
     { 
      if ((sender as PictureBox).Image == null) e.Graphics.Clear(Color.White); 

      // Add this line for high quality drawing: 
      e.Graphics.SmoothingMode = SmoothingMode.HighQuality; 

      e.Graphics.DrawLine(pen, mouseDownPosition, mouseMovePosition); 

      // If you want draw all previous lines here, add bellow code: 
      //foreach (var line in lines) 
      //{ 
      // e.Graphics.DrawLine(pen, line.Item1, line.Item2); 
      //} 
     } 
    } 

    private void pictureBox2_Paint(object sender, PaintEventArgs e) 
    { 
     if ((sender as PictureBox).Image == null) e.Graphics.Clear(Color.White); 

     e.Graphics.SmoothingMode = SmoothingMode.HighQuality; 
     foreach (var line in lines) 
     { 
      e.Graphics.DrawLine(pen, line.Item1, line.Item2); 
     } 
    } 
} 

以上代碼畫線到PictureBox控件,而不是圖像,這允許您刪除一些行或全部清除如果您稍後想要繪製到picturebox的線條。

如果你想直接繪製圖像,事情事件容易得多,你不需要pictureBox2_Paint都:

private void pictureBox1_MouseUp(object sender, MouseEventArgs e) 
{ 
    if (isMoving) 
    { 
     // You event don't need this line 
     //lines.Add(Tuple.Create(mouseDownPosition, mouseMovePosition)); 

     if (pictureBox1.Image != null) 
     { 
      using (var g = Graphics.FromImage(pictureBox1.Image)) 
      { 
       g.SmoothingMode = SmoothingMode.HighQuality; 
       g.DrawLine(pen, mouseDownPosition, mouseMovePosition); 
      } 
      pictureBox2.Image = pictureBox1.Image; 
     } 
    } 
    isMoving = false; 
} 

private void pictureBox1_Paint(object sender, PaintEventArgs e) 
{ 
    if (isMoving) 
    { 
     if ((sender as PictureBox).Image == null) e.Graphics.Clear(Color.White); 

     e.Graphics.SmoothingMode = SmoothingMode.HighQuality; 
     e.Graphics.DrawLine(pen, mouseDownPosition, mouseMovePosition); 
    } 
} 

private void pictureBox2_Paint(object sender, PaintEventArgs e) 
{ 
} 
+1

優秀的解釋!這很好。謝謝! – taji01

+0

很高興幫助你! – Sakura

1

ÿ你在這裏有兩個問題:

  1. 你在控制而不是圖像上繪畫。圖像保持不變。

  2. pictureBox1pictureBox2都指向相同的圖像。當您更改圖像時,兩個控件都將在下一次繪畫事件中受到影響。

所以在pictureBox1_Paint你需要創建的pictureBox1.Image複印件(請嘗試使用Bitmap),然後在其上繪製和更新的圖像分配給pictureBox1.Image。它會被自動繪製。

+0

安德烈,是你指的是類似測試1級的代碼,我只是把? – taji01