如何在Picturebox VB.net上绘画

时间:2017-04-13 20:53:55

标签: vb.net

我已经开始了一个新项目,我在一个图片框上搜索一些解决方案

使用此代码我能够在一个表单上绘制,但我需要在一个图片框上绘图,我已经尝试了多种方法,但我找不到在图片框中的屏幕截图上做到这一点的方法 我需要改变什么才能让它发挥作用? 这是我的代码

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

2 个答案:

答案 0 :(得分:0)

您是否有权直接写入c:\?你得到什么错误信息?  也许尝试不保存图像文件

'screenshot.Save("c:\\dcap.jpg", Imaging.ImageFormat.Bmp)

这绝对会在PictureBox上放置截图。我不知道还能告诉你什么

PictureBox1.Image = screenshot

在这里,我点击按钮6次,它一直在工作!

enter image description here

答案 1 :(得分:-1)

经过这么多次尝试后我才开始工作 这是工作代码

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