2017-04-13 59 views
-1

我開始一個新的項目,我尋找一些解決方案,在一個PictureBox如何在PictureBox中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 
+0

http://stackoverflow.com/a/3124252/17034 –

回答

0

你有權利直接寫入到c:\?你會得到什麼錯誤信息? 也許嘗試不保存圖像文件

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

這絕對是把一個屏幕截圖上的圖片框。我不知道還有什麼可以告訴你的

PictureBox1.Image = screenshot 

在這裏,我點擊按鈕6次,它保持工作!後

enter image description here

+0

我也嘗試像你顯示,但然後它的油漆只在一個窗體上我按下按鈕加載圖片框上的屏幕截圖,我無法在屏幕上只畫上 – Stenberg

-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 
+0

因此,最後,'PictureBox1.Image = screenshot' 。如果我的回答不夠(我懷疑這不是因爲你在這裏拋棄了更多的代碼),那麼你能解釋一下你是如何修復它的嗎? – djv

+0

是的當然,我的行動是測試所有的可能性和我需要做的是在這一行中需要的其他故事PictureBox1_Paint – Stenberg

+0

我沒有像這樣定義這樣的方式,我無法在它的頂部繪畫 – Stenberg