c# - 尝试在图片框中播放视频

时间:2017-02-04 11:37:41

标签: c# picturebox

我想在PictureBox中播放一个小视频。我的桌面上有一个文件夹,包含151个扩展名为.png的框架。这是我已经尝试过的:

System.IO.DirectoryInfo di = new DirectoryInfo("c:\\Users\\" + Environment.UserName + "\\Desktop\\fireplace");

foreach (FileInfo file in di.GetFiles())
{
    pictureBox1.Image = file;
}

这不起作用,因为有一个错误说:

  

无法隐式将System.IO.FileInfo类型转换为System.Drawing.Image

我不知道如何FileInfo成为Image。 (顺便说一下,代码中的壁炉文件夹是包含框架的文件夹。)

2 个答案:

答案 0 :(得分:1)

我的建议是将所有图片加载到一个List<Bitmap>中,然后使用Timer更改PictureBox内的图片:

List<Bitmap> _images = new List<Bitmap>();
int _currentImageIndex = 0;

int CurrentImageIndex
{
    get { return _currentImageIndex; }
    set {
        _currentImageIndex = value;
        if (InvokeRequired)
        {
            Invoke(new MethodInvoker( () => { _pictureBox.Image = _images[_currentImageIndex]; } );
        }
        else 
        {
            _pictureBox.Image = _images[_currentImageIndex];
        }
    }
}

Bitmap LoadImage(Stream stream)
{
    return new Bitmap(stream, false);
}

public void LoadImages(DirectoryInfo dInfo)
{
    foreach(FileInfo fInfo in dInfo.GetFiles())
    {
        if(InvokeRequired)
        {
            Invoke(new MethodInvoker( () => { _images.Add(LoadImage (fInfo.Open(FileMode.Open))); });
        }
        else
        {
            _images.Add(LoadImage (fInfo.Open()));
        }
    }
}

void WhenTimerTicks(object sender, EventArgs e)
{
    if(CurrentImageIndex < _images.Count)
        CurrentImageIndex++;
}

现在你所要做的就是阅读文件,然后设置你的计时器:

LoadImages(new DirectoryInfo("c:\\Users\\" + Environment.UserName + "\\Desktop\\fireplace"));

Timer t = new Timer();
t.Interval = 1000 / 25; // 25 FPS
t.Tick += WhenTimerTicks;
t.Start();

答案 1 :(得分:0)

m.rogalski:

List<Bitmap> _images = new List<Bitmap>();
    int _currentImageIndex = 0;

    int CurrentImageIndex
    {
        get { return _currentImageIndex; }
        set
        {
            _currentImageIndex = value;
            if (InvokeRequired)
            {
                Invoke(new MethodInvoker(() => { pictureBox1.Image = _images[_currentImageIndex]; });
            }
            else
            {
                pictureBox1.Image = _images[_currentImageIndex];
            }
        }
    }

    Bitmap LoadImage(Stream stream)
    {
        return new Bitmap(stream, false);
    }

    public void LoadImages(DirectoryInfo dInfo)
    {
        foreach (FileInfo fInfo in dInfo.GetFiles())
        {
            if (InvokeRequired)
            {
                Invoke(new MethodInvoker(() => { _images.Add(fInfo.Open(FileMode.Open)); })); //Argument 1: cannot convert from 'System.IO.FileStream' to 'System.Drawing.Bitmap'
            }
            else
            {
                _images.Add(fInfo.Open(FileMode.Open)); //Argument 1: cannot convert from 'System.IO.FileStream' to 'System.Drawing.Bitmap'
            }
        }
    }

    void WhenTimerTicks(object sender, EventArgs e)
    {
        if (CurrentImageIndex < _images.Count)
            CurrentImageIndex++;
    }