Общая ошибка в visualbasic.net - PullRequest
0 голосов
/ 10 января 2012

Как мне исправить эту ошибку с помощью приведенного ниже кода в visual basic.net с использованием Visual Basic Express?Не поддерживается исключение было обработано после 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 голосов
/ 10 января 2012

Вам нужно изменить строку:

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
...