简体   繁体   English

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

[英]c# - trying to play a video in a picturebox

I want to play a small video in a PictureBox .我想在PictureBox播放一个小视频。 I have a folder on my desktop containing 151 frames with a .png extension.我的桌面上有一个文件夹,其中包含 151 个带有 .png 扩展名的帧。 This is what I have already tried:这是我已经尝试过的:

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

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

This doesn't work because there's an error that says:这不起作用,因为有一个错误说:

Cannot implicitly convert type System.IO.FileInfo to System.Drawing.Image无法将System.IO.FileInfo类型隐式转换为System.Drawing.Image

I don't know how to make FileInfo to an Image .我不知道如何将FileInfo变成Image (btw. the fireplace folder in the code is the folder that contains the frames.) (顺便说一句。代码中的壁炉文件夹是包含框架的文件夹。)

My suggestion would be to load all of the images into one List<Bitmap> and then use Timer to change the pictures inside PictureBox :我的建议是将所有图像加载到一个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++;
}

Now all you have to do is to read the files and after that set up your timer :现在你所要做的就是阅读文件,然后设置你的计时器:

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

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

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++;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM