Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am building a windows form application which allows users to download images file from url and set the images to picturebox while creating the Picturebox, two buttons and a textbox dynamically for each image.

What I have tried:

Below is my code for downloading image file using loop in form_load method

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

Now when i run the application and this form is opened the form is only displayed when all the photos have been loaded and all controls created.

What i want to do is download each photo and then create its respective controls while the form is displayed.

I also looked into multi-threading and async and wait but was not able to figure anything around.

I am a beginner and a self taught programmer so i know my code is not perfect so i would like your help in solving this matter.

Thanks in advance.
Posted
Updated 30-Jun-17 0:59am
Comments
F-ES Sitecore 30-Jun-17 6:30am    
Have a look at this and this

1 solution

Start by not doing anything intensive in the form Load event: set up stuff, yes, but don;t do more than you have to.

The problem you have is that downloading takes time - and until that is finished, you can't display. So do two things:
1) Cache the download images, so that you can display them quicker - you can also download "new copies" and check them for updates pretty easily (I'd store a hash with each image and compare the D/L hash with the cache version, it's simple to do).
2) Display the cached images in the Form.Shown event rather than load - that gives the user some immediate feedback that the application has started.
3) Do the downloads in a different thread.
I'd suggest using the BackgroundWorker class: BackgroundWorker Class (System.ComponentModel)[^] - it's very easy to use and provides "progress" and "completed" events to your main thread. Use the Progress event to signal a new image downloaded (and update your display in the handler) and the completed event to indicate all done. The link includes a basic example of how to use it.
 
Share this answer
 
Comments
Member 11751386 30-Jun-17 8:06am    
Thanks a million for your answer it was superb.
solved my problem.
Thanks
OriginalGriff 30-Jun-17 8:24am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900