【问题标题】:Picture box not updating its content图片框不更新其内容
【发布时间】:2013-06-21 00:52:39
【问题描述】:

抱歉我的英语不好...您好,我已经制作了这个图片框,但是每当我尝试在其中加载新图片时,它什么都不显示,只是一个空的图片框控件。这是我目前拥有的代码:

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 代码中将有效的Image 分配给args.Result?
  • @DonBoitnott 是的,它分配了图像。
  • 为什么在附加代表之前运行后台工作程序?
  • @HuorSwords 我实际上不知道......这似乎是问题所在。抱歉浪费时间,我真的不知道为什么会这样。谢谢 :)。愚蠢的我......

标签: c# winforms picturebox


【解决方案1】:

当我直接对您的问题发表评论并将其作为答案时,您在附加代表之前运行后台工作程序。

可能,您只需要在代码中交换订单即可。

【讨论】:

    猜你喜欢
    • 2012-12-17
    • 1970-01-01
    • 2016-04-24
    • 1970-01-01
    • 1970-01-01
    • 2016-09-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-22
    相关资源
    最近更新 更多