C# 如何在图片框中绘制图纸

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1537090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:32:49  来源:igfitidea点击:

how to draw drawings in picture box

c#picturebox

提问by Binu

draw pictures in picture Box on Mouse dragging using c#

使用c#在鼠标拖动时在图片框中绘制图片

回答by Firoz

You can do it by capturing the mousemove event of picture box then get graphics from picture box like .

您可以通过捕获图片框的 mousemove 事件然后从图片框中获取图形来实现。

Graphics g= pictureBox.CreateGraphics(); then u can draw whatever u want draw using this graphics object

图形 g=pictureBox.CreateGraphics(); 然后您可以使用此图形对象绘制您想要绘制的任何内容

回答by MusiGenesis

Put a PictureBox on your form, and set its BackColor to White. Then add this code to your form (you have to actually hook up the Mouse events below, i.e. you can't just copy and paste this code into your form):

在窗体上放置一个 PictureBox,并将其 BackColor 设置为 White。然后将此代码添加到您的表单中(您必须实际连接下面的鼠标事件,即您不能只是将此代码复制并粘贴到您的表单中):

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;
}

And then draw away!

然后抽离!

If you like, you can improve the quality of the drawn image somewhat by setting the Graphicsobject's SmoothingModeproperty.

如果您愿意,可以通过设置Graphics对象的SmoothingMode属性来稍微提高绘制图像的质量。

Update:.Net CF does't have the Penscollection, and MoustEventArgsdoesn't have a Location, so here is a CF-friendly version:

更新:.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(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;
}

回答by Jim

Here, pictureBox1 == signature. I translated to vb in this manner:

在这里,pictureBox1 == 签名。我以这种方式翻译成vb:

Global:

全球的:

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