Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 图片框不更新其内容_C#_Winforms_Picturebox - Fatal编程技术网

C# 图片框不更新其内容

C# 图片框不更新其内容,c#,winforms,picturebox,C#,Winforms,Picturebox,对不起,我的英语不好。。。您好,我已经制作了这个图片框,但是每当我尝试加载一个新的图片时,它不会显示任何东西,只是一个空的图片框控件。这是我目前掌握的代码: var loadImage = new BackgroundWorker(); loadImage.RunWorkerAsync(); // The stuff that is being executed in the backgroundworker. loadImage.DoWork += (o, args) => {

对不起,我的英语不好。。。您好,我已经制作了这个图片框,但是每当我尝试加载一个新的图片时,它不会显示任何东西,只是一个空的图片框控件。这是我目前掌握的代码:

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();
    };

我做错什么了吗?如果是这样的话,请告诉我是什么?

当我直接就您的问题向您发表评论并作为回答时,您正在运行后台工作人员,然后再添加您的代表


可能,您只需要交换代码中的顺序。

您是否调试过,以了解是否正在将有效的
图像
分配给
参数。结果
工作
代码中?@DonBoitnott是,它分配图像。为什么在附加代理之前运行后台工作程序?@HuorSwords我其实不知道。。。这似乎就是问题所在。很抱歉浪费时间,我真的不知道为什么会发生这种事。谢谢:)。愚蠢的我。。。