如何在Picturebox VB.net上绘制

如何在Picturebox VB.net上绘制,vb.net,Vb.net,我已经开始了一个新的项目,我正在寻找一些解决方案来在一个画框上画画 有了这段代码,我可以在表单上画画,但我需要在图片框上画画,我尝试了多种方法,但在picturebox的屏幕截图上找不到这样做的方法 我需要改变什么才能让它工作? 这是我的密码 Public Class Form3 Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load C

我已经开始了一个新的项目,我正在寻找一些解决方案来在一个画框上画画

有了这段代码,我可以在表单上画画,但我需要在图片框上画画,我尝试了多种方法,但在picturebox的屏幕截图上找不到这样做的方法 我需要改变什么才能让它工作? 这是我的密码

Public Class Form3

Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Cursor = Cursors.Hand
End Sub

Dim mustPaint As Boolean = False

Private Sub MouseEvent_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
    mustPaint = True
End Sub

Private Sub MouseEvent_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
    If mustPaint Then
        Dim graphic As Graphics = CreateGraphics()
        graphic.FillEllipse(New SolidBrush(Color.Red), e.X, e.Y, 10, 5)
    End If
End Sub

Private Sub MouseEvent_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
    mustPaint = False
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim bounds As Rectangle
    Dim screenshot As System.Drawing.Bitmap
    Dim graph As Graphics
    bounds = Screen.PrimaryScreen.Bounds
    screenshot = New System.Drawing.Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb)
    graph = Graphics.FromImage(screenshot)
    graph.CopyFromScreen(0, 0, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy)
    screenshot.Save("c:\\dcap.jpg", Imaging.ImageFormat.Bmp)
    PictureBox1.BackgroundImage = screenshot
End Sub
End Class

您是否有权直接写入
c:\
?您收到了什么错误消息? 可能尝试不保存图像文件

'screenshot.Save("c:\\dcap.jpg", Imaging.ImageFormat.Bmp)
这绝对是在PictureBox上放了一个截图。我不知道还能告诉你什么

PictureBox1.Image = screenshot
在这里,我点击按钮6次,它一直在工作


经过多次尝试,我终于成功了 这是工作代码

Imports System.Drawing.Drawing2D
Public Class Form3

Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub

Dim mustPaint As Boolean = False
Private lastPT As Point
Private signature As New GraphicsPath

Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
    If Not IsNothing(signature) Then
        If e.Button = Windows.Forms.MouseButtons.Left Then
            lastPT = New Point(e.X, e.Y)
        End If
    End If
End Sub

Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
    If Not IsNothing(signature) Then
        If e.Button = Windows.Forms.MouseButtons.Left Then
            Dim curPt As New Point(e.X, e.Y)
            signature.AddLine(lastPT, curPt)
            lastPT = curPt
            PictureBox1.Refresh()
        End If
    End If
End Sub

Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
    If Not IsNothing(signature) Then
        If e.Button = Windows.Forms.MouseButtons.Left Then
            signature.StartFigure()
        End If
    End If
End Sub

Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
    If Not IsNothing(signature) Then
        e.Graphics.DrawPath(Pens.Black, signature)
    End If
End Sub

Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
    signature.Reset()
    PictureBox1.Refresh()
End Sub

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    Dim bmp As New Drawing.Bitmap(PictureBox1.Width, PictureBox1.Height)
    PictureBox1.DrawToBitmap(bmp, PictureBox1.ClientRectangle)
    bmp.Save(System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "test.bmp"), System.Drawing.Imaging.ImageFormat.Bmp)
End Sub
Private Sub MouseEvent_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
    mustPaint = True
End Sub

Private Sub MouseEvent_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
    If mustPaint Then
        Dim graphic As Graphics = CreateGraphics()
        graphic.FillEllipse(New SolidBrush(Color.Red), e.X, e.Y, 10, 5)
    End If
End Sub

Private Sub MouseEvent_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
    mustPaint = False
End Sub


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim bounds As Rectangle
    Dim screenshot As System.Drawing.Bitmap
    Dim graph As Graphics
    bounds = Screen.PrimaryScreen.Bounds
    screenshot = New System.Drawing.Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb)
    graph = Graphics.FromImage(screenshot)
    graph.CopyFromScreen(0, 0, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy)
    screenshot.Save("c:\\dcap.jpg", Imaging.ImageFormat.Bmp)
    'PictureBox1.BackgroundImage = screenshot
    PictureBox1.Image = screenshot


End Sub

End Class

我尝试过像你所展示的那样,但是它只在表单上绘制。我按下按钮将屏幕截图加载到图片框中。我无法仅在表单上绘制屏幕截图。因此,最后,
PictureBox1.Image=screenshot
。如果我的回答还不够(我怀疑这是因为你在这里扔掉了很多代码),那么你能解释一下你是如何修复它的吗?当然可以,我所做的是测试所有的可能性,我需要做的是在这一行中的其他故事,在需要的图片中,我没有像以前这样定义,所以我无法在它的顶部绘画,像这样
如果不是什么(签名),那么e.Graphics.DrawPath(Pens.Black,signature)如果我用钢笔在屏幕截图或加载的照片上画画,则结束