下载图像文件并动态创建控件C#

时间:2017-06-30 08:52:33

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

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

下面是我在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

1 个答案:

答案 0 :(得分:3)

这是一个可以通过使用任务并行库(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,通过创建下载图像的控件并显示它。