Random Image Generator
-
I am working on a project that takes 30 images from file, stuffs them in an array and displays them sequentially in a random order. There is a 30 second delay between each image, like a slide show, and no image can be displayed more than once. I got everthing down except for the dont display the same image twice and the end the sequence once every image was displayed. Here is my code.
using System;
using System.Drawing;
using System.Windows.Forms;namespace Random_Image_Generator
{
public partial class mainWindow : Form
{
int timeKeeper;
Bitmap[] images = new Bitmap[30];public mainWindow() { InitializeComponent(); timer1.Interval = 1000; images\[0\] = new Bitmap(Bitmap.FromFile("0.bmp")); images\[1\] = new Bitmap(Bitmap.FromFile("1.bmp")); images\[2\] = new Bitmap(Bitmap.FromFile("2.bmp")); images\[3\] = new Bitmap(Bitmap.FromFile("3.bmp")); images\[4\] = new Bitmap(Bitmap.FromFile("4.bmp")); images\[5\] = new Bitmap(Bitmap.FromFile("5.bmp")); images\[6\] = new Bitmap(Bitmap.FromFile("6.bmp")); images\[7\] = new Bitmap(Bitmap.FromFile("7.bmp")); images\[8\] = new Bitmap(Bitmap.FromFile("8.bmp")); images\[9\] = new Bitmap(Bitmap.FromFile("9.bmp")); images\[10\] = new Bitmap(Bitmap.FromFile("10.bmp")); images\[11\] = new Bitmap(Bitmap.FromFile("11.bmp")); images\[12\] = new Bitmap(Bitmap.FromFile("12.bmp")); images\[13\] = new Bitmap(Bitmap.FromFile("13.bmp")); images\[14\] = new Bitmap(Bitmap.FromFile("14.bmp")); images\[15\] = new Bitmap(Bitmap.FromFile("15.bmp")); images\[16\] = new Bitmap(Bitmap.FromFile("16.bmp")); images\[17\] = new Bitmap(Bitmap.FromFile("17.bmp")); images\[18\] = new Bitmap(Bitmap.FromFile("18.bmp")); images\[19\] = new Bitmap(Bitmap.FromFile("19.bmp")); images\[20\] = new Bitmap(Bitmap.FromFile("20.bmp")); images\[21\] = new Bitmap(Bitmap.FromFile("21.bmp")); images\[22\] = new Bitmap(Bitmap.FromFile("22.bmp")); images\[23\] = new Bitmap(Bitmap.FromFile("23.bmp")); images\[24\] = new Bitmap(Bitmap.FromFile("24.bmp")); images\[25\] = new Bitmap(Bitmap.FromFile("25.bmp")); images\[26\] = new Bitmap(Bitmap.FromFile("26.bmp")); images\[27\] = new Bitmap(Bitmap.FromFile("27.bmp"))
List<int> num = new List<int>();
private void nextImage()
{
int randNum = 0;
do
{
randNum = RandomNumber(0, 29);
}while(num.Contains(randNum));
num.Add(randNum);
imageArea.Image = images[randNum];
}
I know the language. I've read a book. - _Madmatt
modified on Monday, March 22, 2010 8:43 PM
-
I am working on a project that takes 30 images from file, stuffs them in an array and displays them sequentially in a random order. There is a 30 second delay between each image, like a slide show, and no image can be displayed more than once. I got everthing down except for the dont display the same image twice and the end the sequence once every image was displayed. Here is my code.
using System;
using System.Drawing;
using System.Windows.Forms;namespace Random_Image_Generator
{
public partial class mainWindow : Form
{
int timeKeeper;
Bitmap[] images = new Bitmap[30];public mainWindow() { InitializeComponent(); timer1.Interval = 1000; images\[0\] = new Bitmap(Bitmap.FromFile("0.bmp")); images\[1\] = new Bitmap(Bitmap.FromFile("1.bmp")); images\[2\] = new Bitmap(Bitmap.FromFile("2.bmp")); images\[3\] = new Bitmap(Bitmap.FromFile("3.bmp")); images\[4\] = new Bitmap(Bitmap.FromFile("4.bmp")); images\[5\] = new Bitmap(Bitmap.FromFile("5.bmp")); images\[6\] = new Bitmap(Bitmap.FromFile("6.bmp")); images\[7\] = new Bitmap(Bitmap.FromFile("7.bmp")); images\[8\] = new Bitmap(Bitmap.FromFile("8.bmp")); images\[9\] = new Bitmap(Bitmap.FromFile("9.bmp")); images\[10\] = new Bitmap(Bitmap.FromFile("10.bmp")); images\[11\] = new Bitmap(Bitmap.FromFile("11.bmp")); images\[12\] = new Bitmap(Bitmap.FromFile("12.bmp")); images\[13\] = new Bitmap(Bitmap.FromFile("13.bmp")); images\[14\] = new Bitmap(Bitmap.FromFile("14.bmp")); images\[15\] = new Bitmap(Bitmap.FromFile("15.bmp")); images\[16\] = new Bitmap(Bitmap.FromFile("16.bmp")); images\[17\] = new Bitmap(Bitmap.FromFile("17.bmp")); images\[18\] = new Bitmap(Bitmap.FromFile("18.bmp")); images\[19\] = new Bitmap(Bitmap.FromFile("19.bmp")); images\[20\] = new Bitmap(Bitmap.FromFile("20.bmp")); images\[21\] = new Bitmap(Bitmap.FromFile("21.bmp")); images\[22\] = new Bitmap(Bitmap.FromFile("22.bmp")); images\[23\] = new Bitmap(Bitmap.FromFile("23.bmp")); images\[24\] = new Bitmap(Bitmap.FromFile("24.bmp")); images\[25\] = new Bitmap(Bitmap.FromFile("25.bmp")); images\[26\] = new Bitmap(Bitmap.FromFile("26.bmp")); images\[27\] = new Bitmap(Bitmap.FromFile("27.bmp"))
Hi, 1.
sebogawa wrote:
images[0] = new Bitmap(Bitmap.FromFile("0.bmp")); images[1] = new Bitmap(Bitmap.FromFile("1.bmp")); images[2] = new Bitmap(Bitmap.FromFile("2.bmp")); images[3] = new Bitmap(Bitmap.FromFile("3.bmp")); images[4] = new Bitmap(Bitmap.FromFile("4.bmp")); images[5] = new Bitmap(Bitmap.FromFile("5.bmp")); images[6] = new Bitmap(Bitmap.FromFile("6.bmp")); images[7] = new Bitmap(Bitmap.FromFile("7.bmp")); images[8] = new Bitmap(Bitmap.FromFile("8.bmp")); images[9] = new Bitmap(Bitmap.FromFile("9.bmp")); images[10] = new Bitmap(Bitmap.FromFile("10.bmp")); images[11] = new Bitmap(Bitmap.FromFile("11.bmp")); images[12] = new Bitmap(Bitmap.FromFile("12.bmp")); images[13] = new Bitmap(Bitmap.FromFile("13.bmp")); images[14] = new Bitmap(Bitmap.FromFile("14.bmp")); images[15] = new Bitmap(Bitmap.FromFile("15.bmp")); images[16] = new Bitmap(Bitmap.FromFile("16.bmp")); images[17] = new Bitmap(Bitmap.FromFile("17.bmp")); images[18] = new Bitmap(Bitmap.FromFile("18.bmp")); images[19] = new Bitmap(Bitmap.FromFile("19.bmp")); images[20] = new Bitmap(Bitmap.FromFile("20.bmp")); images[21] = new Bitmap(Bitmap.FromFile("21.bmp")); images[22] = new Bitmap(Bitmap.FromFile("22.bmp")); images[23] = new Bitmap(Bitmap.FromFile("23.bmp")); images[24] = new Bitmap(Bitmap.FromFile("24.bmp")); images[25] = new Bitmap(Bitmap.FromFile("25.bmp")); images[26] = new Bitmap(Bitmap.FromFile("26.bmp")); images[27] = new Bitmap(Bitmap.FromFile("27.bmp")); images[28] = new Bitmap(Bitmap.FromFile("28.bmp")); images[29] = new Bitmap(Bitmap.FromFile("29.bmp"));
Did you notice some similarity between those lines? Take advantage of it, write a simple 2-line loop that does exactly the same, saving over 1000 characters (and keystrokes?). 2. How would you do it by hand? 3. Hint: forget about arrays, this is another great place for a collection. Look at List<Bitmap> :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
modified on Monday, March 22, 2010 8:45 PM
-
List<int> num = new List<int>();
private void nextImage()
{
int randNum = 0;
do
{
randNum = RandomNumber(0, 29);
}while(num.Contains(randNum));
num.Add(randNum);
imageArea.Image = images[randNum];
}
I know the language. I've read a book. - _Madmatt
modified on Monday, March 22, 2010 8:43 PM
It is not completely obvious how that avoids duplicates. ;P I would be inclined to collect images, not numbers; and pick one out of an "available" collection, not stuff them in a "used" collection. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
-
I am working on a project that takes 30 images from file, stuffs them in an array and displays them sequentially in a random order. There is a 30 second delay between each image, like a slide show, and no image can be displayed more than once. I got everthing down except for the dont display the same image twice and the end the sequence once every image was displayed. Here is my code.
using System;
using System.Drawing;
using System.Windows.Forms;namespace Random_Image_Generator
{
public partial class mainWindow : Form
{
int timeKeeper;
Bitmap[] images = new Bitmap[30];public mainWindow() { InitializeComponent(); timer1.Interval = 1000; images\[0\] = new Bitmap(Bitmap.FromFile("0.bmp")); images\[1\] = new Bitmap(Bitmap.FromFile("1.bmp")); images\[2\] = new Bitmap(Bitmap.FromFile("2.bmp")); images\[3\] = new Bitmap(Bitmap.FromFile("3.bmp")); images\[4\] = new Bitmap(Bitmap.FromFile("4.bmp")); images\[5\] = new Bitmap(Bitmap.FromFile("5.bmp")); images\[6\] = new Bitmap(Bitmap.FromFile("6.bmp")); images\[7\] = new Bitmap(Bitmap.FromFile("7.bmp")); images\[8\] = new Bitmap(Bitmap.FromFile("8.bmp")); images\[9\] = new Bitmap(Bitmap.FromFile("9.bmp")); images\[10\] = new Bitmap(Bitmap.FromFile("10.bmp")); images\[11\] = new Bitmap(Bitmap.FromFile("11.bmp")); images\[12\] = new Bitmap(Bitmap.FromFile("12.bmp")); images\[13\] = new Bitmap(Bitmap.FromFile("13.bmp")); images\[14\] = new Bitmap(Bitmap.FromFile("14.bmp")); images\[15\] = new Bitmap(Bitmap.FromFile("15.bmp")); images\[16\] = new Bitmap(Bitmap.FromFile("16.bmp")); images\[17\] = new Bitmap(Bitmap.FromFile("17.bmp")); images\[18\] = new Bitmap(Bitmap.FromFile("18.bmp")); images\[19\] = new Bitmap(Bitmap.FromFile("19.bmp")); images\[20\] = new Bitmap(Bitmap.FromFile("20.bmp")); images\[21\] = new Bitmap(Bitmap.FromFile("21.bmp")); images\[22\] = new Bitmap(Bitmap.FromFile("22.bmp")); images\[23\] = new Bitmap(Bitmap.FromFile("23.bmp")); images\[24\] = new Bitmap(Bitmap.FromFile("24.bmp")); images\[25\] = new Bitmap(Bitmap.FromFile("25.bmp")); images\[26\] = new Bitmap(Bitmap.FromFile("26.bmp")); images\[27\] = new Bitmap(Bitmap.FromFile("27.bmp"))
How about loading your images into a
List<Bitmap>
. Apply a random Sort algorithm, then create aQueue<Bitmap>
from the list. Now you can just dequeue each image as you display it.Dave
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
It is not completely obvious how that avoids duplicates. ;P I would be inclined to collect images, not numbers; and pick one out of an "available" collection, not stuff them in a "used" collection. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
Not the best, but neither was the original code ;P I agree that a lot of duplicate typing could have been avoided.
I know the language. I've read a book. - _Madmatt
-
I am working on a project that takes 30 images from file, stuffs them in an array and displays them sequentially in a random order. There is a 30 second delay between each image, like a slide show, and no image can be displayed more than once. I got everthing down except for the dont display the same image twice and the end the sequence once every image was displayed. Here is my code.
using System;
using System.Drawing;
using System.Windows.Forms;namespace Random_Image_Generator
{
public partial class mainWindow : Form
{
int timeKeeper;
Bitmap[] images = new Bitmap[30];public mainWindow() { InitializeComponent(); timer1.Interval = 1000; images\[0\] = new Bitmap(Bitmap.FromFile("0.bmp")); images\[1\] = new Bitmap(Bitmap.FromFile("1.bmp")); images\[2\] = new Bitmap(Bitmap.FromFile("2.bmp")); images\[3\] = new Bitmap(Bitmap.FromFile("3.bmp")); images\[4\] = new Bitmap(Bitmap.FromFile("4.bmp")); images\[5\] = new Bitmap(Bitmap.FromFile("5.bmp")); images\[6\] = new Bitmap(Bitmap.FromFile("6.bmp")); images\[7\] = new Bitmap(Bitmap.FromFile("7.bmp")); images\[8\] = new Bitmap(Bitmap.FromFile("8.bmp")); images\[9\] = new Bitmap(Bitmap.FromFile("9.bmp")); images\[10\] = new Bitmap(Bitmap.FromFile("10.bmp")); images\[11\] = new Bitmap(Bitmap.FromFile("11.bmp")); images\[12\] = new Bitmap(Bitmap.FromFile("12.bmp")); images\[13\] = new Bitmap(Bitmap.FromFile("13.bmp")); images\[14\] = new Bitmap(Bitmap.FromFile("14.bmp")); images\[15\] = new Bitmap(Bitmap.FromFile("15.bmp")); images\[16\] = new Bitmap(Bitmap.FromFile("16.bmp")); images\[17\] = new Bitmap(Bitmap.FromFile("17.bmp")); images\[18\] = new Bitmap(Bitmap.FromFile("18.bmp")); images\[19\] = new Bitmap(Bitmap.FromFile("19.bmp")); images\[20\] = new Bitmap(Bitmap.FromFile("20.bmp")); images\[21\] = new Bitmap(Bitmap.FromFile("21.bmp")); images\[22\] = new Bitmap(Bitmap.FromFile("22.bmp")); images\[23\] = new Bitmap(Bitmap.FromFile("23.bmp")); images\[24\] = new Bitmap(Bitmap.FromFile("24.bmp")); images\[25\] = new Bitmap(Bitmap.FromFile("25.bmp")); images\[26\] = new Bitmap(Bitmap.FromFile("26.bmp")); images\[27\] = new Bitmap(Bitmap.FromFile("27.bmp"))
Use a List, pick a random index (0 to Count-1 ), display and remove that image, repeat until empty.
-
Use a List, pick a random index (0 to Count-1 ), display and remove that image, repeat until empty.
Yes, and while we're at it: Why load all these bitmaps in advance at all? I would only load the one currently selected for display. For selection an array of filenames will suffice. This will go a little easier on the memory. With 30 bitmaps this may not appear important, but still it is a waste of memory. Ans when one day the program is expanded to support 300 bitmaps (or 30000, 3000000 ...) you will get to see something I have not seen since the days of the old C64: An out of memory exception.
A while ago he asked me what he should have printed on my business cards. I said 'Wizard'. I read books which nobody else understand. Then I do something which nobody understands. After that the computer does something which nobody understands. When asked, I say things about the results which nobody understand. But everybody expects miracles from me on a regular basis. Looks to me like the classical definition of a wizard.
-
I am working on a project that takes 30 images from file, stuffs them in an array and displays them sequentially in a random order. There is a 30 second delay between each image, like a slide show, and no image can be displayed more than once. I got everthing down except for the dont display the same image twice and the end the sequence once every image was displayed. Here is my code.
using System;
using System.Drawing;
using System.Windows.Forms;namespace Random_Image_Generator
{
public partial class mainWindow : Form
{
int timeKeeper;
Bitmap[] images = new Bitmap[30];public mainWindow() { InitializeComponent(); timer1.Interval = 1000; images\[0\] = new Bitmap(Bitmap.FromFile("0.bmp")); images\[1\] = new Bitmap(Bitmap.FromFile("1.bmp")); images\[2\] = new Bitmap(Bitmap.FromFile("2.bmp")); images\[3\] = new Bitmap(Bitmap.FromFile("3.bmp")); images\[4\] = new Bitmap(Bitmap.FromFile("4.bmp")); images\[5\] = new Bitmap(Bitmap.FromFile("5.bmp")); images\[6\] = new Bitmap(Bitmap.FromFile("6.bmp")); images\[7\] = new Bitmap(Bitmap.FromFile("7.bmp")); images\[8\] = new Bitmap(Bitmap.FromFile("8.bmp")); images\[9\] = new Bitmap(Bitmap.FromFile("9.bmp")); images\[10\] = new Bitmap(Bitmap.FromFile("10.bmp")); images\[11\] = new Bitmap(Bitmap.FromFile("11.bmp")); images\[12\] = new Bitmap(Bitmap.FromFile("12.bmp")); images\[13\] = new Bitmap(Bitmap.FromFile("13.bmp")); images\[14\] = new Bitmap(Bitmap.FromFile("14.bmp")); images\[15\] = new Bitmap(Bitmap.FromFile("15.bmp")); images\[16\] = new Bitmap(Bitmap.FromFile("16.bmp")); images\[17\] = new Bitmap(Bitmap.FromFile("17.bmp")); images\[18\] = new Bitmap(Bitmap.FromFile("18.bmp")); images\[19\] = new Bitmap(Bitmap.FromFile("19.bmp")); images\[20\] = new Bitmap(Bitmap.FromFile("20.bmp")); images\[21\] = new Bitmap(Bitmap.FromFile("21.bmp")); images\[22\] = new Bitmap(Bitmap.FromFile("22.bmp")); images\[23\] = new Bitmap(Bitmap.FromFile("23.bmp")); images\[24\] = new Bitmap(Bitmap.FromFile("24.bmp")); images\[25\] = new Bitmap(Bitmap.FromFile("25.bmp")); images\[26\] = new Bitmap(Bitmap.FromFile("26.bmp")); images\[27\] = new Bitmap(Bitmap.FromFile("27.bmp"))
Hi I would try the following approach. Create the class in which you store the bitmap and a boolean field, which tells you if the image was already shown:
public class ImageItem
{
private Bitmap _image;
public bool Shown{get;set;}public ImageItem(Bitmap image) { \_image = image; Shown = false; }
}
private void timer1_Tick(object sender, EventArgs e)
{
if(images.Any(image => image.Shown == false))
{
nextImage()
}
//You shown all the images
else
{
//quit.
}
}private void nextImage(Bitmap image)
{
int randNumber;
do
{
randNumber = RandomNumber(0, 29);
}while(images[randNumber].Shown != false)
images[randNumber].Shown = true;
imageArea.Image = images[randNumber];
}I typed the code directly to the page(without visual studio) so I hope I didn't make any typos. Uros
-
Hi I would try the following approach. Create the class in which you store the bitmap and a boolean field, which tells you if the image was already shown:
public class ImageItem
{
private Bitmap _image;
public bool Shown{get;set;}public ImageItem(Bitmap image) { \_image = image; Shown = false; }
}
private void timer1_Tick(object sender, EventArgs e)
{
if(images.Any(image => image.Shown == false))
{
nextImage()
}
//You shown all the images
else
{
//quit.
}
}private void nextImage(Bitmap image)
{
int randNumber;
do
{
randNumber = RandomNumber(0, 29);
}while(images[randNumber].Shown != false)
images[randNumber].Shown = true;
imageArea.Image = images[randNumber];
}I typed the code directly to the page(without visual studio) so I hope I didn't make any typos. Uros
So 12 hours later you other responses and pass it off as your own X|
I know the language. I've read a book. - _Madmatt
-
So 12 hours later you other responses and pass it off as your own X|
I know the language. I've read a book. - _Madmatt