2012-01-09 48 views
0

如何使用visual basic express在visual basic.net中的下面的代碼修復此錯誤? 3循環後不支持異常未處理。 「PictureBox1.Image.Save(dtmTestX,System.Drawing.Imaging.ImageFormat.Jpeg)」visualbasic.net中的一般錯誤

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

你得到的錯誤信息是什麼? – CedX 2012-01-09 20:59:21

+1

重新標記。這不是VBA。 – HardCode 2012-01-09 21:07:22

+0

文件名將包含從DateTime到字符串轉換的「:」字符。這不是文件名中的有效字符。 – 2012-01-09 21:37:28

回答

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

相同的錯誤:不支持的異常未處理 – user101579 2012-01-09 21:25:09

+0

您可以編輯您的問題以包含完整的異常文本和堆棧跟蹤。您可以通過將文本包裝在try/catch塊中並使用異常的tostring方法將所有內容寫入控制檯來獲取此信息。如果您需要,我可以更新答案以顯示此代碼。 – 2012-01-09 21:26:14

+0

PictureBox1.Image.Save(dtmTestSaveLocation,System.Drawing.Imaging.ImageFormat.Jpeg) - NotSupportedException未處理 - 不支持給定路徑的格式。 – user101579 2012-01-09 21:27:28