Click here to Skip to main content
15,911,306 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to develop a education game, I need to create a random picture box and a progress bar, after 10 seconds then picture box will change to another picture. Anybody knows how to code?
Posted
Updated 6-Apr-15 17:33pm
Comments
[no name] 6-Apr-15 22:53pm    
why progress bar?
do you want to show 10 seconds count in progress bar?
visualStudio2010 6-Apr-15 23:16pm    
yes
visualStudio2010 6-Apr-15 23:16pm    
because I develop a spelling game for kids.
[no name] 7-Apr-15 0:04am    
are you using Windows Forms or WPF?
visualStudio2010 7-Apr-15 0:09am    
windows forms

The key here is: don't use PictureBox. I mean, you can use it if you have already available images, but if you want to generate anything random, or any other dynamic behavior, this control won't help at all, will only add troubles, extra waste of your development time and some resources. I'll tell you what to do instead:
Append a picture within picturebox[^],
draw a rectangle in C#[^],
How do I clear a panel from old drawing[^].

For more detail on graphics rendering, please see my past answer:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
capture the drawing on a panel[^],
Drawing Lines between mdi child forms[^],
How to speed up my vb.net application?[^].

Pay attention for the last answer. You may need to change the view on a non-UI thread (even if you use a timer), so you need UI thread invocation mechanism. Please see my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

If you use one specific timer, System.Windows.Forms.Timer, you won't need to use invocation. The problem with this timer is very low accuracy. But its accuracy, unacceptable for anything periodic, can suit your for a single use. 10 seconds delay (but why?) may not really require any accuracy.

—SA
 
Share this answer
 
v2
Hello,
May be this will help you.
change the code as per your need...
I used Background worker (not timer)

PictureBox control name: picturebox1
Progress Bar Control name: progressbar1


first of all
create variables.

C#
DirectoryInfo di;
List<FileInfo> images= new List<FileInfo>();
string dirpath = @"C:\Images"; // set your image directory path
int timeTemp = 0;
BackgroundWorker backgroundWorker = new BackgroundWorker
{
    WorkerReportsProgress = true,
    WorkerSupportsCancellation = true
};


In Form Load event
C#
di = new DirectoryInfo(dirpath);

GetImagesOfType("*.jpg");//will add .jpg files to list - add other file types below
//GetImagesOfType("*.png"); 

backgroundWorker.DoWork += BackgroundWorkerOnDoWork;
backgroundWorker.ProgressChanged += BackgroundWorkerOnProgressChanged;
ChangePicture();
if (!backgroundWorker.IsBusy)
   backgroundWorker.RunWorkerAsync();


Add below events for Background Worker

C#
private void BackgroundWorkerOnProgressChanged(object sender, ProgressChangedEventArgs e)
{
   progressBar1.Value = e.ProgressPercentage;
}

private void BackgroundWorkerOnDoWork(object sender, DoWorkEventArgs e)
{
   BackgroundWorker worker = (BackgroundWorker)sender;
   while (!worker.CancellationPending)
   {
    Thread.Sleep(1000);
    if (timeTemp < 100) timeTemp += 10;
    else
    {
       timeTemp = 0;
       ChangePicture();
    }
    worker.ReportProgress(timeTemp);
   }
}


Change Picture Method
C#
public void ChangePicture()//to change pics
        {
            Random d = new Random();
            int index = d.Next(0, images.Count);
            FileInfo item = images.ElementAt(index);
            pictureBox1.Image = Image.FromFile(item.FullName);
        }


Get Images Method
C#
public void GetImagesOfType(string type)// will read all the images of specified TYPE from the source
       {
           FileInfo[] Img = di.GetFiles(type);
           foreach (FileInfo im in Img)
               images.Add(im);
       }
 
Share this answer
 
v2

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