visualbasic.net中的一般错误

时间:2012-01-09 20:55:03

标签: vb.net system.drawing

如何使用visual basic express在visual basic.net中使用以下代码修复此错误? 不支持异常在3次循环后未处理。 “PictureBox1.Image.Save(dtmTestX,System.Drawing.Imaging.ImageFormat.Jpeg)”

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

    Dim dtmTest As Date
    dtmTest = TimeValue(Now)
    Dim bounds As Rectangle
    Dim screenshot As System.Drawing.Bitmap
    Dim graph As Graphics
    Dim dtmTestSaveLocation As String
    dtmTestSaveLocation = "D:\test" + dtmTest + ".jpg"

    bounds = Screen.PrimaryScreen.Bounds
    screenshot = New System.Drawing.Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
    graph = Graphics.FromImage(screenshot)
    graph.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy)
    PictureBox1.Image = screenshot

    PictureBox1.Image.Save(dtmTestSaveLocation, System.Drawing.Imaging.ImageFormat.Jpeg)
End Sub

1 个答案:

答案 0 :(得分:0)

您需要更改一行:

PictureBox1.Image.Save(dtmTestX, System.Drawing.Imaging.ImageFormat.Jpeg)

为:

PictureBox1.Image.Save(dtmTestSaveLocation, System.Drawing.Imaging.ImageFormat.Jpeg)

您还应该在使用标签时真正嵌入图形元素,以确保它们得到妥善处理:

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    Try
        Dim dtmTest As Date
        Dim bounds As Rectangle
        Dim dtmTestSaveLocation As String

        dtmTest = TimeValue(Now)
        dtmTestSaveLocation = "D:\test" & dtmTest.ToShortTimeString.Replace(":", "_") & ".jpg"

        bounds = Screen.PrimaryScreen.Bounds
        Using screenshot As New System.Drawing.Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
            Using graph As Graphics = Graphics.FromImage(screenshot)
                graph.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy)
                PictureBox1.Image = screenshot
                screenshot.Save()
                PictureBox1.Image.Save(dtmTestSaveLocation, System.Drawing.Imaging.ImageFormat.Jpeg)
            End Using
        End Using
    Catch theException As Exception
        Console.WriteLine(theException.ToString)
        ' Note: In production code, you would want to do something useful with the exception 
        ' here, such as showing it to the user in a messagebox or writing it to a log
    End Try
End Sub