我正在使用工作線程將.png圖像(從路徑字符串)加載到全局PictureBox2對象中,然後退出_RunWorkerCompleted以在名爲processpic2的方法中使用PictureBox2的寬度和高度進行其他工作。一切正常,直到第5或第6張圖像被添加到PB。這一點,在processpic2方法中,由於PictureBox2的圖像屬性評估爲無,所以拋出異常。PictureBox停止添加圖像
爲什麼PB會在一段時間後停止拍照?
Public Class Form1
Public WithEvents BackgroundWorker1 As New System.ComponentModel.BackgroundWorker
Private Sub BackGroundworker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim args As ArgumentType = e.Argument
PictureBox2.Image = Nothing
PictureBox2.Invalidate()
Dim img As Image
Using str As Stream = File.OpenRead(args._pathstr)
img = Image.FromStream(str)
End Using
PictureBox2.Image = img
e.Result = "done"
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) _Handles BackgroundWorker1.RunWorkerCompleted
'Called when the BackgroundWorker is completed.
processpic2()
End Sub
Sub btnLoadPicture(pathstr)
Dim args As ArgumentType = New ArgumentType()
args._pathstr = pathstr
BackgroundWorker1.RunWorkerAsync(args)
End Sub
Sub processpic2()
If PictureBox2.Image Is Nothing Then MsgBox("Image is Nothing")
End Sub
不要從後臺工作進程的UI元素,這就是爲什麼...使用委託和調用...圖片框是在不同的線程比背景的工作人員... – Codexer
我不認爲我需要回答這個爲什麼評論。看看你正在使用的UI控件的調用會讓你朝着正確的方向前進。如果您仍然需要幫助,請更新您的問題,我們很樂意爲您提供幫助。 – Codexer
我同意Zaggler。你可能會在'RunWorkerCompletedEventArgs'(即'e.Result')中返回加載的圖像,並在RunWorkerCompleted中設置'PictureBox.Image'。 – SSS