显示指定位置的随机GIF图像

时间:2018-05-16 03:32:21

标签: c# visual-studio visual-studio-2017

我已经在AI上工作了一段时间,但我似乎无法让我的AI在这个位置显示随机GIF图像进入我的图片框。

  

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 \

需要什么的例子。

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

但是我需要GIF图像而不是音频文件。

这是我一直使用的代码示例。

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;



                }

2 个答案:

答案 0 :(得分:1)

此代码可以帮助您入门:

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();

主要是将文件名转换为数据结构。我使用Dictionary - 您可以轻松地使用List使用:

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();

然后随机选择一个文件名并用该文件名填充pictureBox2

您还应该strongly consider移动random成为static字段。

答案 1 :(得分:0)

您可以使用以下内容: -

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();