我正在使用諸如以下圖像:http://imgur.com/a2VKbC#Windows窗體。 PictureBox上的可移動區域
我已經設法在掃描圖像上的頁面之間找到垂直線條。但有時會出現一些錯誤,我需要爲用戶選擇更改此線位置和角度的選項。我認爲這在PictureBox中會很好。
我需要以某種方式在當前圖像的圖片框上的兩個可移動點之間繪製一條線。當我移動一個點時,線的位置和角度必須適當地改變。
我正在使用諸如以下圖像:http://imgur.com/a2VKbC#Windows窗體。 PictureBox上的可移動區域
我已經設法在掃描圖像上的頁面之間找到垂直線條。但有時會出現一些錯誤,我需要爲用戶選擇更改此線位置和角度的選項。我認爲這在PictureBox中會很好。
我需要以某種方式在當前圖像的圖片框上的兩個可移動點之間繪製一條線。當我移動一個點時,線的位置和角度必須適當地改變。
以下是您可以根據需要使用的示例代碼。它主要採用4個事件:
- 油漆
- 的MouseDown
- 的MouseMove
- 的MouseUp
您可以複製粘貼此代碼到一個名爲Form1窗體,一個名爲pictureBox1
圖片框
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;
}
使用Graphics類。
雖然有關於圖畫(在鼠標移動的處理程序或在需要的地方)的新事件:
// 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. hello來自lviv :)
你好,來自基輔! :)但是,如何在放置後移動這些點? – ieaglle 2011-02-09 19:39:32
添加處理程序來控制MouseMove。然後你將能夠找到PB上的鼠標(point1)。 – 2011-02-09 19:43:40