2011-02-09 3 views
1

다음과 같은 이미지로 작업하고 있습니다. http://imgur.com/a2VKbC# Windows Forms. PictureBox의 이동 가능 영역

스캔 한 이미지의 세로선을 찾을 수있었습니다. 하지만 때로는 오류가 있으며 사용자가이 선 위치와 각도를 변경할 수있는 옵션을 만들어야합니다. 나는 이것이 PictureBox 내에서 멋지다고 생각한다.

어떻게 든 현재 이미지가있는 그림 상자의 두 이동 가능 점 사이에 선을 그려야합니다. 점을 움직이면 선의 위치와 각도가 바뀌어야합니다.

답변

2

다음은 필요한 경우 사용할 수있는 샘플 코드입니다. 그것은 기본적으로 4 개 이벤트 사용
- 페인트를
- MouseDown
- MouseMove 이벤트
- 이는 MouseUp

당신은

pictureBox1

라는 그림 상자로, Form1에라는 형태로이 코드를 복사하여 붙여 넣을 수 있습니다

int handleRadius = 3; 
    int mPointMoveInProgress = 0; 
    Point mPoint1, mPoint2; 

    public Form1() 
    { 
     InitializeComponent(); 

     mPoint1 = new Point(50, 50); // Set correct default values 
     mPoint1 = new Point(50, 300); // Set correct default values 
    } 

    private void pictureBox1_Paint(object sender, PaintEventArgs e) 
    { 
     // Draw line 
     e.Graphics.DrawLine(new Pen(Color.Black, 2), mPoint1, mPoint2); 

     Rectangle rectangle; 

     // Draw first handle 
     rectangle = new Rectangle(mPoint1.X - handleRadius, mPoint1.Y - handleRadius, handleRadius * 2, handleRadius * 2); 
     e.Graphics.FillRectangle(Brushes.White, rectangle); 
     e.Graphics.DrawRectangle(Pens.Black, rectangle); 

     // Draw second handle 
     rectangle = new Rectangle(mPoint2.X - handleRadius, mPoint2.Y - handleRadius, handleRadius * 2, handleRadius * 2); 
     e.Graphics.FillRectangle(Brushes.White, rectangle); 
     e.Graphics.DrawRectangle(Pens.Black, rectangle); 
    } 

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e) 
    { 
     // Determine if a point is under the cursor. If so, declare that a move is in progress 
     if (Math.Abs(e.X - mPoint1.X) < handleRadius && Math.Abs(e.Y - mPoint1.Y) < handleRadius) mPointMoveInProgress = 1; 
     else if (Math.Abs(e.X - mPoint2.X) < handleRadius && Math.Abs(e.Y - mPoint2.Y) < handleRadius) mPointMoveInProgress = 2; 
     else mPointMoveInProgress = 0; 
    } 

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e) 
    { 
     if (mPointMoveInProgress == 1) // If moving first point 
     { 
      mPoint1.X = e.X ; 
      mPoint1.Y = e.Y ; 
      Refresh(); 
     } 
     else if (mPointMoveInProgress == 2) // If moving second point 
     { 
      mPoint2.X = e.X ; 
      mPoint2.Y = e.Y ; 
      Refresh(); 
     } 
     else // If moving in the PictureBox: change cursor to hand if above a handle 
     { 
      if (Math.Abs(e.X - mPoint1.X) < handleRadius && Math.Abs(e.Y - mPoint1.Y) < handleRadius) Cursor.Current = Cursors.Hand; 
      else if (Math.Abs(e.X - mPoint2.X) < handleRadius && Math.Abs(e.Y - mPoint2.Y) < handleRadius) Cursor.Current = Cursors.Hand; 
      else Cursor.Current = Cursors.Default; 
     } 
    } 

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e) 
    { 
     // Declare that no move is in progress 
     mPointMoveInProgress = 0; 
    } 

+0

이것입니다 !!! 고맙습니다!! – ieaglle

1

그래픽 클래스를 사용하십시오.

(마우스 이동 처리기에서 또는 필요한 곳에) 그리기에 대한 새로운 이벤트가있는 동안 :

// we don't need to change imageSource 
Image imgSourceCopy = imageSource.Clone as Image; 

Graphics g = Graphics.FromImage(imgSourceCopy); 

g.DrawLine(point1, point2); 
pictureBox.Image = imgSourceCopy; 

imgSourceCopy 만 선을 그리기 위해 사용됩니다. p.s. 안녕하세요 lviv에서 :)

+0

안녕하세요 키예프에서! :) 그러나 나는 그들이 배치 된 후에 어떻게 그 점들을 움직일 수 있습니까? – ieaglle

+0

MouseMove를 제어하기위한 핸들러를 추가하십시오. 그러면 PB (point1)에서 마우스를 찾을 수 있습니다. –

+0

http://www.java2s.com/Code/CSharp/2D-Graphics/Drawaline.htm 이것은 도움이 될 수 있습니다 –