-1

I have start a new project and i search for some solution to paint on a picturebox

with this code i am able to draw on a form but the i need to draw on a picture box, i have try multiple ways but i can not find the way to do it on the screenshoot in the picturebox What i need to change to get it work? This is my code

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
Stenberg
  • 25
  • 2
  • 9

2 Answers2

0

Do you have rights to write directly to c:\? What error message do you get? Maybe try without saving the image file

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

This absolutely puts a screenshot on the PictureBox. I don't know what else to tell you

PictureBox1.Image = screenshot

Here, I clicked the button 6 times and it kept working!

enter image description here

djv
  • 15,168
  • 7
  • 48
  • 72
  • I have try like you show but then its paint only on a form wen i press the button to load the screenshot on the picture box the i am not able to paint on the screenshot only on the form – Stenberg Apr 13 '17 at 21:32
-1

Well after to try so many times i got it work This is the working code

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
Stenberg
  • 25
  • 2
  • 9
  • So in the end, `PictureBox1.Image = screenshot`. If my answer wasn't enough (I suspect it wasn't since you dumped a lot more code here) then could you explain how you fixed it? – djv Apr 14 '17 at 17:26
  • Yes Of Course , what i deed is to test all the possiblity s and what i needed to do is other story in this line in needed PictureBox1_Paint – Stenberg Apr 15 '17 at 12:00
  • i not heaved defined like this before so way i not able to paint on the top of it – Stenberg Apr 15 '17 at 12:01
  • and like that If Not IsNothing(signature) Then e.Graphics.DrawPath(Pens.Black, signature) End If – Stenberg Apr 15 '17 at 12:02
  • I use a pen to paint on the screenshot or my loaded photo – Stenberg Apr 15 '17 at 12:03
  • and i can save all the alterations done on it after paint – Stenberg Apr 15 '17 at 12:05
  • `signature` is not even in your original question. – djv Apr 15 '17 at 20:12
  • No Its true because i have give up from the first option to this one – Stenberg Apr 15 '17 at 22:04
  • The fisrt options i get paint but only on a form and the solution i have find it is with the signature so way i have change – Stenberg Apr 15 '17 at 22:05
  • `PictureBox1.BackgroundImage = screenshot` does not put the image on the PictureBox. `PictureBox1.Image = screenshot` does. That's how we fixed your problem. – djv Apr 16 '17 at 19:13
  • No Not That part That One 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 – Stenberg Apr 17 '17 at 16:07
  • I Solved By Paint Direct On The PictureBox Item And Then save all the alterations in one file only ,clear button etc after to know how to do its easy to remake correct. – Stenberg Apr 17 '17 at 16:10
  • In My first quest is how to paint on a picturebox photo so on the first code i can only paint on a form but after add picturebox on the top of it i continue to paint there but i can not see because i have the picture box there and there is no configuration or code to make it transparent so i needed to find a way to do it, Well there is the problem here: i needed to find way to solve this enigmatic code – Stenberg Apr 17 '17 at 16:17
  • This is put the image on it PictureBox1.Image = screenshot – Stenberg Apr 17 '17 at 16:19
  • And Then 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 – Stenberg Apr 17 '17 at 16:20
  • And save button 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 – Stenberg Apr 17 '17 at 16:21
  • In My Opinion This Is The Part 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 – Stenberg Apr 17 '17 at 16:22
  • You can Find More About Here » VB.NET » VISUAL BASIC LANGUAGE VB.NET Method Signatures – Stenberg Apr 17 '17 at 16:24
  • Here is the title of your question: `How to paint on Picturebox VB.net`. And your question itself: `I have start a new project and i search for some solution to paint on a picturebox. with this code i am able to draw on a form but the i need to draw on a picture box, i have try multiple ways but i can not find the way to do it on the screenshoot in the picturebox What i need to change to get it work? This is my code` *My answer solved this*. So if you had another question, you needed to make a new question on StackOverflow. It is also very hard to understand what you are saying. Good luck – djv Apr 17 '17 at 16:40