【问题标题】:Download image files and create controls dynamically C# [closed]下载图像文件并动态创建控件 C# [关闭]
【发布时间】:2017-06-30 17:20:03
【问题描述】:

我正在构建一个 Windows 窗体应用程序,它允许用户从 url 下载图像文件并将图像设置为图片框,同时为每个图像动态创建图片框、两个按钮和一个文本框。

以下是我在 form_load 方法中使用循环下载图像文件的代码

noofimages = no of images

imagesnames = array containing images names

uploadnumber= folder name created with images

for (int i = 0; i < Globals.noofimages; i++)
{
    client = new WebClient();
    string url = "http://www.upload2printer.co.il/public/uploads/" + Globals.uploadnumber + "/" + Globals.imagesnames[i] + "";
    Uri uri = new Uri(url);
    byte[] bytes;
    bytes = client.DownloadData(uri);
    MemoryStream ms = new MemoryStream(bytes);
    Globals.images[i] = System.Drawing.Image.FromStream(ms);
    create_controls(i);
}


Now create_controls(i) is a method responsible for creating the controls

private void create_controls(int index)
{
    PictureBox pb = new PictureBox();
    pb.Image = Globals.images[index];
    pb.Size = new Size(200, 120);
    Button b1 = new Button();
    Button b2 = new Button();
    TextBox noofprints = new TextBox();
    b1.Size = new Size(20, 20);
    b1.Name = index.ToString();
    b1.Text = "+";
    b1.Location = new Point(x, y + 125);
    b1.Click += new EventHandler(button_Click);
    b2.Size = new Size(20, 20);
    b2.Text = "-";
    b2.Name = index.ToString();
    b2.Location = new Point(x + 180, y + 125);
    b2.Click += new EventHandler(button_Click);
    noofprints.Name = index.ToString();
    noofprints.Size = new Size(160, 18);
    if (status == 0)
    {
        noofprints.Text = "1";
    }
    else if (status == 1)
    {
        noofprints.Text = Globals.noofcopy[index].ToString();
    }
    noofprints.TextAlign = HorizontalAlignment.Center;
    noofprints.Location = new Point(x + 20, y + 125);
    pb.SizeMode = PictureBoxSizeMode.StretchImage;
    pb.Location = new Point(x, y);
    x += pb.Width + 10;
    maxheight = Math.Max(pb.Height, maxheight);
    if (x > this.panel1.Width - 100)
    {
        x = 20;
        y += maxheight + 30;
    }
    this.panel1.Controls.Add(b1);
    this.panel1.Controls.Add(b2);
    this.panel1.Controls.Add(noofprints);
    this.panel1.Controls.Add(pb);
}

现在,当我运行应用程序并打开此表单时,该表单仅在加载所有照片并创建所有控件时显示。

我想要做的是下载每张照片,然后在显示表单时创建其各自的控件。

我还研究了多线程和异步等待,但无法解决任何问题。

我是初学者和自学程序员,所以我知道我的代码并不完美,所以我希望你能帮助解决这个问题。

提前致谢。

below is the image of my interface i want to show pictures one by one not all at once

【问题讨论】:

  • 可能您在 Form_Load 事件中执行此操作。完成此活动后,表格将显示!在 Form_Load 事件中创建一个新任务,并在每个下载的图片之后,将其添加到您的表单中。另一个提示:您必须修改控件的位置
  • 不妨看看stackoverflow.com/questions/19163966/…。您希望保持 UI 响应并在旅途中一次添加一个控件,并在添加后立即呈现它。我认为这是要走的路!

标签: c# .net multithreading winforms async-await


【解决方案1】:

使用任务并行库 (TPL) 数据流方法可以很好地解决这个问题。

但是对于初学者来说,它可能看起来很复杂。

在这里阅读: https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/dataflow-task-parallel-library

https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/walkthrough-using-dataflow-in-a-windows-forms-application

通常您需要做的是创建一个单独的dataflow blocks 来处理特定的工作。 在您的情况下,您将有两个块 - 第一个块可能负责从 Web 获取图像,第二个块可能负责更新 UI,通过为下载的图像创建控件并显示它。

【讨论】:

    猜你喜欢
    • 2013-04-24
    • 1970-01-01
    • 2012-03-12
    • 2011-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多