如何在图片框中绘制图纸
在图片中绘制图片使用c#拖动鼠标
将PictureBox放在表单上,并将其BackColor设置为白色。 然后将此代码添加到您的表单中(您必须实际连接下面的鼠标事件,即您不能将此代码复制并粘贴到您的表单中):
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; }
然后抽离!
如果您愿意,可以通过设置Graphics
对象的SmoothingMode
属性来稍微提高绘制图像的质量。
更新: .Net CF没有Pens
集合,而且MoustEventArgs
没有Location
,所以这里是CF友好版本:
private Point? _Previous = null; private Pen _Pen = new Pen(Color.Black); private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { _Previous = new Point(eX, eY); 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, eX, eY); } pictureBox1.Invalidate(); _Previous = new Point(eX, eY); } } private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { _Previous = null; }
这里,pictureBox1 ==签名。 我以这种方式翻译成vb:
全球:
Dim _previous As Point = Nothing Dim _pen As Pen = New Pen(Color.Black) Dim drawing As Boolean = False ''' ''' This handles the signature drawing events (drawing) ''' ''' ''' ''' 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, eX, eY) End Using signature.Invalidate() _previous = New Point(eX, eY) End If End Sub ''' ''' this indicates somebody is going to write a signature ''' ''' ''' ''' Private Sub signature_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles signature.MouseDown _previous = New Point(eX, eY) drawing = True signature_MouseMove(sender, e) End Sub ''' ''' the signature is done. ''' ''' ''' ''' 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
您可以通过捕获图片框的mousemove事件然后从图片框中获取图片来实现。
图形g = pictureBox.CreateGraphics(); 那么你可以使用这个图形对象绘制你想要绘制的任何东西