locked
Load Pictures to PictureBoxes whether need Thread or not? RRS feed

  • Question

  • When I open a Form, there are six PictureBoxes in it, so I need to load six pictures from folder. 
    This steps whether I need to use Thread? if yes, please share me a sample. If not, please share me a reason. 

    Hope someone would help me resolved it, thank you very much.

    Monday, April 24, 2017 1:07 AM

Answers

  • The following form has six PictureBox controls. I placed six images in the Bin\Debug folder one level down e.g. Bin\Debug\Images.

    The code adds the PictureBox controls into a Dictionary which is used in ImageLoad class Execute method where in the Execute method we get all images in the Image folder. If there are six images then load them, if the count is different abort. Now you can tinker with this to you hearts content over the count matching or perhaps alter the code to dynamically create the PictureBox controls which I'm not doing. Also I'm targeting .JPG, again if this does not suit your requirements change it :-)

    Note I names the PictureBox controls PictureBox1 through PictureBox6.

    During the load the form remains responsive. 

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication3
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private async void Form1_Load(object sender, EventArgs e)
            {
                var demo = new ImageLoader();
    
                // get our picture boxes
                demo.dictPictureBoxes = Controls.OfType<PictureBox>().ToList()
                    .Select((element, index) => new { element, index })
                    .ToDictionary(ele => ele.index , ele => ele.element);
    
                await demo.DoStuff();
                if (demo.Success)
                {
                    MessageBox.Show("Done");
                }
                else
                {
                    MessageBox.Show("Failed");
                }
                
            }
        }
        public class ImageLoader
        {
            public Dictionary<int,PictureBox> dictPictureBoxes { get; set; }
            bool success = false;
            public bool Success { get { return success; } }
            public async Task DoStuff()
            {
                await Task.Run(async () =>
                {
                    await Execute();
                });
            }
            private async Task<bool> Execute()
            {
    
                var filePaths = Directory.GetFiles(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Images"), "*.jpg")
                    .Select((item, index) => new { File = item, Index = index }).ToList();
    
                // if there are more files then pictures abort
                if (filePaths.Count != dictPictureBoxes.Count)
                {
                    return Success;
                }
    
                for (int i = 0; i < filePaths.Count(); i++)
                {
                    await Task.Delay(0);
                    // load image file into picture box
                    dictPictureBoxes[i].Image = Image.FromFile(filePaths[i].File);
                }
    
                success = true;
    
                return Success;
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by GoyourGod Wednesday, April 26, 2017 1:47 AM
    Wednesday, April 26, 2017 1:32 AM

All replies

  • Hi GoyourGod,

    Why do you think about Thead?  Does it take long to load six pictures in six PictureBoxes?

    I suppose you have not written code yet. If this is correct, leap before you look. It would not take so long that you imagine.  
    ___________
    Ashidacchi
    Monday, April 24, 2017 2:35 AM
  • Thank you friend, but in my side, I want to find these six pictures. Every one customer has six pictures, so if I have 10000 customers, there are 6000 pictures. When I search six pictures for a specify custom, whether need much more time?

    My customers not more than 1000. Than you Ashidacchi, please share your idea. :)

    Monday, April 24, 2017 3:38 AM
  • Hi GoyourGod and qing_,

      # Do you work for the same company?

    I suppose your real issue is searching / collecting six pictures from many pictures, not loading pictures in PictureBoxes.
    I'd like to confirm:
    Are all pictures in different places, or are they in the same place?
    Could you explain where they are allocated, and how is your environment?
    ___________
    Ashidacchi 

    • Edited by Ashidacchi Monday, April 24, 2017 5:01 AM
    Monday, April 24, 2017 4:55 AM
  • I have two account, they are all myself. 

    there is only one folder which stored all pictures.

    Thank you Ashidacchi

    Monday, April 24, 2017 6:09 AM
  • Hi GoyourGod,

      # Oh! They are all yourself.  Thank you, I understand.

    All pictures are in a single folder.  OK, I've got it.
    How do you select / identify six pictures among them?  i.e. What is the criteria?
    Do you have six file names to select?

    I'm afraid your information is partial, doesn't give me a whole image of your issue. So, "ask and then answer" will repeat many times. Please provide me all information for me to make answer._____________
    Ashidacchi
    Monday, April 24, 2017 6:20 AM
  • Please refer to the following screenshot, but I just want to know whether I need to use thread to find the six pictures. :)

    Monday, April 24, 2017 6:24 AM
  • Hi,

      # I feel disappointed a little.  You have not answered my question still. 

    But, if you want to use thread, please use Async and Await.

    Asynchronous Programming with async and await (C#)
    https://msdn.microsoft.com/en-us/library/mt674882.aspx?f=255&MSPPError=-2147217396

    Asynchronous Programming with Async and Await (Visual Basic)
    https://msdn.microsoft.com/en-us/library/mt674902.aspx
    ____________
    Ashidacchi

    Monday, April 24, 2017 6:32 AM
  • Hi friend, I just want to know, when a form loaded and need to get six pictures from a folder, whether I need to use async to update the PictureBox. My question is simple.
    Monday, April 24, 2017 7:25 AM
  • Hi,

    You have added question one after another, without answering my question.
    Anyway, whether you need to use thread depends on time to get pictures from a folder.
    So, I asked in my prior post:
    How do you select / identify six pictures among them?  i.e. What is the criteria?
    Do you have six file names to select?

    One more question:
    How long does it take for you to collect six pictures and load them in six PictureBoxes?
    ___________________
    Ashidacchi


    • Edited by Ashidacchi Monday, April 24, 2017 7:58 AM
    • Marked as answer by GoyourGod Wednesday, April 26, 2017 1:19 AM
    • Unmarked as answer by GoyourGod Wednesday, April 26, 2017 1:48 AM
    Monday, April 24, 2017 7:41 AM
  • The following form has six PictureBox controls. I placed six images in the Bin\Debug folder one level down e.g. Bin\Debug\Images.

    The code adds the PictureBox controls into a Dictionary which is used in ImageLoad class Execute method where in the Execute method we get all images in the Image folder. If there are six images then load them, if the count is different abort. Now you can tinker with this to you hearts content over the count matching or perhaps alter the code to dynamically create the PictureBox controls which I'm not doing. Also I'm targeting .JPG, again if this does not suit your requirements change it :-)

    Note I names the PictureBox controls PictureBox1 through PictureBox6.

    During the load the form remains responsive. 

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication3
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private async void Form1_Load(object sender, EventArgs e)
            {
                var demo = new ImageLoader();
    
                // get our picture boxes
                demo.dictPictureBoxes = Controls.OfType<PictureBox>().ToList()
                    .Select((element, index) => new { element, index })
                    .ToDictionary(ele => ele.index , ele => ele.element);
    
                await demo.DoStuff();
                if (demo.Success)
                {
                    MessageBox.Show("Done");
                }
                else
                {
                    MessageBox.Show("Failed");
                }
                
            }
        }
        public class ImageLoader
        {
            public Dictionary<int,PictureBox> dictPictureBoxes { get; set; }
            bool success = false;
            public bool Success { get { return success; } }
            public async Task DoStuff()
            {
                await Task.Run(async () =>
                {
                    await Execute();
                });
            }
            private async Task<bool> Execute()
            {
    
                var filePaths = Directory.GetFiles(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Images"), "*.jpg")
                    .Select((item, index) => new { File = item, Index = index }).ToList();
    
                // if there are more files then pictures abort
                if (filePaths.Count != dictPictureBoxes.Count)
                {
                    return Success;
                }
    
                for (int i = 0; i < filePaths.Count(); i++)
                {
                    await Task.Delay(0);
                    // load image file into picture box
                    dictPictureBoxes[i].Image = Image.FromFile(filePaths[i].File);
                }
    
                success = true;
    
                return Success;
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by GoyourGod Wednesday, April 26, 2017 1:47 AM
    Wednesday, April 26, 2017 1:32 AM
  • Thank you very much, please look my other thread that about datagrideview, I have posted my new idea, whether you could add that feature. :)
    Wednesday, April 26, 2017 1:48 AM