2
對不起,我的英語不好......你好,我已經制作了這個圖片盒,但每當我嘗試在那裏加載一個新圖片時,它不會顯示任何內容,只是一個空的圖片框控制。這是我目前的代碼:圖片框沒有更新其內容
var loadImage = new BackgroundWorker();
loadImage.RunWorkerAsync();
// The stuff that is being executed in the backgroundworker.
loadImage.DoWork += (o, args) =>
{
// Loop through the list of items to find the matching picture.
foreach (var item in listItems)
{
if (item.Name == name)
{
try
{
// Get the image.
var request = WebRequest.Create(item.Picture);
using (var response = request.GetResponse())
using (var stream = response.GetResponseStream())
{
Image image = Image.FromStream(stream);
args.Result = image;
return;
}
}
catch (Exception)
{
}
}
}
};
// Execute this when the backgroundworker is finished.
loadImage.RunWorkerCompleted += (o, args) =>
{
pictureBox1.Image = (Image)args.Result;
Application.DoEvents();
};
有什麼我做錯了嗎?如果是這樣,你能告訴我什麼?
您是否調試瞭解您是否在'DoWork'代碼中爲'args.Result'分配了一個有效的'Image'? – DonBoitnott
@DonBoitnott是的,它分配圖像。 –
爲什麼你在附加你的代表之前運行後臺工作者**? – HuorSwords