2017-04-13 67 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