2011-02-09 11 views
1

私は次のような画像を扱っています:http://imgur.com/a2VKbC#Windowsフォーム。 PictureBox上の移動可能な領域

私はスキャンした画像のページ間に垂直線を見つけることができました。しかし時にはいくつかのエラーがあり、私はユーザーがこの行の位置と角度を変更するオプションを作る必要があります。私はこれがPictureBox内でうまくいくと思います。

私はどういうわけか、現在の画像のあるピクチャボックス上の2つの可動点の間に線を描く必要があります。ポイントを動かすと、ラインの位置と角度が適切に変更されなければなりません。

答えて

2

あなたの必要に応じて使用できるサンプルコードです。あなたがコピー&ペーストすることができ、このコードをForm1のと呼ばれるフォームに、pictureBox1

と呼ばれるピクチャボックスとのMouseUp

- のMouseDown
- -
ペイント - のMouseMove

:それは基本的に4つのイベントを使用しています

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.こんにちはfrom lviv :)

+0

こんにちはキエフ! :)しかし、私は彼らが配置された後にそれらのポイントを移動することができますか? – ieaglle

+0

MouseMoveを制御するハンドラを追加します。次に、PB(point1)上のマウスの位置を見つけることができます。 –

+0

http://www.java2s.com/Code/CSharp/2D-Graphics/Drawaline.htmこれは役に立ちます –

関連する問題