2009-10-08 41 views

Trả lời

0

Bạn có thể thực hiện việc này bằng cách chụp sự kiện di chuột của hộp hình rồi lấy đồ họa từ hộp hình ảnh như thế nào.

Đồ họa g = pictureBox.CreateGraphics(); thì u có thể vẽ bất kỳ thứ gì bạn muốn vẽ bằng cách sử dụng đối tượng đồ họa này

+0

Eeeeep, thần không! Bạn sẽ nhận được đồ họa flickery theo cách này, các usecase chỉ cho .CreateGraphics() Tôi biết là để đo chuỗi. Thay vào đó, bạn muốn ghi đè lên OnPaint. – Quibblesome

+0

hoặc sử dụng thuộc tính .Image. Nếu chúng ta sẽ làm bản vẽ tùy chỉnh mặc dù tôi muốn giết tắt picturebox hoàn toàn và kế thừa từ Control thay thế. – Quibblesome

20

Đặt Hộp ảnh trên biểu mẫu của bạn và đặt BackColor thành Trắng. Sau đó, thêm mã này vào dạng của bạn (bạn phải thực sự treo lên các sự kiện chuột dưới đây, ví dụ: bạn có thể không chỉ cần sao chép và dán mã này vào hình thức của bạn):

private Point? _Previous = null; 
private void pictureBox1_MouseDown(object sender, MouseEventArgs e) 
{ 
    _Previous = e.Location; 
    pictureBox1_MouseMove(sender, e); 
} 
private void pictureBox1_MouseMove(object sender, MouseEventArgs e) 
{ 
    if (_Previous != null) 
    { 
     if (pictureBox1.Image == null) 
     { 
      Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height); 
      using (Graphics g = Graphics.FromImage(bmp)) 
      { 
       g.Clear(Color.White); 
      } 
      pictureBox1.Image = bmp; 
     } 
     using (Graphics g = Graphics.FromImage(pictureBox1.Image)) 
     { 
      g.DrawLine(Pens.Black, _Previous.Value, e.Location); 
     } 
     pictureBox1.Invalidate(); 
     _Previous = e.Location; 
    } 
} 
private void pictureBox1_MouseUp(object sender, MouseEventArgs e) 
{ 
    _Previous = null; 
} 

Và sau đó vẽ đi!

Nếu bạn thích, bạn có thể cải thiện chất lượng hình ảnh được vẽ một phần bằng cách đặt thuộc tính SmoothingMode của đối tượng .

Cập nhật: Net CF does't có bộ sưu tập Pens, và MoustEventArgs không có Location, vì vậy đây là một phiên bản CF-thân thiện:

private Point? _Previous = null; 
private Pen _Pen = new Pen(Color.Black); 
private void pictureBox1_MouseDown(object sender, MouseEventArgs e) 
{ 
    _Previous = new Point(e.X, e.Y); 
    pictureBox1_MouseMove(sender, e); 
} 
private void pictureBox1_MouseMove(object sender, MouseEventArgs e) 
{ 
    if (_Previous != null) 
    { 
     if (pictureBox1.Image == null) 
     { 
      Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height); 
      using (Graphics g = Graphics.FromImage(bmp)) 
      { 
       g.Clear(Color.White); 
      } 
      pictureBox1.Image = bmp; 
     } 
     using (Graphics g = Graphics.FromImage(pictureBox1.Image)) 
     { 
      g.DrawLine(_Pen, _Previous.Value.X, _Previous.Value.Y, e.X, e.Y); 
     } 
     pictureBox1.Invalidate(); 
     _Previous = new Point(e.X, e.Y); 
    } 
} 
private void pictureBox1_MouseUp(object sender, MouseEventArgs e) 
{ 
    _Previous = null; 
} 
+0

Cảm ơn sự giúp đỡ, nhưng tôi đã nhận lỗi mà không biết bút và e.location – Gold

+0

@Gold: chỉ cần thêm một phiên bản CF thân thiện của phương pháp này, vì CF không có bộ sưu tập Bút và MouseEventArgs không có Vị trí (chỉ X và Y). – MusiGenesis

+0

+1 cho một câu trả lời dài, chi tiết cho một câu hỏi ngắn, không được tốt. :) – JYelton

2

Ở đây, pictureBox1 == chữ ký . Tôi dịch sang vb theo cách này:

toàn cầu:

Dim _previous As Point = Nothing 
Dim _pen As Pen = New Pen(Color.Black) 
Dim drawing As Boolean = False 


''' <summary> 
''' This handles the signature drawing events (drawing) 
''' </summary> 
''' <param name="sender"></param> 
''' <param name="e"></param> 
''' <remarks></remarks> 
Private Sub signature_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles signature.MouseMove 
    If drawing = True Then 
     If signature.Image Is Nothing Then 
      Dim bmp As Bitmap = New Bitmap(signature.Width, signature.Height) 

      Using g As Graphics = Graphics.FromImage(bmp) 
       g.Clear(Color.White) 
      End Using 

      signature.Image = bmp 
     End If 

     Using g As Graphics = Graphics.FromImage(signature.Image) 
      g.DrawLine(_pen, _previous.X, _previous.Y, e.X, e.Y) 
     End Using 
     signature.Invalidate() 
     _previous = New Point(e.X, e.Y) 
    End If 
End Sub 

''' <summary> 
''' this indicates somebody is going to write a signature 
''' </summary> 
''' <param name="sender"></param> 
''' <param name="e"></param> 
''' <remarks></remarks> 
Private Sub signature_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles signature.MouseDown 
    _previous = New Point(e.X, e.Y) 
    drawing = True 
    signature_MouseMove(sender, e) 

End Sub 

''' <summary> 
''' the signature is done. 
''' </summary> 
''' <param name="sender"></param> 
''' <param name="e"></param> 
''' <remarks></remarks> 
Private Sub signature_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles signature.MouseUp 
    _previous = Nothing 
    drawing = False 
End Sub 
Các vấn đề liên quan