-1

I have been working on my AI for a while now, but i can't seem to get my AI to display a random GIF image into my Picture Box from this location.

C:\Users\scatt\Desktop\Marvel-J.A.R.V.I.S-Personal-Assistant-Winform-C--master\Marvel J.A.R.V.I.S Personal Assistant\Resources\AIPICS\

Example of what's needed.

string[] imagePaths1 = Directory.GetFiles(@"C:\Users\scatt\Desktop\Marvel-J.A.R.V.I.S-Personal-Assistant-Winform-C--master\images", "*.gif", SearchOption.AllDirectories);

But instead of Audio files I need GIF images.

This is a sample of the code I have been using.

case "test":
                int image1;
                Random randim = new Random();
                image1 = randim.Next(0, 4);

                switch (image1)
                {
                    case 0:
                        pictureBox2.Image = Image.FromFile(@"C:\Users\scatt\Desktop\Marvel-J.A.R.V.I.S-Personal-Assistant-Winform-C--master\Marvel J.A.R.V.I.S Personal Assistant\Resources\AIPICS\giphy.gif");
                        pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
                        pictureBox1.CancelAsync();
                        break;
                    case 1:
                        pictureBox2.Image = Image.FromFile(@"C:\Users\scatt\Desktop\Marvel-J.A.R.V.I.S-Personal-Assistant-Winform-C--master\Marvel J.A.R.V.I.S Personal Assistant\Resources\AIPICS\Party!.gif");
                        pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
                        pictureBox1.CancelAsync();
                        break;
                    case 2:
                        pictureBox2.Image = Image.FromFile(@"C:\Users\scatt\Desktop\Marvel-J.A.R.V.I.S-Personal-Assistant-Winform-C--master\Marvel J.A.R.V.I.S Personal Assistant\Resources\AIPICS\Staredown.gif");
                        pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
                        pictureBox1.CancelAsync();
                        break;

                    case 3:
                        pictureBox2.Image = Image.FromFile(@"C:\Users\scatt\Desktop\Marvel-J.A.R.V.I.S-Personal-Assistant-Winform-C--master\Marvel J.A.R.V.I.S Personal Assistant\Resources\AIPICS\tenor.gif");
                        pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
                        pictureBox1.CancelAsync();
                        break;

                    case 4:
                        pictureBox2.Image = Image.FromFile(@"C:\Users\scatt\Desktop\Marvel-J.A.R.V.I.S-Personal-Assistant-Winform-C--master\Marvel J.A.R.V.I.S Personal Assistant\Resources\AIPICS\idgaf-obama.gif");
                        pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
                        pictureBox1.CancelAsync();
                        break;

                    case 5:
                        pictureBox2.Image = Image.FromFile(@"C:\Users\scatt\Desktop\Marvel-J.A.R.V.I.S-Personal-Assistant-Winform-C--master\Marvel J.A.R.V.I.S Personal Assistant\Resources\AIPICS\homealone.gif");
                        pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
                        pictureBox1.CancelAsync();
                        break;

                    case 6:
                        pictureBox2.Image = Image.FromFile(@"C:\Users\scatt\Desktop\Marvel-J.A.R.V.I.S-Personal-Assistant-Winform-C--master\Marvel J.A.R.V.I.S Personal Assistant\Resources\AIPICS\giphy.gif");
                        pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
                        pictureBox1.CancelAsync();
                        break;

                    case 7:
                        pictureBox2.Image = Image.FromFile(@"C:\Users\scatt\Desktop\Marvel-J.A.R.V.I.S-Personal-Assistant-Winform-C--master\Marvel J.A.R.V.I.S Personal Assistant\Resources\AIPICS\fastsoccer.gif");
                        pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
                        pictureBox1.CancelAsync();
                        break;

                    case 8:
                        pictureBox2.Image = Image.FromFile(@"C:\Users\scatt\Desktop\Marvel-J.A.R.V.I.S-Personal-Assistant-Winform-C--master\Marvel J.A.R.V.I.S Personal Assistant\Resources\AIPICS\wallstreet.gif");
                        pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
                        pictureBox1.CancelAsync();
                        break;


                    case 9:
                        pictureBox2.Image = Image.FromFile(@"C:\Users\scatt\Desktop\Marvel-J.A.R.V.I.S-Personal-Assistant-Winform-C--master\Marvel J.A.R.V.I.S Personal Assistant\Resources\AIPICS\DYjbX.gif");
                        pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
                        pictureBox1.CancelAsync();
                        break;



                }

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • 1
    What does your code do now? What do you want it to do instead? – mjwills May 16 '18 at 03:36
  • That's very good to know thank you. – smokey johnson May 16 '18 at 03:41
  • and i'm still learning, i was guessing it was the amount it will randomize threw that i added in the ' switch (image1) { case 0: pictureBox2.Image = Image.FromFile(@"C:\Users\scatt\Desktop\Marvel-J.A.R.V.I.S-Personal-Assistant-Winform-C--master\Marvel J.A.R.V.I.S Personal Assistant\Resources\AIPICS\giphy.gif"); pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage; pictureBox1.CancelAsync(); break;' – smokey johnson May 16 '18 at 03:44
  • Right now it only randomizes the images i add in 'case 1, case 2, case 3 i want it to randomize all images in a folder instead. @mjwills – smokey johnson May 16 '18 at 03:46
  • Woah the downvoters are out in force, let me rectify – TheGeneral May 16 '18 at 04:05
  • You haven't asked us a question. – Enigmativity May 16 '18 at 04:11
  • 1
    My question was how can i randomize the gif images in a folder to a picture box instead of the way i was originaly doing, i have tried multiple times so i thought i would ask for help, and the community was more than helpful, thank you for everyone's help so many great ideas! and pointers i can thank you enough. – smokey johnson May 16 '18 at 04:15

2 Answers2

1

This code may get you started:

var random = new Random();
var folder = @"C:\Users\scatt\Desktop\Marvel-J.A.R.V.I.S-Personal-Assistant-Winform-C--master\images";

var fileNames = Directory.EnumerateFiles(folder, "*.gif", SearchOption.AllDirectories)
    .Select((file, index) => new {file, index})
    .ToDictionary(z => z.index, y => y.file);

var randomPic = fileNames[random.Next(0, fileNames.Count)];
pictureBox2.Image = MediaTypeNames.Image.FromFile(Path.Combine(folder, randomPic));
pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.CancelAsync();

The main thing is getting the filenames into a data structure. I use a Dictionary - you could just as easily use a List using:

var random = new Random();
var folder = @"C:\Users\scatt\Desktop\Marvel-J.A.R.V.I.S-Personal-Assistant-Winform-C--master\images";

var fileNames = Directory.EnumerateFiles(folder, "*.gif", SearchOption.AllDirectories).ToList();

var randomPic = fileNames[random.Next(0, fileNames.Count)];
pictureBox2.Image = MediaTypeNames.Image.FromFile(Path.Combine(folder, randomPic));
pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.CancelAsync();

Then randomly choose a filename and populate pictureBox2 with that filename.

You should also strongly consider moving random to be a static field.

mjwills
  • 23,389
  • 6
  • 40
  • 63
  • Why create a dictionary from an index? Why not just create an array (from which you can just index naturally)? – Enigmativity May 16 '18 at 04:14
  • That would also work @Enigmativity. I used `Dictionary` partly to try and teach something new to the OP. I'll change the answer to show the `List` code explicitly - thanks for asking. – mjwills May 16 '18 at 04:15
0

you can use the following :-

var basePath =
    @"C:\Users\scatt\Desktop\Marvel-J.A.R.V.I.S-Personal-Assistant-Winform-C--master\Marvel J.A.R.V.I.S Personal Assistant\Resources\AIPICS\";

var pics = System.IO.Directory.EnumerateFiles(basePath, "*.gif").ToArray();
var randomPic = pics.OrderBy(p => Guid.NewGuid()).First();
pictureBox2.Image = MediaTypeNames.Image.FromFile(randomPic);
pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.CancelAsync();
Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156