Results 1 to 3 of 3

Thread: [RESOLVED] Preload photos, then randomly show them

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    Resolved [RESOLVED] Preload photos, then randomly show them

    Hi,
    I would like to preload 10-20 photos (200kB or so each) when the program starts so that I can randomly show them quicker later.

    Please show me how to display them in picturebox, command button or maybe draw them on a form side by side. I need to be able to switch the pictures or move them around with a press of a button.

    Tried the following code to load them, but don't know how to display them. Also let me know if there's a better way of doing this.

    Code:
    ImageList pics = new System.Windows.Forms.ImageList();
    
    private void button1_Click(object sender, System.EventArgs e)
    {
    
        // Get all the picture files in the current directory.
        string[] picFiles = Directory.GetFiles(Application.StartupPath, "*.jpg");
    
        // Create an Image object for each file and add it to the ImageList.
        foreach (string pic in picFiles)
        {
            Bitmap newB = new Bitmap(pic);
            pics.Images.Add(newB);
        }
    }
    Thanks

    Tomexx.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Preload photos, then randomly show them

    Code:
    private Image[] pictures;
    private Random rng = new Random();
    
    private void Form1_Load(object sender, EventArgs e)
    {
        string[] files = System.IO.Directory.GetFiles(Application.StartupPath, "*.jpg");
    
        this.pictures = new Image[files.Length];
    
        for (int index = 0; index < files.Length; index++)
        {
            this.pictures[index] = Image.FromFile(files[index]);
        }
    }
    
    private void button1_Click(object sender, EventArgs e)
    {
        this.pictureBox1.Image = this.pictures[this.rng.Next(0, this.pictures.Length)];
    }
    Don't use an ImageList unless you have some specific use for an ImageList, like for a control that allows you to specify an ImageList and ImageIndex to select an Image, e.g. ListView or TreeView
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    Re: Preload photos, then randomly show them

    Thanks alot!
    Thanks

    Tomexx.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width