按钮点击从资源随机播放图片 [英] Shuffle random picture from resources on button click

查看:115
本文介绍了按钮点击从资源随机播放图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有1个图片框,名为 studPic 。我想要的是,当我点击随机播放按钮时,从资源中获取随机图像。

I have 1 picture box, named studPic. What I want to get is, when I click "shuffle" button, to get random image from resources.

private void button2_Click(object sender, EventArgs e)
        {
...     
        }

经过研究我发现以下

http:// social.msdn.microsoft.com/Forums/en-US/Vsexpressvcs/thread/38d36fa4-1f5f-43e9-80f6-3c9a4edc7705/

我是新手到C#..有没有更简单的方法来实现这个结果?例如,不添加图片名称?

I'm newbie to C#.. Is there easier way to achieve this result? For example, without adding picture names?

List<string> pictureNames = new List<string>();
pictureNames.Add("1");
pictureNames.Add("2");
pictureNames.Add("3");
int randomPictureIndex = new Random().Next(0, pictureNames.Count);
string randomPictureName = pictureNames[randomPictureIndex];
pictureNames.Remove(randomPictureName);
Image img = Properties.Resources.randomPictureName; //erroor
studPic.Image = img;

收到错误消息
错误1'Properties.Resources'不包含'的定义' randomPictureName'

getting error message Error 1 'Properties.Resources' does not contain a definition for 'randomPictureName'

推荐答案

我不会为此使用系统资源。它们不像断开连接软件的文件系统那样可维护。

I wouldn't use System Resources for this. They're not as maintainable as the file system for disconnected software.

将您的图像放在应用程序的文件夹中。通过这种方式,可以轻松更新/更改它们。
说:

Have your images in a folder for your app. This way they can be updated/changed easily. Say :

C:\Ninjas - app
c:\Ninjas\images - images

创建一个包含这些图像的数组。

Create an array that holds these images.

string[] files = System.IO.Directory.GetFiles("c:\ninjas\images");

您需要在GetFiles上放置一些过滤器,以确保您只能获得图片。

You'll need to put some filters on the GetFiles to ensure you only get pictures.

现在抓住该数组中的一个随机位置(你已经显示你知道如何做随机数)。

Now grab a random position in that array (you've already shown you know how to do random numbers).

我们有阵列,让它们随机播放然后你可以顺序通过它们(比随机选择更快.CPU会爱你吧)

We have the array, let's shuffle it and then you can go through them sequentially (way faster than randomly picking on. CPU will love you for it)

    private string[] files;
    private int currentIndex = 0;

    private void initializeImages()
    {
        //Grab directories in your images directory
        string appRoot = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
        files = System.IO.Directory.GetFiles(appRoot + @"\images");
        Random rnd = new Random();
        files = files.OrderBy(x => rnd.Next()).ToArray();
    }


    private void setImage()
    {
        pictureBox1.ImageLocation = files[currentIndex];
    }

    private void previousImage()
    {
        currentIndex = currentIndex > 0 ? currentIndex - 1 : 0;
        setImage();
    }

    private void nextImage()
    {
        currentIndex = currentIndex < files.Length - 1 ? currentIndex + 1 : files.Length - 1;
        setImage();
    }

一些事情:


  • 不要对文件路径进行硬编码。在你的app.config文件中有这个,并引用它。

  • 你可以将文件数组放在全局,这样就不需要每次都运行它。

如果您希望将此作为幻灯片放映直到用户取消它,我建议您使用以下内容:

If you want to have this as a slide show that runs until the user cancels it I'd recommend the following:


  • 使用调用方法的计时器对象来增加图像计数,从而更改图像。

  • 不要在GUI上使用thread.sleep,因为它会暂停你的GUI - 这不是一件好事。

如果你想添加下一个/上一个按钮,你需要有一个可以增加/减少的全局索引(比如currentIndex),然后调用代码来设置图像

If you want to add Next/Previous buttons, you'll need to have a global index (say currentIndex) that can be increased/decreased, then call code to set the image

这篇关于按钮点击从资源随机播放图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆