2014-06-30 23 views
-1

我正在嘗試使用圖片框並挑選出2點(照片中的眼睛)。我會用g.DrawString挑出兩點,在這裏我將畫出2個不同的'x'。現在的問題是我卡住了,如果用戶把'x'放在錯誤的位置上,我想調整它。是否有任何代碼可以使g.DrawString'x'能夠移動?如何讓C#中的g.drawstring在放入圖片框後進行調整

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace Camera 
{ 
    public partial class CamDisplay : Form 
    { 

     public CamDisplay() 
     { 
      InitializeComponent(); 
      this.pictureBox1.ImageLocation = @"C:\center90\center90(1).jpg"; 
     } 


     bool MousedClicked = true; 
     bool MouseClicked2 = true; 

     private void pictureBox1_MouseClick(object sender, MouseEventArgs e) 
     { 
      base.OnMouseClick(e); 

      if (MousedClicked == true) 
      { 
       txtXaxis.Text = e.X.ToString(); 
       txtYaxis.Text = e.Y.ToString(); 
       MousedClicked = false; 

       using (Graphics g = Graphics.FromHwnd(pictureBox1.Handle)) 
       { 
        using (Font myFont = new Font("Calibri", 8)) 
        { 
         g.DrawString("X", myFont, Brushes.Red, new PointF(e.X, e.Y)); 
        } 
       } 
      } 

      else if (MouseClicked2 == true) 
      { 
       txtRXaxis.Text = e.X.ToString(); 
       txtRYaxis.Text = e.Y.ToString(); 
       MouseClicked2 = false; 

       using (Graphics g = Graphics.FromHwnd(pictureBox1.Handle)) 
       { 
        using (Font myFont = new Font("Calibri", 8)) 
        { 
         g.DrawString("X", myFont, Brushes.Red, new PointF(e.X, e.Y)); 
        } 
       } 
      } 

      else 
      { 
       MousedClicked = false; 
       MouseClicked2 = false; 
      } 

     } 

     private void CamDisplay_MouseEnter(object sender, EventArgs e) 
     { 
      pictureBox1.SizeMode = PictureBoxSizeMode.Zoom; 
      pictureBox1.Cursor = Cursors.Hand; 
     } 
    } 
} 

回答

0

你的問題還不清楚。據我瞭解你的問題,你有兩個選擇:

  1. 如果您使用的是圖形對象直接操作BMP(即你直接抽繩位圖。)有沒有辦法「撤銷」之前的操作。您可以在內存中保留原始位圖的本地副本,將其複製到第二個位圖並僅在副本上執行操作,這是您在窗體中顯示的副本。當您需要「撤消」時,您只需返回原始副本。

  2. 不要直接操作位圖,而是在PictureBox的OnPaint()事件中執行繪圖操作。這樣你只能畫到屏幕上,保持原來的Bmp不變。看到這裏的例子:http://msdn.microsoft.com/en-us/library/b818z6z6%28v=vs.110%29.aspx

+0

我在我的代碼中添加是否可以給我一些建議,我應該做出改變? – user3766178

+0

我很抱歉拖延很久,我一直很忙。快速瀏覽一下你的代碼,看起來你是直接在鼠標點擊事件上繪製窗口。這將是對上述建議(2)的變化。刷新窗口/控件實際上應該「擦除」兩個標記。嘗試調用pictureBox1.Refresh()。 –

相關問題